Skip to content

Commit

Permalink
Sticky context shows line number
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraTenshi authored and matoous committed Oct 16, 2022
1 parent fe24074 commit 3614b69
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use helix_core::{
use helix_view::{
apply_transaction,
document::{Mode, SCRATCH_BUFFER_NAME},
editor::{CompleteAction, CursorShapeConfig},
editor::{CompleteAction, CursorShapeConfig, LineNumber},
graphics::{Color, CursorKind, Modifier, Rect, Style},
input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
keyboard::{KeyCode, KeyModifiers},
Expand Down Expand Up @@ -157,11 +157,14 @@ impl EditorView {
&editor.config(),
);

let mut context_ln = None;
if editor.config().sticky_context {
Self::render_sticky_context(editor, doc, view, surface, theme);
context_ln = Self::render_sticky_context(editor, doc, view, surface, theme);
}

Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused);
Self::render_gutter(
editor, doc, view, view.area, surface, theme, is_focused, context_ln,
);
Self::render_rulers(editor, doc, view, inner, surface, theme);

if is_focused {
Expand Down Expand Up @@ -424,7 +427,7 @@ impl EditorView {
view: &View,
surface: &mut Surface,
theme: &Theme,
) {
) -> Option<Vec<usize>> {
if let Some(syntax) = doc.syntax() {
let tree = syntax.tree();
let text = doc.text().slice(..);
Expand Down Expand Up @@ -466,8 +469,9 @@ impl EditorView {
let mut context_area = view.inner_area();
context_area.height = 1;

let mut line_numbers = Vec::new();
for line_num in context {
if line_num > view.offset.row {
if line_num >= view.offset.row {
continue;
}
surface.clear_with(context_area, context_style);
Expand All @@ -485,7 +489,21 @@ impl EditorView {
);

context_area.y += 1;
let line_number = match editor.config().line_number {
LineNumber::Absolute => line_num,
LineNumber::Relative => {
let res = text.byte_to_line(cursor_byte) - line_num;
match res {
n if n < 2 => 1,
_ => res - 1,
}
}
};
line_numbers.push(line_number);
}
Some(line_numbers)
} else {
None
}
}

Expand Down Expand Up @@ -790,6 +808,7 @@ impl EditorView {
surface: &mut Surface,
theme: &Theme,
is_focused: bool,
context_ln: Option<Vec<usize>>,
) {
let text = doc.text().slice(..);
let last_line = view.last_line(doc);
Expand All @@ -814,7 +833,12 @@ impl EditorView {
for (constructor, width) in view.gutters() {
let gutter = constructor(editor, doc, view, theme, is_focused, *width);
text.reserve(*width); // ensure there's enough space for the gutter
for (i, line) in (view.offset.row..(last_line + 1)).enumerate() {
for (i, mut line) in (view.offset.row..(last_line + 1)).enumerate() {
if let Some(ref line_numbers) = context_ln {
if line_numbers.len() > i {
line = line_numbers[i];
}
}
let selected = cursors.contains(&line);
let x = viewport.x + offset;
let y = viewport.y + i as u16;
Expand Down

0 comments on commit 3614b69

Please sign in to comment.