From 82728a04912ef1b69d26d913456c01542cb60332 Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Sat, 2 Sep 2023 20:21:37 -0400 Subject: [PATCH 1/3] Support new tree-sitter textobject/movement for entries of list-likes --- book/src/guides/textobject.md | 2 ++ helix-term/src/commands.rs | 12 +++++++++++ helix-term/src/keymap/default.rs | 2 ++ runtime/queries/_typescript/textobjects.scm | 6 ++++++ runtime/queries/bash/textobjects.scm | 3 +++ runtime/queries/c/textobjects.scm | 6 ++++++ runtime/queries/ecma/textobjects.scm | 9 ++++++++ runtime/queries/graphql/textobjects.scm | 23 ++++++++++++++++++++ runtime/queries/java/textobjects.scm | 6 ++++++ runtime/queries/json/textobjects.scm | 5 +++++ runtime/queries/lua/textobjects.scm | 3 +++ runtime/queries/php/textobjects.scm | 12 +++++++++++ runtime/queries/python/textobjects.scm | 12 +++++++++++ runtime/queries/ruby/textobjects.scm | 12 +++++++++++ runtime/queries/rust/textobjects.scm | 24 +++++++++++++++++++++ runtime/queries/toml/textobjects.scm | 5 +++++ 16 files changed, 142 insertions(+) create mode 100644 runtime/queries/graphql/textobjects.scm create mode 100644 runtime/queries/json/textobjects.scm create mode 100644 runtime/queries/toml/textobjects.scm diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md index 405f11c1b00a..66eae198b5f2 100644 --- a/book/src/guides/textobject.md +++ b/book/src/guides/textobject.md @@ -25,6 +25,8 @@ The following [captures][tree-sitter-captures] are recognized: | `parameter.inside` | | `comment.inside` | | `comment.around` | +| `element.inside` | +| `element.around` | [Example query files][textobject-examples] can be found in the helix GitHub repository. diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4ac2496ebd48..049241707c2f 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -470,6 +470,8 @@ impl MappableCommand { goto_prev_comment, "Goto previous comment", goto_next_test, "Goto next test", goto_prev_test, "Goto previous test", + goto_next_entry, "Goto next pairing", + goto_prev_entry, "Goto previous pairing", goto_next_paragraph, "Goto next paragraph", goto_prev_paragraph, "Goto previous paragraph", dap_launch, "Launch debug target", @@ -5100,6 +5102,14 @@ fn goto_prev_test(cx: &mut Context) { goto_ts_object_impl(cx, "test", Direction::Backward) } +fn goto_next_entry(cx: &mut Context) { + goto_ts_object_impl(cx, "entry", Direction::Forward) +} + +fn goto_prev_entry(cx: &mut Context) { + goto_ts_object_impl(cx, "entry", Direction::Backward) +} + fn select_textobject_around(cx: &mut Context) { select_textobject(cx, textobject::TextObject::Around); } @@ -5164,6 +5174,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { 'a' => textobject_treesitter("parameter", range), 'c' => textobject_treesitter("comment", range), 'T' => textobject_treesitter("test", range), + 'e' => textobject_treesitter("entry", range), 'p' => textobject::textobject_paragraph(text, range, objtype, count), 'm' => textobject::textobject_pair_surround_closest( text, range, objtype, count, @@ -5196,6 +5207,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { ("a", "Argument/parameter (tree-sitter)"), ("c", "Comment (tree-sitter)"), ("T", "Test (tree-sitter)"), + ("e", "Data structure entry (tree-sitter)"), ("m", "Closest surrounding pair"), (" ", "... or any character acting as a pair"), ]; diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index bab662b04dd3..1b19324aea11 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -113,6 +113,7 @@ pub fn default() -> HashMap { "t" => goto_prev_class, "a" => goto_prev_parameter, "c" => goto_prev_comment, + "e" => goto_prev_entry, "T" => goto_prev_test, "p" => goto_prev_paragraph, "space" => add_newline_above, @@ -126,6 +127,7 @@ pub fn default() -> HashMap { "t" => goto_next_class, "a" => goto_next_parameter, "c" => goto_next_comment, + "e" => goto_next_entry, "T" => goto_next_test, "p" => goto_next_paragraph, "space" => add_newline_below, diff --git a/runtime/queries/_typescript/textobjects.scm b/runtime/queries/_typescript/textobjects.scm index c248aeade497..09198519fb4c 100644 --- a/runtime/queries/_typescript/textobjects.scm +++ b/runtime/queries/_typescript/textobjects.scm @@ -4,3 +4,9 @@ (type_alias_declaration value: (_) @class.inside) ] @class.around + +(enum_body + (_) @entry.around) + +(enum_assignment (_) @entry.inside) + diff --git a/runtime/queries/bash/textobjects.scm b/runtime/queries/bash/textobjects.scm index 59983b30ceed..09536a6b54cf 100644 --- a/runtime/queries/bash/textobjects.scm +++ b/runtime/queries/bash/textobjects.scm @@ -7,3 +7,6 @@ (comment) @comment.inside (comment)+ @comment.around + +(array + (_) @entry.around) diff --git a/runtime/queries/c/textobjects.scm b/runtime/queries/c/textobjects.scm index 2a3da66f3be9..63ec4a5677f2 100644 --- a/runtime/queries/c/textobjects.scm +++ b/runtime/queries/c/textobjects.scm @@ -19,3 +19,9 @@ (comment) @comment.inside (comment)+ @comment.around + +(enumerator + (_) @entry.inside) @entry.around + +(initializer_list + (_) @entry.around) diff --git a/runtime/queries/ecma/textobjects.scm b/runtime/queries/ecma/textobjects.scm index c80dc81b4cc7..a19eb25bb1c6 100644 --- a/runtime/queries/ecma/textobjects.scm +++ b/runtime/queries/ecma/textobjects.scm @@ -34,3 +34,12 @@ (comment) @comment.inside (comment)+ @comment.around + +(array + (_) @entry.around) + +(pair + (_) @entry.inside) @entry.around + +(pair_pattern + (_) @entry.inside) @entry.around diff --git a/runtime/queries/graphql/textobjects.scm b/runtime/queries/graphql/textobjects.scm new file mode 100644 index 000000000000..b6035367242c --- /dev/null +++ b/runtime/queries/graphql/textobjects.scm @@ -0,0 +1,23 @@ +(type_definition) @class.around + +(executable_definition) @function.around + +(arguments_definition + (input_value_definition) @parameter.inside @parameter.movement) + +(arguments + (argument) @parameter.inside @parameter.movement) + +(selection + [(field) (fragment_spread)] @entry.around) + +(selection + (field (selection_set) @entry.inside)) + +(field_definition + (_) @entry.inside) @entry.around + +(input_fields_definition + (input_value_definition ) @entry.around) + +(enum_value) @entry.around diff --git a/runtime/queries/java/textobjects.scm b/runtime/queries/java/textobjects.scm index b0e73a0a75db..1fe0726a839e 100644 --- a/runtime/queries/java/textobjects.scm +++ b/runtime/queries/java/textobjects.scm @@ -36,3 +36,9 @@ (line_comment)+ @comment.around (block_comment) @comment.around + +(array_initializer + (_) @entry.around) + +(enum_body + (enum_constant) @entry.around) diff --git a/runtime/queries/json/textobjects.scm b/runtime/queries/json/textobjects.scm new file mode 100644 index 000000000000..a8fd57c99415 --- /dev/null +++ b/runtime/queries/json/textobjects.scm @@ -0,0 +1,5 @@ +(pair + (_) @entry.inside) @entry.around + +(array + (_) @entry.around) diff --git a/runtime/queries/lua/textobjects.scm b/runtime/queries/lua/textobjects.scm index 6fb2000df40e..305c3ae7d8ff 100644 --- a/runtime/queries/lua/textobjects.scm +++ b/runtime/queries/lua/textobjects.scm @@ -13,3 +13,6 @@ (comment) @comment.inside (comment)+ @comment.around + +(table_constructor + (field (_) @entry.inside) @entry.around) diff --git a/runtime/queries/php/textobjects.scm b/runtime/queries/php/textobjects.scm index e35eebd767ca..e721b864d46d 100644 --- a/runtime/queries/php/textobjects.scm +++ b/runtime/queries/php/textobjects.scm @@ -38,3 +38,15 @@ (comment) @comment.inside (comment)+ @comment.around + +(array_creation_expression + (array_element_initializer + (_) @entry.inside + ) @entry.around @entry.movement) + +(list_literal + (_) @entry.inside @entry.around @entry.movement) + +[ + (enum_case) +] @entry.around @entry.movement diff --git a/runtime/queries/python/textobjects.scm b/runtime/queries/python/textobjects.scm index 966e47446a69..2b9556fca7bc 100644 --- a/runtime/queries/python/textobjects.scm +++ b/runtime/queries/python/textobjects.scm @@ -21,3 +21,15 @@ name: (identifier) @_name body: (block)? @test.inside) @test.around (#match? @_name "^test_")) + +(list + (_) @entry.around) + +(tuple + (_) @entry.around) + +(set + (_) @entry.around) + +(pair + (_) @entry.inside) @entry.around diff --git a/runtime/queries/ruby/textobjects.scm b/runtime/queries/ruby/textobjects.scm index 2d48fa6fccd7..123c55678cfc 100644 --- a/runtime/queries/ruby/textobjects.scm +++ b/runtime/queries/ruby/textobjects.scm @@ -42,3 +42,15 @@ ; Comments (comment) @comment.inside (comment)+ @comment.around + +(pair + (_) @entry.inside) @entry.around + +(array + (_) @entry.around) + +(string_array + (_) @entry.around) + +(symbol_array + (_) @entry.around) diff --git a/runtime/queries/rust/textobjects.scm b/runtime/queries/rust/textobjects.scm index df26331d8389..de517d362408 100644 --- a/runtime/queries/rust/textobjects.scm +++ b/runtime/queries/rust/textobjects.scm @@ -59,3 +59,27 @@ (function_item body: (_) @test.inside) @test.around (#eq? @_test_attribute "test")) + +(array_expression + (_) @entry.around) + +(tuple_expression + (_) @entry.around) + +(tuple_pattern + (_) @entry.around) + +; Commonly used vec macro intializer is special cased +(macro_invocation + (identifier) @_id (token_tree (_) @entry.around) + (#eq? @_id "vec")) + +(enum_variant) @entry.around + +(field_declaration + (_) @entry.inside) @entry.around + +(field_initializer + (_) @entry.inside) @entry.around + +(shorthand_field_initializer) @entry.around diff --git a/runtime/queries/toml/textobjects.scm b/runtime/queries/toml/textobjects.scm new file mode 100644 index 000000000000..a8fd57c99415 --- /dev/null +++ b/runtime/queries/toml/textobjects.scm @@ -0,0 +1,5 @@ +(pair + (_) @entry.inside) @entry.around + +(array + (_) @entry.around) From e3469c54e0cab729fb39523422aa6f9d90ddc599 Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Tue, 12 Mar 2024 20:14:22 -0400 Subject: [PATCH 2/3] Docgen --- book/src/generated/lang-support.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index 7792bf594181..c60897c3646a 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -65,7 +65,7 @@ | gomod | ✓ | | | `gopls` | | gotmpl | ✓ | | | `gopls` | | gowork | ✓ | | | `gopls` | -| graphql | ✓ | | | `graphql-lsp` | +| graphql | ✓ | ✓ | | `graphql-lsp` | | groovy | ✓ | | | | | hare | ✓ | | | | | haskell | ✓ | ✓ | | `haskell-language-server-wrapper` | @@ -85,7 +85,7 @@ | javascript | ✓ | ✓ | ✓ | `typescript-language-server` | | jinja | ✓ | | | | | jsdoc | ✓ | | | | -| json | ✓ | | ✓ | `vscode-json-language-server` | +| json | ✓ | ✓ | ✓ | `vscode-json-language-server` | | json5 | ✓ | | | | | jsonnet | ✓ | | | `jsonnet-language-server` | | jsx | ✓ | ✓ | ✓ | `typescript-language-server` | @@ -176,7 +176,7 @@ | templ | ✓ | | | `templ` | | tfvars | ✓ | | ✓ | `terraform-ls` | | todotxt | ✓ | | | | -| toml | ✓ | | | `taplo` | +| toml | ✓ | ✓ | | `taplo` | | tsq | ✓ | | | | | tsx | ✓ | ✓ | ✓ | `typescript-language-server` | | twig | ✓ | | | | From 61275a0cf7fc7cb0be03494d5709f2bda0daad9f Mon Sep 17 00:00:00 2001 From: Daniel S Poulin Date: Sun, 31 Mar 2024 09:41:42 -0400 Subject: [PATCH 3/3] Make book consistent with chosen query name Co-authored-by: Michael Davis --- book/src/guides/textobject.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md index 66eae198b5f2..5f59e1bee6d7 100644 --- a/book/src/guides/textobject.md +++ b/book/src/guides/textobject.md @@ -25,8 +25,8 @@ The following [captures][tree-sitter-captures] are recognized: | `parameter.inside` | | `comment.inside` | | `comment.around` | -| `element.inside` | -| `element.around` | +| `entry.inside` | +| `entry.around` | [Example query files][textobject-examples] can be found in the helix GitHub repository.