Skip to content

Commit

Permalink
Merge pull request #2 from s0LA1337/md/lsp-context-linenr
Browse files Browse the repository at this point in the history
Sticky context shows line number
  • Loading branch information
matoous committed Oct 14, 2022
2 parents 5f95619 + 72ce47f commit 2037d39
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 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,12 @@ 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, surface, theme, is_focused, context_ln);
Self::render_rulers(editor, doc, view, inner, surface, theme);

if is_focused {
Expand Down Expand Up @@ -424,7 +425,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,6 +467,7 @@ 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 {
continue;
Expand All @@ -485,7 +487,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 @@ -786,11 +802,12 @@ impl EditorView {
editor: &Editor,
doc: &Document,
view: &View,
viewport: Rect,
surface: &mut Surface,
theme: &Theme,
is_focused: bool,
context_ln: Option<Vec<usize>>,
) {
let viewport = view.area;
let text = doc.text().slice(..);
let last_line = view.last_line(doc);

Expand All @@ -814,7 +831,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 2037d39

Please sign in to comment.