Improve tree-sitter-subtree (#11663)

* Make unnamed nodes visible in subtree view

* Refine command description

* Update generated docs

* Update unit test expected output
This commit is contained in:
chtenb 2024-09-30 03:59:31 +02:00 committed by GitHub
parent 48b89d4dcf
commit 8cdce9212c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 14 deletions

View File

@ -72,7 +72,7 @@
| `:sort` | Sort ranges in selection. | | `:sort` | Sort ranges in selection. |
| `:rsort` | Sort ranges in selection in reverse order. | | `:rsort` | Sort ranges in selection in reverse order. |
| `:reflow` | Hard-wrap the current selection of lines to a given width. | | `: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-reload` | Refresh user config. |
| `:config-open` | Open the user config.toml file. | | `:config-open` | Open the user config.toml file. |
| `:config-open-workspace` | Open the workspace config.toml file. | | `:config-open-workspace` | Open the workspace config.toml file. |

View File

@ -2692,6 +2692,8 @@ fn pretty_print_tree_impl<W: fmt::Write>(
} }
write!(fmt, "({}", node.kind())?; write!(fmt, "({}", node.kind())?;
} else {
write!(fmt, " \"{}\"", node.kind())?;
} }
// Handle children. // Handle children.
@ -2950,7 +2952,7 @@ fn assert_pretty_print(
#[test] #[test]
fn test_pretty_print() { fn test_pretty_print() {
let source = r#"// Hello"#; 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: // A large tree should be indented with fields:
let source = r#"fn main() { let source = r#"fn main() {
@ -2960,16 +2962,16 @@ fn test_pretty_print() {
"rust", "rust",
source, source,
concat!( concat!(
"(function_item\n", "(function_item \"fn\"\n",
" name: (identifier)\n", " name: (identifier)\n",
" parameters: (parameters)\n", " parameters: (parameters \"(\" \")\")\n",
" body: (block\n", " body: (block \"{\"\n",
" (expression_statement\n", " (expression_statement\n",
" (macro_invocation\n", " (macro_invocation\n",
" macro: (identifier)\n", " macro: (identifier) \"!\"\n",
" (token_tree\n", " (token_tree \"(\"\n",
" (string_literal\n", " (string_literal \"\"\"\n",
" (string_content)))))))", " (string_content) \"\"\") \")\")) \";\") \"}\"))",
), ),
0, 0,
source.len(), source.len(),
@ -2981,7 +2983,7 @@ fn test_pretty_print() {
// Error nodes are printed as errors: // Error nodes are printed as errors:
let source = r#"}{"#; 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. // Fields broken under unnamed nodes are determined correctly.
// In the following source, `object` belongs to the `singleton_method` // In the following source, `object` belongs to the `singleton_method`
@ -2996,11 +2998,11 @@ fn test_pretty_print() {
"ruby", "ruby",
source, source,
concat!( concat!(
"(singleton_method\n", "(singleton_method \"def\"\n",
" object: (self)\n", " object: (self) \".\"\n",
" name: (identifier)\n", " name: (identifier)\n",
" body: (body_statement\n", " body: (body_statement\n",
" (true)))" " (true)) \"end\")"
), ),
0, 0,
source.len(), source.len(),

View File

@ -3032,7 +3032,7 @@ fn read(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
TypableCommand { TypableCommand {
name: "tree-sitter-subtree", name: "tree-sitter-subtree",
aliases: &["ts-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, fun: tree_sitter_subtree,
signature: CommandSignature::none(), signature: CommandSignature::none(),
}, },