on master help sign in
Add comment textobject for surround selection and navigation (#1605)
commit9bfb0caf1b4bafdac8eb964f38f7820740056fff authorDaniel S Poulin <dan.poulin@digitalsurgeons.com> dateSun 6 Mar 2022, 00:24 (5 years ago) parent7633c5a
17 files changed+95
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.rs+2 runtime/queries/c/textobjects.scm+4 runtime/queries/cmake/textobjects.scm+9 runtime/queries/fish/textobjects.scm+4 runtime/queries/go/textobjects.scm+4 runtime/queries/llvm-mir/textobjects.scm+9 runtime/queries/llvm/textobjects.scm+4 runtime/queries/perl/textobjects.scm+9 runtime/queries/php/textobjects.scm+4 runtime/queries/python/textobjects.scm+4 runtime/queries/rescript/textobjects.scm+7 runtime/queries/rust/textobjects.scm+9 runtime/queries/tablegen/textobjects.scm+9
book/src/guides/textobject.md+2
@@ -21,6 +21,8 @@ The following [captures][tree-sitter-captures] are recognized:
2121| `class.inside` |
2222| `class.around` |
2323| `parameter.inside` |
24+| `comment.inside` |
25+| `comment.around` |
2426
2527[Example query files][textobject-examples] can be found in the helix GitHub repository.
2628
book/src/keymap.md+2
@@ -270,6 +270,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire
270270| `[c` | Go to previous class (**TS**) | `goto_prev_class` |
271271| `]p` | Go to next parameter (**TS**) | `goto_next_parameter` |
272272| `[p` | Go to previous parameter (**TS**) | `goto_prev_parameter` |
273+| `]o` | Go to next comment (**TS**) | `goto_next_comment` |
274+| `[o` | Go to previous comment (**TS**) | `goto_prev_comment` |
273275| `[space` | Add newline above | `add_newline_above` |
274276| `]space` | Add newline below | `add_newline_below` |
275277
book/src/usage.md+1
@@ -69,6 +69,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`.
6969| `f` | Function |
7070| `c` | Class |
7171| `p` | Parameter |
72+| `o` | Comment |
7273
7374> NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current
7475document and a special tree-sitter query file to work properly. [Only
helix-term/src/commands.rs+12
@@ -401,6 +401,8 @@ impl MappableCommand {
401401 goto_prev_class, "Goto previous class",
402402 goto_next_parameter, "Goto next parameter",
403403 goto_prev_parameter, "Goto previous parameter",
404+ goto_next_comment, "Goto next comment",
405+ goto_prev_comment, "Goto previous comment",
404406 dap_launch, "Launch debug target",
405407 dap_toggle_breakpoint, "Toggle breakpoint",
406408 dap_continue, "Continue program execution",
@@ -5381,6 +5383,14 @@ fn goto_prev_parameter(cx: &mut Context) {
53815383 goto_ts_object_impl(cx, "parameter", Direction::Backward)
53825384}
53835385
5386+fn goto_next_comment(cx: &mut Context) {
5387+ goto_ts_object_impl(cx, "comment", Direction::Forward)
5388+}
5389+
5390+fn goto_prev_comment(cx: &mut Context) {
5391+ goto_ts_object_impl(cx, "comment", Direction::Backward)
5392+}
5393+
53845394fn select_textobject_around(cx: &mut Context) {
53855395 select_textobject(cx, textobject::TextObject::Around);
53865396}
@@ -5423,6 +5433,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
54235433 'c' => textobject_treesitter("class", range),
54245434 'f' => textobject_treesitter("function", range),
54255435 'p' => textobject_treesitter("parameter", range),
5436+ 'o' => textobject_treesitter("comment", range),
54265437 'm' => {
54275438 let ch = text.char(range.cursor(text));
54285439 if !ch.is_ascii_alphanumeric() {
@@ -5456,6 +5467,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
54565467 ("c", "Class (tree-sitter)"),
54575468 ("f", "Function (tree-sitter)"),
54585469 ("p", "Parameter (tree-sitter)"),
5470+ ("o", "Comment (tree-sitter)"),
54595471 ("m", "Matching delimiter under cursor"),
54605472 (" ", "... or any character acting as a pair"),
54615473 ];
helix-term/src/keymap.rs+2
@@ -608,6 +608,7 @@ impl Default for Keymaps {
608608 "f" => goto_prev_function,
609609 "c" => goto_prev_class,
610610 "p" => goto_prev_parameter,
611+ "o" => goto_prev_comment,
611612 "space" => add_newline_above,
612613 },
613614 "]" => { "Right bracket"
@@ -616,6 +617,7 @@ impl Default for Keymaps {
616617 "f" => goto_next_function,
617618 "c" => goto_next_class,
618619 "p" => goto_next_parameter,
620+ "o" => goto_next_comment,
619621 "space" => add_newline_below,
620622 },
621623
runtime/queries/c/textobjects.scm+4
@@ -11,3 +11,7 @@
1111 body: (_) @class.inside) @class.around
1212
1313(parameter_declaration) @parameter.inside
14+
15+(comment) @comment.inside
16+
17+(comment)+ @comment.around
runtime/queries/cmake/textobjects.scm+9
@@ -1,3 +1,12 @@
11(macro_def) @function.around
22
33(argument) @parameter.inside
4+
5+[
6+ (bracket_comment)
7+ (line_comment)
8+] @comment.inside
9+
10+(line_comment)+ @comment.around
11+
12+(bracket_comment) @comment.around
runtime/queries/fish/textobjects.scm+4
@@ -1 +1,5 @@
11(function_definition) @function.around
2+
3+(comment) @comment.inside
4+
5+(comment)+ @comment.around
runtime/queries/go/textobjects.scm+4
@@ -19,3 +19,7 @@
1919
2020(argument_list
2121 (_) @parameter.inside)
22+
23+(comment) @comment.inside
24+
25+(comment)+ @comment.around
runtime/queries/llvm-mir/textobjects.scm+9
@@ -1,3 +1,12 @@
11(basic_block) @function.around
22
33(argument) @parameter.inside
4+
5+[
6+ (comment)
7+ (multiline_comment)
8+] @comment.inside
9+
10+(comment)+ @comment.around
11+
12+(multiline_comment) @comment.around
runtime/queries/llvm/textobjects.scm+4
@@ -14,3 +14,7 @@
1414 (array_vector_body) @class.inside) @class.around
1515
1616(argument) @parameter.inside
17+
18+(comment) @comment.inside
19+
20+(comment)+ @comment.around
runtime/queries/perl/textobjects.scm+9
@@ -6,3 +6,12 @@
66
77(argument
88 (_) @parameter.inside)
9+
10+[
11+ (comments)
12+ (pod_statement)
13+] @comment.inside
14+
15+(comments)+ @comment.around
16+
17+(pod_statement) @comment.around
runtime/queries/php/textobjects.scm+4
@@ -28,3 +28,7 @@
2828 (variadic_parameter)
2929 (property_promotion_parameter)
3030 ] @parameter.inside)
31+
32+(comment) @comment.inside
33+
34+(comment)+ @comment.around
runtime/queries/python/textobjects.scm+4
@@ -12,3 +12,7 @@
1212
1313(argument_list
1414 (_) @parameter.inside)
15+
16+(comment) @comment.inside
17+
18+(comment)+ @comment.around
runtime/queries/rescript/textobjects.scm+7
@@ -7,3 +7,10 @@
77;----------
88
99(function body: (_) @function.inside) @function.around
10+
11+; Comments
12+;---------
13+
14+(comment) @comment.inside
15+
16+(comment)+ @comment.around
runtime/queries/rust/textobjects.scm+9
@@ -24,3 +24,12 @@
2424
2525(arguments
2626 (_) @parameter.inside)
27+
28+[
29+ (line_comment)
30+ (block_comment)
31+] @comment.inside
32+
33+(line_comment)+ @comment.around
34+
35+(block_comment) @comment.around
runtime/queries/tablegen/textobjects.scm+9
@@ -5,3 +5,12 @@
55 body: (_) @class.inside) @class.around
66
77(_ argument: _ @parameter.inside)
8+
9+[
10+ (comment)
11+ (multiline_comment)
12+] @comment.inside
13+
14+(comment)+ @comment.around
15+
16+(multiline_comment) @comment.around