diff options
author | Daniel S Poulin | 2022-03-06 05:24:24 +0000 |
---|---|---|
committer | GitHub | 2022-03-06 05:24:24 +0000 |
commit | 9bfb0caf1b4bafdac8eb964f38f7820740056fff (patch) | |
tree | a2e8120456a2b3d3acb43f881c9499e71eb37fac | |
parent | 7633c5acd30258fc9caca926bfaa264d07d508ec (diff) |
Add comment textobject for surround selection and navigation (#1605)
-rw-r--r-- | book/src/guides/textobject.md | 2 | ||||
-rw-r--r-- | book/src/keymap.md | 2 | ||||
-rw-r--r-- | book/src/usage.md | 1 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 12 | ||||
-rw-r--r-- | helix-term/src/keymap.rs | 2 | ||||
-rw-r--r-- | runtime/queries/c/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/cmake/textobjects.scm | 9 | ||||
-rw-r--r-- | runtime/queries/fish/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/go/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/llvm-mir/textobjects.scm | 9 | ||||
-rw-r--r-- | runtime/queries/llvm/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/perl/textobjects.scm | 9 | ||||
-rw-r--r-- | runtime/queries/php/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/python/textobjects.scm | 4 | ||||
-rw-r--r-- | runtime/queries/rescript/textobjects.scm | 7 | ||||
-rw-r--r-- | runtime/queries/rust/textobjects.scm | 9 | ||||
-rw-r--r-- | runtime/queries/tablegen/textobjects.scm | 9 |
17 files changed, 95 insertions, 0 deletions
diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md index 7200a514..cccd4bbf 100644 --- a/book/src/guides/textobject.md +++ b/book/src/guides/textobject.md @@ -21,6 +21,8 @@ The following [captures][tree-sitter-captures] are recognized: | `class.inside` | | `class.around` | | `parameter.inside` | +| `comment.inside` | +| `comment.around` | [Example query files][textobject-examples] can be found in the helix GitHub repository. diff --git a/book/src/keymap.md b/book/src/keymap.md index 5de4edf9..2549fbaf 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -270,6 +270,8 @@ Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaire | `[c` | Go to previous class (**TS**) | `goto_prev_class` | | `]p` | Go to next parameter (**TS**) | `goto_next_parameter` | | `[p` | Go to previous parameter (**TS**) | `goto_prev_parameter` | +| `]o` | Go to next comment (**TS**) | `goto_next_comment` | +| `[o` | Go to previous comment (**TS**) | `goto_prev_comment` | | `[space` | Add newline above | `add_newline_above` | | `]space` | Add newline below | `add_newline_below` | diff --git a/book/src/usage.md b/book/src/usage.md index 039628bf..9ae1eac5 100644 --- a/book/src/usage.md +++ b/book/src/usage.md @@ -69,6 +69,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`. | `f` | Function | | `c` | Class | | `p` | Parameter | +| `o` | Comment | > 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 diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8ee05ece..2a8f462d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -401,6 +401,8 @@ impl MappableCommand { goto_prev_class, "Goto previous class", goto_next_parameter, "Goto next parameter", goto_prev_parameter, "Goto previous parameter", + goto_next_comment, "Goto next comment", + goto_prev_comment, "Goto previous comment", dap_launch, "Launch debug target", dap_toggle_breakpoint, "Toggle breakpoint", dap_continue, "Continue program execution", @@ -5381,6 +5383,14 @@ fn goto_prev_parameter(cx: &mut Context) { goto_ts_object_impl(cx, "parameter", Direction::Backward) } +fn goto_next_comment(cx: &mut Context) { + goto_ts_object_impl(cx, "comment", Direction::Forward) +} + +fn goto_prev_comment(cx: &mut Context) { + goto_ts_object_impl(cx, "comment", Direction::Backward) +} + fn select_textobject_around(cx: &mut Context) { select_textobject(cx, textobject::TextObject::Around); } @@ -5423,6 +5433,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { 'c' => textobject_treesitter("class", range), 'f' => textobject_treesitter("function", range), 'p' => textobject_treesitter("parameter", range), + 'o' => textobject_treesitter("comment", range), 'm' => { let ch = text.char(range.cursor(text)); if !ch.is_ascii_alphanumeric() { @@ -5456,6 +5467,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { ("c", "Class (tree-sitter)"), ("f", "Function (tree-sitter)"), ("p", "Parameter (tree-sitter)"), + ("o", "Comment (tree-sitter)"), ("m", "Matching delimiter under cursor"), (" ", "... or any character acting as a pair"), ]; diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 0147f58e..a5a615f3 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -608,6 +608,7 @@ impl Default for Keymaps { "f" => goto_prev_function, "c" => goto_prev_class, "p" => goto_prev_parameter, + "o" => goto_prev_comment, "space" => add_newline_above, }, "]" => { "Right bracket" @@ -616,6 +617,7 @@ impl Default for Keymaps { "f" => goto_next_function, "c" => goto_next_class, "p" => goto_next_parameter, + "o" => goto_next_comment, "space" => add_newline_below, }, diff --git a/runtime/queries/c/textobjects.scm b/runtime/queries/c/textobjects.scm index b0f03668..45f554e2 100644 --- a/runtime/queries/c/textobjects.scm +++ b/runtime/queries/c/textobjects.scm @@ -11,3 +11,7 @@ body: (_) @class.inside) @class.around (parameter_declaration) @parameter.inside + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/cmake/textobjects.scm b/runtime/queries/cmake/textobjects.scm index b0d1b108..1fa9ded5 100644 --- a/runtime/queries/cmake/textobjects.scm +++ b/runtime/queries/cmake/textobjects.scm @@ -1,3 +1,12 @@ (macro_def) @function.around (argument) @parameter.inside + +[ + (bracket_comment) + (line_comment) +] @comment.inside + +(line_comment)+ @comment.around + +(bracket_comment) @comment.around
\ No newline at end of file diff --git a/runtime/queries/fish/textobjects.scm b/runtime/queries/fish/textobjects.scm index 67fd6614..de8caa96 100644 --- a/runtime/queries/fish/textobjects.scm +++ b/runtime/queries/fish/textobjects.scm @@ -1 +1,5 @@ (function_definition) @function.around + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/go/textobjects.scm b/runtime/queries/go/textobjects.scm index 9bcfc690..d77e14b7 100644 --- a/runtime/queries/go/textobjects.scm +++ b/runtime/queries/go/textobjects.scm @@ -19,3 +19,7 @@ (argument_list (_) @parameter.inside) + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/llvm-mir/textobjects.scm b/runtime/queries/llvm-mir/textobjects.scm index 73f6f772..003ce594 100644 --- a/runtime/queries/llvm-mir/textobjects.scm +++ b/runtime/queries/llvm-mir/textobjects.scm @@ -1,3 +1,12 @@ (basic_block) @function.around (argument) @parameter.inside + +[ + (comment) + (multiline_comment) +] @comment.inside + +(comment)+ @comment.around + +(multiline_comment) @comment.around diff --git a/runtime/queries/llvm/textobjects.scm b/runtime/queries/llvm/textobjects.scm index 3738a3bb..dd15dc14 100644 --- a/runtime/queries/llvm/textobjects.scm +++ b/runtime/queries/llvm/textobjects.scm @@ -14,3 +14,7 @@ (array_vector_body) @class.inside) @class.around (argument) @parameter.inside + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/perl/textobjects.scm b/runtime/queries/perl/textobjects.scm index 988e22b4..972b8774 100644 --- a/runtime/queries/perl/textobjects.scm +++ b/runtime/queries/perl/textobjects.scm @@ -6,3 +6,12 @@ (argument (_) @parameter.inside) + +[ + (comments) + (pod_statement) +] @comment.inside + +(comments)+ @comment.around + +(pod_statement) @comment.around diff --git a/runtime/queries/php/textobjects.scm b/runtime/queries/php/textobjects.scm index 04ffefd2..51abe5c7 100644 --- a/runtime/queries/php/textobjects.scm +++ b/runtime/queries/php/textobjects.scm @@ -28,3 +28,7 @@ (variadic_parameter) (property_promotion_parameter) ] @parameter.inside) + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/python/textobjects.scm b/runtime/queries/python/textobjects.scm index a52538af..0ca26089 100644 --- a/runtime/queries/python/textobjects.scm +++ b/runtime/queries/python/textobjects.scm @@ -12,3 +12,7 @@ (argument_list (_) @parameter.inside) + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/rescript/textobjects.scm b/runtime/queries/rescript/textobjects.scm index 7ee8cd1a..fa1c4ff0 100644 --- a/runtime/queries/rescript/textobjects.scm +++ b/runtime/queries/rescript/textobjects.scm @@ -7,3 +7,10 @@ ;---------- (function body: (_) @function.inside) @function.around + +; Comments +;--------- + +(comment) @comment.inside + +(comment)+ @comment.around diff --git a/runtime/queries/rust/textobjects.scm b/runtime/queries/rust/textobjects.scm index e3132687..086db67a 100644 --- a/runtime/queries/rust/textobjects.scm +++ b/runtime/queries/rust/textobjects.scm @@ -24,3 +24,12 @@ (arguments (_) @parameter.inside) + +[ + (line_comment) + (block_comment) +] @comment.inside + +(line_comment)+ @comment.around + +(block_comment) @comment.around diff --git a/runtime/queries/tablegen/textobjects.scm b/runtime/queries/tablegen/textobjects.scm index 2cb80268..89645b34 100644 --- a/runtime/queries/tablegen/textobjects.scm +++ b/runtime/queries/tablegen/textobjects.scm @@ -5,3 +5,12 @@ body: (_) @class.inside) @class.around (_ argument: _ @parameter.inside) + +[ + (comment) + (multiline_comment) +] @comment.inside + +(comment)+ @comment.around + +(multiline_comment) @comment.around |