1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-02 20:16:04 +02:00

Add :tree-sitter-scopes, useful when developing indents.toml

This commit is contained in:
Blaž Hrastnik 2021-08-13 13:15:36 +09:00
parent 88cc0f85a5
commit fd1eaafff5
2 changed files with 42 additions and 0 deletions

View File

@ -313,6 +313,27 @@ pub fn suggested_indent_for_pos(
}
}
pub fn get_scopes(syntax: Option<&Syntax>, text: RopeSlice, pos: usize) -> Vec<&'static str> {
let mut scopes = Vec::new();
if let Some(syntax) = syntax {
let byte_start = text.char_to_byte(pos);
let node = match get_highest_syntax_node_at_bytepos(syntax, byte_start) {
Some(node) => node,
None => return scopes,
};
scopes.push(node.kind());
while let Some(parent) = node.parent() {
scopes.push(parent.kind())
}
}
scopes.reverse();
return scopes;
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -1883,6 +1883,20 @@ fn reload(
doc.reload(view.id)
}
fn tree_sitter_scopes(
cx: &mut compositor::Context,
_args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let pos = doc.selection(view.id).primary().cursor(text);
let scopes = indent::get_scopes(doc.syntax(), text, pos);
cx.editor.set_status(format!("scopes: {:?}", &scopes));
Ok(())
}
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
@ -2114,6 +2128,13 @@ fn reload(
doc: "Discard changes and reload from the source file.",
fun: reload,
completer: None,
},
TypableCommand {
name: "tree-sitter-scopes",
alias: None,
doc: "Display tree sitter scopes, primarily for theming and development.",
fun: tree_sitter_scopes,
completer: None,
}
];