From 9bfb0caf1b4bafdac8eb964f38f7820740056fff Mon Sep 17 00:00:00 2001 From: Daniel S Poulin Date: Sun, 6 Mar 2022 00:24:24 -0500 Subject: [PATCH] Add comment textobject for surround selection and navigation (#1605) --- 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 +++++++++ 17 files changed, 95 insertions(+) diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md index 7200a5144..cccd4bbf0 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 5de4edf93..2549fbaf9 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 039628bf8..9ae1eac59 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 8ee05ece7..2a8f462db 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 0147f58e1..a5a615f32 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 b0f036685..45f554e28 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 b0d1b1083..1fa9ded5b 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 67fd6614e..de8caa96c 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 9bcfc6903..d77e14b71 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 73f6f7728..003ce5947 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 3738a3bb9..dd15dc140 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 988e22b49..972b87745 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 04ffefd22..51abe5c7d 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 a52538afd..0ca260890 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 7ee8cd1a9..fa1c4ff0d 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 e3132687a..086db67ae 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 2cb802688..89645b342 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