Skip to content

Commit

Permalink
Improve tree-sitter-subtree (#11663)
Browse files Browse the repository at this point in the history
* Make unnamed nodes visible in subtree view

* Refine command description

* Update generated docs

* Update unit test expected output
  • Loading branch information
chtenb authored Sep 30, 2024
1 parent 48b89d4 commit 8cdce92
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
| `:sort` | Sort ranges in selection. |
| `:rsort` | Sort ranges in selection in reverse order. |
| `:reflow` | Hard-wrap the current selection of lines to a given width. |
| `:tree-sitter-subtree`, `:ts-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. |
| `:tree-sitter-subtree`, `:ts-subtree` | Display the smallest tree-sitter subtree that spans the primary selection, primarily for debugging queries. |
| `:config-reload` | Refresh user config. |
| `:config-open` | Open the user config.toml file. |
| `:config-open-workspace` | Open the workspace config.toml file. |
Expand Down
26 changes: 14 additions & 12 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,8 @@ fn pretty_print_tree_impl<W: fmt::Write>(
}

write!(fmt, "({}", node.kind())?;
} else {
write!(fmt, " \"{}\"", node.kind())?;
}

// Handle children.
Expand Down Expand Up @@ -2950,7 +2952,7 @@ mod test {
#[test]
fn test_pretty_print() {
let source = r#"// Hello"#;
assert_pretty_print("rust", source, "(line_comment)", 0, source.len());
assert_pretty_print("rust", source, "(line_comment \"//\")", 0, source.len());

// A large tree should be indented with fields:
let source = r#"fn main() {
Expand All @@ -2960,16 +2962,16 @@ mod test {
"rust",
source,
concat!(
"(function_item\n",
"(function_item \"fn\"\n",
" name: (identifier)\n",
" parameters: (parameters)\n",
" body: (block\n",
" parameters: (parameters \"(\" \")\")\n",
" body: (block \"{\"\n",
" (expression_statement\n",
" (macro_invocation\n",
" macro: (identifier)\n",
" (token_tree\n",
" (string_literal\n",
" (string_content)))))))",
" macro: (identifier) \"!\"\n",
" (token_tree \"(\"\n",
" (string_literal \"\"\"\n",
" (string_content) \"\"\") \")\")) \";\") \"}\"))",
),
0,
source.len(),
Expand All @@ -2981,7 +2983,7 @@ mod test {

// Error nodes are printed as errors:
let source = r#"}{"#;
assert_pretty_print("rust", source, "(ERROR)", 0, source.len());
assert_pretty_print("rust", source, "(ERROR \"}\" \"{\")", 0, source.len());

// Fields broken under unnamed nodes are determined correctly.
// In the following source, `object` belongs to the `singleton_method`
Expand All @@ -2996,11 +2998,11 @@ mod test {
"ruby",
source,
concat!(
"(singleton_method\n",
" object: (self)\n",
"(singleton_method \"def\"\n",
" object: (self) \".\"\n",
" name: (identifier)\n",
" body: (body_statement\n",
" (true)))"
" (true)) \"end\")"
),
0,
source.len(),
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3032,7 +3032,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "tree-sitter-subtree",
aliases: &["ts-subtree"],
doc: "Display tree sitter subtree under cursor, primarily for debugging queries.",
doc: "Display the smallest tree-sitter subtree that spans the primary selection, primarily for debugging queries.",
fun: tree_sitter_subtree,
signature: CommandSignature::none(),
},
Expand Down

0 comments on commit 8cdce92

Please sign in to comment.