Skip to content

Commit

Permalink
Add comment textobject for surround selection and navigation (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
EpocSquadron committed Mar 6, 2022
1 parent 7633c5a commit 9bfb0ca
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions book/src/guides/textobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

Expand Down
1 change: 1 addition & 0 deletions book/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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"),
];
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
},

Expand Down
4 changes: 4 additions & 0 deletions runtime/queries/c/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
body: (_) @class.inside) @class.around

(parameter_declaration) @parameter.inside

(comment) @comment.inside

(comment)+ @comment.around
9 changes: 9 additions & 0 deletions runtime/queries/cmake/textobjects.scm
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions runtime/queries/fish/textobjects.scm
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
(function_definition) @function.around

(comment) @comment.inside

(comment)+ @comment.around
4 changes: 4 additions & 0 deletions runtime/queries/go/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

(argument_list
(_) @parameter.inside)

(comment) @comment.inside

(comment)+ @comment.around
9 changes: 9 additions & 0 deletions runtime/queries/llvm-mir/textobjects.scm
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
(basic_block) @function.around

(argument) @parameter.inside

[
(comment)
(multiline_comment)
] @comment.inside

(comment)+ @comment.around

(multiline_comment) @comment.around
4 changes: 4 additions & 0 deletions runtime/queries/llvm/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
(array_vector_body) @class.inside) @class.around

(argument) @parameter.inside

(comment) @comment.inside

(comment)+ @comment.around
9 changes: 9 additions & 0 deletions runtime/queries/perl/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@

(argument
(_) @parameter.inside)

[
(comments)
(pod_statement)
] @comment.inside

(comments)+ @comment.around

(pod_statement) @comment.around
4 changes: 4 additions & 0 deletions runtime/queries/php/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
(variadic_parameter)
(property_promotion_parameter)
] @parameter.inside)

(comment) @comment.inside

(comment)+ @comment.around
4 changes: 4 additions & 0 deletions runtime/queries/python/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@

(argument_list
(_) @parameter.inside)

(comment) @comment.inside

(comment)+ @comment.around
7 changes: 7 additions & 0 deletions runtime/queries/rescript/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@
;----------

(function body: (_) @function.inside) @function.around

; Comments
;---------

(comment) @comment.inside

(comment)+ @comment.around
9 changes: 9 additions & 0 deletions runtime/queries/rust/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@

(arguments
(_) @parameter.inside)

[
(line_comment)
(block_comment)
] @comment.inside

(line_comment)+ @comment.around

(block_comment) @comment.around
9 changes: 9 additions & 0 deletions runtime/queries/tablegen/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
body: (_) @class.inside) @class.around

(_ argument: _ @parameter.inside)

[
(comment)
(multiline_comment)
] @comment.inside

(comment)+ @comment.around

(multiline_comment) @comment.around

0 comments on commit 9bfb0ca

Please sign in to comment.