feat: tree sitter path

This adds a "tree-sitter-path" element to the possible status lines, which
is useful for large code bases where you cannot see the beginning of the current
function/class/type etc.

Suggested config:

```
[editor.statusline]
center = ["tree-sitter-path"]
```
pull/8407/head
David Vogt 1 year ago
parent 6f1437e9f3
commit 92ae31925f

@ -158,6 +158,7 @@ where
} }
helix_view::editor::StatusLineElement::Position => render_position, helix_view::editor::StatusLineElement::Position => render_position,
helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage, helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
helix_view::editor::StatusLineElement::TreeSitterPath => render_tree_sitter_path,
helix_view::editor::StatusLineElement::TotalLineNumbers => render_total_line_numbers, helix_view::editor::StatusLineElement::TotalLineNumbers => render_total_line_numbers,
helix_view::editor::StatusLineElement::Separator => render_separator, helix_view::editor::StatusLineElement::Separator => render_separator,
helix_view::editor::StatusLineElement::Spacer => render_spacer, helix_view::editor::StatusLineElement::Spacer => render_spacer,
@ -381,6 +382,55 @@ where
} }
} }
fn render_tree_sitter_path<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
if let Some(syntax) = context.doc.syntax() {
let text = context.doc.text().slice(..);
let pos = text.char_to_byte(
context
.doc
.selection(context.view.id)
.primary()
.cursor(text),
);
let mut node = match syntax
.tree()
.root_node()
.descendant_for_byte_range(pos, pos)
{
Some(node) => node,
None => return,
};
let mut scopes = Vec::new();
loop {
match node.child_by_field_name("name") {
Some(child) => {
let child_id = text.byte_slice(child.byte_range());
scopes.push(format!("\u{203A}{}", child_id));
}
None => {}
}
match node.parent() {
Some(parent) => {
node = parent;
}
None => {
break;
}
}
}
scopes.reverse();
write(context, scopes.join(""), None);
}
}
fn render_file_line_ending<F>(context: &mut RenderContext, write: F) fn render_file_line_ending<F>(context: &mut RenderContext, write: F)
where where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,

@ -557,6 +557,9 @@ pub enum StatusLineElement {
/// The cursor position as a percent of the total file /// The cursor position as a percent of the total file
PositionPercentage, PositionPercentage,
// Path of tree sitter elements
TreeSitterPath,
/// The total line numbers of the current file /// The total line numbers of the current file
TotalLineNumbers, TotalLineNumbers,

Loading…
Cancel
Save