on master help sign in
Add :tree-sitter-scopes, useful when developing indents.toml
commitfd1eaafff57fb40dd0e504c3d5fd5e8abe14b5f5 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 13 Aug 2021, 13:15 (5 years ago) parent88cc0f8
2 files changed+42
helix-core/src/indent.rs+21 helix-term/src/commands.rs+21
helix-core/src/indent.rs+21
@@ -313,6 +313,27 @@ pub fn suggested_indent_for_pos(
313313 }
314314}
315315
316+pub fn get_scopes(syntax: Option<&Syntax>, text: RopeSlice, pos: usize) -> Vec<&'static str> {
317+ let mut scopes = Vec::new();
318+ if let Some(syntax) = syntax {
319+ let byte_start = text.char_to_byte(pos);
320+ let node = match get_highest_syntax_node_at_bytepos(syntax, byte_start) {
321+ Some(node) => node,
322+ None => return scopes,
323+ };
324+
325+ scopes.push(node.kind());
326+
327+ while let Some(parent) = node.parent() {
328+ scopes.push(parent.kind())
329+ }
330+ }
331+
332+ scopes.reverse();
333+
334+ return scopes;
335+}
336+
316337#[cfg(test)]
317338mod test {
318339 use super::*;
helix-term/src/commands.rs+21
@@ -1883,6 +1883,20 @@ mod cmd {
18831883 doc.reload(view.id)
18841884 }
18851885
1886+ fn tree_sitter_scopes(
1887+ cx: &mut compositor::Context,
1888+ _args: &[&str],
1889+ _event: PromptEvent,
1890+ ) -> anyhow::Result<()> {
1891+ let (view, doc) = current!(cx.editor);
1892+ let text = doc.text().slice(..);
1893+
1894+ let pos = doc.selection(view.id).primary().cursor(text);
1895+ let scopes = indent::get_scopes(doc.syntax(), text, pos);
1896+ cx.editor.set_status(format!("scopes: {:?}", &scopes));
1897+ Ok(())
1898+ }
1899+
18861900 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
18871901 TypableCommand {
18881902 name: "quit",
@@ -2114,6 +2128,13 @@ mod cmd {
21142128 doc: "Discard changes and reload from the source file.",
21152129 fun: reload,
21162130 completer: None,
2131+ },
2132+ TypableCommand {
2133+ name: "tree-sitter-scopes",
2134+ alias: None,
2135+ doc: "Display tree sitter scopes, primarily for theming and development.",
2136+ fun: tree_sitter_scopes,
2137+ completer: None,
21172138 }
21182139 ];
21192140