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"]
```
This commit is contained in:
David Vogt 2023-09-26 15:17:45 +02:00
parent 6f1437e9f3
commit 92ae31925f
2 changed files with 53 additions and 0 deletions

View File

@ -158,6 +158,7 @@ fn get_render_function<F>(element_id: StatusLineElementID) -> impl Fn(&mut Rende
}
helix_view::editor::StatusLineElement::Position => render_position,
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::Separator => render_separator,
helix_view::editor::StatusLineElement::Spacer => render_spacer,
@ -381,6 +382,55 @@ fn render_file_encoding<F>(context: &mut RenderContext, write: F)
}
}
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)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,

View File

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