Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Dec 10, 2022
1 parent 41ae90f commit 60ac894
Showing 1 changed file with 64 additions and 67 deletions.
131 changes: 64 additions & 67 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,83 +416,80 @@ impl EditorView {
surface: &mut Surface,
theme: &Theme,
) -> Option<Vec<usize>> {
if let Some(syntax) = doc.syntax() {
let tree = syntax.tree();
let text = doc.text().slice(..);
let cursor_byte = doc.selection(view.id).primary().cursor(text);
let context_nodes = doc
.language_config()
.and_then(|lc| lc.sticky_context_nodes.as_ref());

let mut parent = tree
.root_node()
.descendant_for_byte_range(cursor_byte, cursor_byte)
.and_then(|n| n.parent());

// context is list of numbers of lines that should be rendered in the LSP context
let mut context: Vec<usize> = Vec::new();

while let Some(node) = parent {
let line = text.byte_to_line(node.start_byte());

// if parent of previous node is still on the same line, use the parent node
if let Some(&prev_line) = context.last() {
if prev_line == line {
context.pop();
}
}
let syntax = doc.syntax()?;
let tree = syntax.tree();
let text = doc.text().slice(..);
let cursor_byte = doc.selection(view.id).primary().cursor(text);
let context_nodes = doc
.language_config()
.and_then(|lc| lc.sticky_context_nodes.as_ref());

if context_nodes.map_or(true, |nodes| nodes.iter().any(|n| n == node.kind())) {
context.push(line);
}
let mut parent = tree
.root_node()
.descendant_for_byte_range(cursor_byte, cursor_byte)
.and_then(|n| n.parent());

parent = node.parent();
// context is list of numbers of lines that should be rendered in the LSP context
let mut context: Vec<usize> = Vec::new();

while let Some(node) = parent {
let line = text.byte_to_line(node.start_byte());

// if parent of previous node is still on the same line, use the parent node
if let Some(&prev_line) = context.last() {
if prev_line == line {
context.pop();
}
}
if context_nodes.map_or(true, |nodes| nodes.iter().any(|n| n == node.kind())) {
context.push(line);
}

// we render from top most (last in the list)
context.reverse();
parent = node.parent();
}

// TODO: this probably needs it's own style, although it seems to work well even with cursorline
let context_style = theme.get("ui.cursorline.primary");
let mut context_area = view.inner_area(doc);
context_area.height = 1;
// we render from top most (last in the list)
context.reverse();

let mut line_numbers = Vec::new();
for line_num in context {
if line_num >= view.offset.row {
continue;
}
surface.clear_with(context_area, context_style);
// TODO: this probably needs it's own style, although it seems to work well even with cursorline
let context_style = theme.get("ui.cursorline.primary");
let mut context_area = view.inner_area(doc);
context_area.height = 1;

let offset = Position::new(line_num, 0);
let highlights = Self::doc_syntax_highlights(doc, offset, 1, theme);
Self::render_text_highlights(
doc,
offset,
context_area,
surface,
theme,
highlights,
&editor.config(),
);
let mut line_numbers = Vec::new();
for line_num in context {
if line_num >= view.offset.row {
continue;
}
surface.clear_with(context_area, context_style);

let offset = Position::new(line_num, 0);
let highlights = Self::doc_syntax_highlights(doc, offset, 1, theme);
Self::render_text_highlights(
doc,
offset,
context_area,
surface,
theme,
highlights,
&editor.config(),
);

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,
}
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
}
};
line_numbers.push(line_number);
}

Some(line_numbers)
}

pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
Expand Down

0 comments on commit 60ac894

Please sign in to comment.