Skip to content

Commit

Permalink
Sticky context despawn on cursor contact
Browse files Browse the repository at this point in the history
Remove todo comment
  • Loading branch information
SoraTenshi committed Feb 27, 2023
1 parent a097ec0 commit 7ce93e9
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use super::{document::LineDecoration, lsp::SignatureHelp};

#[derive(Debug)]
pub struct StickyNode {
visual_line: u16,
line_nr: usize,
indicator: Option<String>,
}
Expand Down Expand Up @@ -795,22 +796,20 @@ impl EditorView {
if !config.sticky_context {
return None;
}
//TODO: split up to own logic, before it is actually rendered
// Having a pipeline there would vastly improve usability + reusability
// basically do the following pipeline:
// calculate contexts, that are required
// -> pass line numbers to gutter renderer
// -> pass to initial render document state, which doesn't render
// the reserved space
// -> and now finally render the contextes
// this might lead to solving the issue with the cursor as well as the logic is split
// maybe it's also a good idea to summarize nodes that should be together

let syntax = doc.syntax()?;
let tree = syntax.tree();
let text = doc.text().slice(..);
let viewport = view.inner_area(doc);

let visual_cursor_pos = view
.screen_coords_at_pos(doc, text, doc.selection(view.id).primary().cursor(text))
.expect("cursor should always be visible")
.row as u16;

if visual_cursor_pos == 0 {
return None;
}

// Use the cached nodes to determine the current topmost viewport
let anchor_line = text.char_to_line(view.offset.anchor);
let top_first_byte =
Expand All @@ -827,7 +826,6 @@ impl EditorView {

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

while let Some(node) = parent {
// if the node is smaller than half the viewport height, skip
if (node.end_position().row - node.start_position().row) < viewport.height as usize / 2
Expand All @@ -836,7 +834,7 @@ impl EditorView {
continue;
}

let line = text.byte_to_line(node.start_byte());
let line = text.char_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() {
Expand All @@ -847,6 +845,7 @@ impl EditorView {

if context_nodes.map_or(true, |nodes| nodes.iter().any(|n| n == node.kind())) {
context.push(StickyNode {
visual_line: 0, // with sorting it will be done
line_nr: line,
indicator: None,
});
Expand All @@ -861,13 +860,17 @@ impl EditorView {
}

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

// allow a maximum of a quarter the viewport height
// to be occupied by sticky nodes
context = context
.into_iter()
.take(viewport.height as usize / 4)
.rev()
.take(viewport.height as usize / 3)
.enumerate()
.take_while(|(i, _)| *i + 1 != visual_cursor_pos as usize)
.map(|(index, node)| {
let mut new_node = node;
new_node.visual_line = index as u16;
new_node
})
.collect();

if config.sticky_context_indicator {
Expand All @@ -879,6 +882,7 @@ impl EditorView {
str.push_str(&"─".repeat(side_placeholder));

context.push(StickyNode {
visual_line: context.len() as u16,
line_nr: 0,
indicator: Some(str),
})
Expand Down

0 comments on commit 7ce93e9

Please sign in to comment.