on master help sign in
text-objects: add test capture & elixir queries
commit67f6c85792dbdbe0ff3f9328874c7ab23ff5569b authorConnor Lay (Clay) <connorlay@pm.me> dateSat 18 Jun 2022, 14:24 (4 years ago) parent43027d9
6 files changed+26−1
book/src/guides/textobject.md+2 book/src/keymap.md+2 book/src/usage.md+1 helix-term/src/commands.rs+12 helix-term/src/keymap/default.rs+2 runtime/queries/elixir/textobjects.scm+7−1
book/src/guides/textobject.md+2
@@ -20,6 +20,8 @@ The following [captures][tree-sitter-captures] are recognized:
2020| `function.around` |
2121| `class.inside` |
2222| `class.around` |
23+| `test.inside` |
24+| `test.around` |
2325| `parameter.inside` |
2426| `comment.inside` |
2527| `comment.around` |
book/src/keymap.md+2
@@ -282,6 +282,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire
282282| `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` |
283283| `]o` | Go to next comment (**TS**) | `goto_next_comment` |
284284| `[o` | Go to previous comment (**TS**) | `goto_prev_comment` |
285+| `]t` | Go to next test (**TS**) | `goto_next_test` |
286+| `]t` | Go to previous test (**TS**) | `goto_prev_test` |
285287| `]p` | Go to next paragraph | `goto_next_paragraph` |
286288| `[p` | Go to previous paragraph | `goto_prev_paragraph` |
287289| `[space` | Add newline above | `add_newline_above` |
book/src/usage.md+1
@@ -143,6 +143,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`.
143143| `c` | Class |
144144| `a` | Argument/parameter |
145145| `o` | Comment |
146+| `t` | Test |
146147
147148> NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current
148149document and a special tree-sitter query file to work properly. [Only
helix-term/src/commands.rs+12
@@ -395,6 +395,8 @@ impl MappableCommand {
395395 goto_prev_parameter, "Goto previous parameter",
396396 goto_next_comment, "Goto next comment",
397397 goto_prev_comment, "Goto previous comment",
398+ goto_next_test, "Goto next test",
399+ goto_prev_test, "Goto previous test",
398400 goto_next_paragraph, "Goto next paragraph",
399401 goto_prev_paragraph, "Goto previous paragraph",
400402 dap_launch, "Launch debug target",
@@ -4098,6 +4100,14 @@ fn goto_prev_comment(cx: &mut Context) {
40984100 goto_ts_object_impl(cx, "comment", Direction::Backward)
40994101}
41004102
4103+fn goto_next_test(cx: &mut Context) {
4104+ goto_ts_object_impl(cx, "test", Direction::Forward)
4105+}
4106+
4107+fn goto_prev_test(cx: &mut Context) {
4108+ goto_ts_object_impl(cx, "test", Direction::Backward)
4109+}
4110+
41014111fn select_textobject_around(cx: &mut Context) {
41024112 select_textobject(cx, textobject::TextObject::Around);
41034113}
@@ -4141,6 +4151,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
41414151 'f' => textobject_treesitter("function", range),
41424152 'a' => textobject_treesitter("parameter", range),
41434153 'o' => textobject_treesitter("comment", range),
4154+ 't' => textobject_treesitter("test", range),
41444155 'p' => textobject::textobject_paragraph(text, range, objtype, count),
41454156 'm' => textobject::textobject_surround_closest(text, range, objtype, count),
41464157 // TODO: cancel new ranges if inconsistent surround matches across lines
@@ -4170,6 +4181,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
41704181 ("f", "Function (tree-sitter)"),
41714182 ("a", "Argument/parameter (tree-sitter)"),
41724183 ("o", "Comment (tree-sitter)"),
4184+ ("t", "Test (tree-sitter)"),
41734185 ("m", "Matching delimiter under cursor"),
41744186 (" ", "... or any character acting as a pair"),
41754187 ];
helix-term/src/keymap/default.rs+2
@@ -104,6 +104,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
104104 "c" => goto_prev_class,
105105 "a" => goto_prev_parameter,
106106 "o" => goto_prev_comment,
107+ "t" => goto_prev_test,
107108 "p" => goto_prev_paragraph,
108109 "space" => add_newline_above,
109110 },
@@ -114,6 +115,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
114115 "c" => goto_next_class,
115116 "a" => goto_next_parameter,
116117 "o" => goto_next_comment,
118+ "t" => goto_next_test,
117119 "p" => goto_next_paragraph,
118120 "space" => add_newline_below,
119121 },
runtime/queries/elixir/textobjects.scm+7−1
@@ -16,7 +16,7 @@
1616 (pair
1717 value: (_) @function.inside))?)?
1818 (do_block (_)* @function.inside)?)
19− (#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp|test|describe|setup)$")) @function.around
19+ (#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$")) @function.around
2020
2121(anonymous_function
2222 (stab_clause right: (body) @function.inside)) @function.around
@@ -25,3 +25,9 @@
2525 target: (identifier) @_keyword
2626 (do_block (_)* @class.inside))
2727 (#match? @_keyword "^(defmodule|defprotocol|defimpl)$")) @class.around
28+
29+((call
30+ target: (identifier) @_keyword
31+ (arguments ((string) . (_)?))
32+ (do_block (_)* @test.inside)?)
33+ (#match? @_keyword "^(test|describe)$")) @test.around