text-objects: add test capture & elixir queries

This commit is contained in:
Connor Lay (Clay) 2022-06-18 14:24:01 -07:00 committed by Michael Davis
parent 43027d9104
commit 67f6c85792
6 changed files with 26 additions and 1 deletions

View file

@ -20,6 +20,8 @@ The following [captures][tree-sitter-captures] are recognized:
| `function.around` | | `function.around` |
| `class.inside` | | `class.inside` |
| `class.around` | | `class.around` |
| `test.inside` |
| `test.around` |
| `parameter.inside` | | `parameter.inside` |
| `comment.inside` | | `comment.inside` |
| `comment.around` | | `comment.around` |

View file

@ -282,6 +282,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire
| `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` | | `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` |
| `]o` | Go to next comment (**TS**) | `goto_next_comment` | | `]o` | Go to next comment (**TS**) | `goto_next_comment` |
| `[o` | Go to previous comment (**TS**) | `goto_prev_comment` | | `[o` | Go to previous comment (**TS**) | `goto_prev_comment` |
| `]t` | Go to next test (**TS**) | `goto_next_test` |
| `]t` | Go to previous test (**TS**) | `goto_prev_test` |
| `]p` | Go to next paragraph | `goto_next_paragraph` | | `]p` | Go to next paragraph | `goto_next_paragraph` |
| `[p` | Go to previous paragraph | `goto_prev_paragraph` | | `[p` | Go to previous paragraph | `goto_prev_paragraph` |
| `[space` | Add newline above | `add_newline_above` | | `[space` | Add newline above | `add_newline_above` |

View file

@ -143,6 +143,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`.
| `c` | Class | | `c` | Class |
| `a` | Argument/parameter | | `a` | Argument/parameter |
| `o` | Comment | | `o` | Comment |
| `t` | Test |
> NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current > NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current
document and a special tree-sitter query file to work properly. [Only document and a special tree-sitter query file to work properly. [Only

View file

@ -395,6 +395,8 @@ impl MappableCommand {
goto_prev_parameter, "Goto previous parameter", goto_prev_parameter, "Goto previous parameter",
goto_next_comment, "Goto next comment", goto_next_comment, "Goto next comment",
goto_prev_comment, "Goto previous comment", goto_prev_comment, "Goto previous comment",
goto_next_test, "Goto next test",
goto_prev_test, "Goto previous test",
goto_next_paragraph, "Goto next paragraph", goto_next_paragraph, "Goto next paragraph",
goto_prev_paragraph, "Goto previous paragraph", goto_prev_paragraph, "Goto previous paragraph",
dap_launch, "Launch debug target", dap_launch, "Launch debug target",
@ -4098,6 +4100,14 @@ fn goto_prev_comment(cx: &mut Context) {
goto_ts_object_impl(cx, "comment", Direction::Backward) goto_ts_object_impl(cx, "comment", Direction::Backward)
} }
fn goto_next_test(cx: &mut Context) {
goto_ts_object_impl(cx, "test", Direction::Forward)
}
fn goto_prev_test(cx: &mut Context) {
goto_ts_object_impl(cx, "test", Direction::Backward)
}
fn select_textobject_around(cx: &mut Context) { fn select_textobject_around(cx: &mut Context) {
select_textobject(cx, textobject::TextObject::Around); select_textobject(cx, textobject::TextObject::Around);
} }
@ -4141,6 +4151,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
'f' => textobject_treesitter("function", range), 'f' => textobject_treesitter("function", range),
'a' => textobject_treesitter("parameter", range), 'a' => textobject_treesitter("parameter", range),
'o' => textobject_treesitter("comment", range), 'o' => textobject_treesitter("comment", range),
't' => textobject_treesitter("test", range),
'p' => textobject::textobject_paragraph(text, range, objtype, count), 'p' => textobject::textobject_paragraph(text, range, objtype, count),
'm' => textobject::textobject_surround_closest(text, range, objtype, count), 'm' => textobject::textobject_surround_closest(text, range, objtype, count),
// TODO: cancel new ranges if inconsistent surround matches across lines // TODO: cancel new ranges if inconsistent surround matches across lines
@ -4170,6 +4181,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
("f", "Function (tree-sitter)"), ("f", "Function (tree-sitter)"),
("a", "Argument/parameter (tree-sitter)"), ("a", "Argument/parameter (tree-sitter)"),
("o", "Comment (tree-sitter)"), ("o", "Comment (tree-sitter)"),
("t", "Test (tree-sitter)"),
("m", "Matching delimiter under cursor"), ("m", "Matching delimiter under cursor"),
(" ", "... or any character acting as a pair"), (" ", "... or any character acting as a pair"),
]; ];

View file

@ -104,6 +104,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"c" => goto_prev_class, "c" => goto_prev_class,
"a" => goto_prev_parameter, "a" => goto_prev_parameter,
"o" => goto_prev_comment, "o" => goto_prev_comment,
"t" => goto_prev_test,
"p" => goto_prev_paragraph, "p" => goto_prev_paragraph,
"space" => add_newline_above, "space" => add_newline_above,
}, },
@ -114,6 +115,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"c" => goto_next_class, "c" => goto_next_class,
"a" => goto_next_parameter, "a" => goto_next_parameter,
"o" => goto_next_comment, "o" => goto_next_comment,
"t" => goto_next_test,
"p" => goto_next_paragraph, "p" => goto_next_paragraph,
"space" => add_newline_below, "space" => add_newline_below,
}, },

View file

@ -16,7 +16,7 @@
(pair (pair
value: (_) @function.inside))?)? value: (_) @function.inside))?)?
(do_block (_)* @function.inside)?) (do_block (_)* @function.inside)?)
(#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp|test|describe|setup)$")) @function.around (#match? @_keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$")) @function.around
(anonymous_function (anonymous_function
(stab_clause right: (body) @function.inside)) @function.around (stab_clause right: (body) @function.inside)) @function.around
@ -25,3 +25,9 @@
target: (identifier) @_keyword target: (identifier) @_keyword
(do_block (_)* @class.inside)) (do_block (_)* @class.inside))
(#match? @_keyword "^(defmodule|defprotocol|defimpl)$")) @class.around (#match? @_keyword "^(defmodule|defprotocol|defimpl)$")) @class.around
((call
target: (identifier) @_keyword
(arguments ((string) . (_)?))
(do_block (_)* @test.inside)?)
(#match? @_keyword "^(test|describe)$")) @test.around