Skip to content

Commit b300fb9

Browse files
Ansimorphjdrst
authored andcommitted
Add textobjects for XML, HTML and JSX (helix-editor#11158)
1 parent f4f9d97 commit b300fb9

File tree

10 files changed

+68
-26
lines changed

10 files changed

+68
-26
lines changed

book/src/generated/lang-support.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
| hocon |||| |
105105
| hoon || | | |
106106
| hosts || | | |
107-
| html || | | `vscode-html-language-server`, `superhtml` |
107+
| html || | | `vscode-html-language-server`, `superhtml` |
108108
| htmldjango || | | `djlsp`, `vscode-html-language-server`, `superhtml` |
109109
| hurl |||| |
110110
| hyprlang || || `hyprls` |
@@ -274,7 +274,7 @@
274274
| wit || || |
275275
| wren |||| |
276276
| xit || | | |
277-
| xml || || |
277+
| xml || || |
278278
| xtc || | | |
279279
| yaml |||| `yaml-language-server`, `ansible-language-server` |
280280
| yara || | | `yls` |

book/src/generated/static-cmd.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@
267267
| `goto_prev_comment` | Goto previous comment | normal: `` [c ``, select: `` [c `` |
268268
| `goto_next_test` | Goto next test | normal: `` ]T ``, select: `` ]T `` |
269269
| `goto_prev_test` | Goto previous test | normal: `` [T ``, select: `` [T `` |
270+
| `goto_next_xml_element` | Goto next (X)HTML element | normal: `` ]x ``, select: `` ]x `` |
271+
| `goto_prev_xml_element` | Goto previous (X)HTML element | normal: `` [x ``, select: `` [x `` |
270272
| `goto_next_entry` | Goto next pairing | normal: `` ]e ``, select: `` ]e `` |
271273
| `goto_prev_entry` | Goto previous pairing | normal: `` [e ``, select: `` [e `` |
272274
| `goto_next_paragraph` | Goto next paragraph | normal: `` ]p ``, select: `` ]p `` |

book/src/guides/textobject.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The following [captures][tree-sitter-captures] are recognized:
2828
| `comment.around` |
2929
| `entry.inside` |
3030
| `entry.around` |
31+
| `xml-element.inside` |
32+
| `xml-element.around` |
3133

3234
[Example query files][textobject-examples] can be found in the helix GitHub repository.
3335

book/src/keymap.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,32 @@ Displays the signature of the selected completion item. Remapping currently not
348348

349349
These mappings are in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaired).
350350

351-
| Key | Description | Command |
352-
| ----- | ----------- | ------- |
353-
| `]d` | Go to next diagnostic (**LSP**) | `goto_next_diag` |
354-
| `[d` | Go to previous diagnostic (**LSP**) | `goto_prev_diag` |
355-
| `]D` | Go to last diagnostic in document (**LSP**) | `goto_last_diag` |
356-
| `[D` | Go to first diagnostic in document (**LSP**) | `goto_first_diag` |
357-
| `]f` | Go to next function (**TS**) | `goto_next_function` |
358-
| `[f` | Go to previous function (**TS**) | `goto_prev_function` |
359-
| `]t` | Go to next type definition (**TS**) | `goto_next_class` |
360-
| `[t` | Go to previous type definition (**TS**) | `goto_prev_class` |
361-
| `]a` | Go to next argument/parameter (**TS**) | `goto_next_parameter` |
362-
| `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` |
363-
| `]c` | Go to next comment (**TS**) | `goto_next_comment` |
364-
| `[c` | Go to previous comment (**TS**) | `goto_prev_comment` |
365-
| `]T` | Go to next test (**TS**) | `goto_next_test` |
366-
| `[T` | Go to previous test (**TS**) | `goto_prev_test` |
367-
| `]p` | Go to next paragraph | `goto_next_paragraph` |
368-
| `[p` | Go to previous paragraph | `goto_prev_paragraph` |
369-
| `]g` | Go to next change | `goto_next_change` |
370-
| `[g` | Go to previous change | `goto_prev_change` |
371-
| `]G` | Go to last change | `goto_last_change` |
372-
| `[G` | Go to first change | `goto_first_change` |
373-
| `]Space` | Add newline below | `add_newline_below` |
374-
| `[Space` | Add newline above | `add_newline_above` |
351+
| Key | Description | Command |
352+
| ----- | ----------- | ------- |
353+
| `]d` | Go to next diagnostic (**LSP**) | `goto_next_diag` |
354+
| `[d` | Go to previous diagnostic (**LSP**) | `goto_prev_diag` |
355+
| `]D` | Go to last diagnostic in document (**LSP**) | `goto_last_diag` |
356+
| `[D` | Go to first diagnostic in document (**LSP**) | `goto_first_diag` |
357+
| `]f` | Go to next function (**TS**) | `goto_next_function` |
358+
| `[f` | Go to previous function (**TS**) | `goto_prev_function` |
359+
| `]t` | Go to next type definition (**TS**) | `goto_next_class` |
360+
| `[t` | Go to previous type definition (**TS**) | `goto_prev_class` |
361+
| `]a` | Go to next argument/parameter (**TS**) | `goto_next_parameter` |
362+
| `[a` | Go to previous argument/parameter (**TS**) | `goto_prev_parameter` |
363+
| `]c` | Go to next comment (**TS**) | `goto_next_comment` |
364+
| `[c` | Go to previous comment (**TS**) | `goto_prev_comment` |
365+
| `]T` | Go to next test (**TS**) | `goto_next_test` |
366+
| `[T` | Go to previous test (**TS**) | `goto_prev_test` |
367+
| `]p` | Go to next paragraph | `goto_next_paragraph` |
368+
| `[p` | Go to previous paragraph | `goto_prev_paragraph` |
369+
| `]g` | Go to next change | `goto_next_change` |
370+
| `[g` | Go to previous change | `goto_prev_change` |
371+
| `]G` | Go to last change | `goto_last_change` |
372+
| `[G` | Go to first change | `goto_first_change` |
373+
| `[x` | Go to next (X)HTML element | `goto_next_xml_element` |
374+
| `]x` | Go to previous (X)HTML element | `goto_prev_xml_element` |
375+
| `]Space` | Add newline below | `add_newline_below` |
376+
| `[Space` | Add newline above | `add_newline_above` |
375377

376378
## Insert mode
377379

book/src/textobjects.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function or block of code.
2424
| `c` | Comment |
2525
| `T` | Test |
2626
| `g` | Change |
27+
| `x` | (X)HTML element |
2728

2829
> 💡 `f`, `t`, etc. need a tree-sitter grammar active for the current
2930
document and a special tree-sitter query file to work properly. [Only

helix-term/src/commands.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ impl MappableCommand {
565565
goto_prev_comment, "Goto previous comment",
566566
goto_next_test, "Goto next test",
567567
goto_prev_test, "Goto previous test",
568+
goto_next_xml_element, "Goto next (X)HTML element",
569+
goto_prev_xml_element, "Goto previous (X)HTML element",
568570
goto_next_entry, "Goto next pairing",
569571
goto_prev_entry, "Goto previous pairing",
570572
goto_next_paragraph, "Goto next paragraph",
@@ -5882,6 +5884,14 @@ fn goto_prev_test(cx: &mut Context) {
58825884
goto_ts_object_impl(cx, "test", Direction::Backward)
58835885
}
58845886

5887+
fn goto_next_xml_element(cx: &mut Context) {
5888+
goto_ts_object_impl(cx, "xml-element", Direction::Forward)
5889+
}
5890+
5891+
fn goto_prev_xml_element(cx: &mut Context) {
5892+
goto_ts_object_impl(cx, "xml-element", Direction::Backward)
5893+
}
5894+
58855895
fn goto_next_entry(cx: &mut Context) {
58865896
goto_ts_object_impl(cx, "entry", Direction::Forward)
58875897
}
@@ -5949,6 +5959,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
59495959
'c' => textobject_treesitter("comment", range),
59505960
'T' => textobject_treesitter("test", range),
59515961
'e' => textobject_treesitter("entry", range),
5962+
'x' => textobject_treesitter("xml-element", range),
59525963
'p' => textobject::textobject_paragraph(text, range, objtype, count),
59535964
'm' => textobject::textobject_pair_surround_closest(
59545965
doc.syntax(),
@@ -5993,6 +6004,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
59936004
("e", "Data structure entry (tree-sitter)"),
59946005
("m", "Closest surrounding pair (tree-sitter)"),
59956006
("g", "Change"),
6007+
("x", "X(HTML) element (tree-sitter)"),
59966008
(" ", "... or any character acting as a pair"),
59976009
];
59986010

helix-term/src/keymap/default.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
120120
"e" => goto_prev_entry,
121121
"T" => goto_prev_test,
122122
"p" => goto_prev_paragraph,
123+
"x" => goto_prev_xml_element,
123124
"space" => add_newline_above,
124125
},
125126
"]" => { "Right bracket"
@@ -134,6 +135,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
134135
"e" => goto_next_entry,
135136
"T" => goto_next_test,
136137
"p" => goto_next_paragraph,
138+
"x" => goto_next_xml_element,
137139
"space" => add_newline_below,
138140
},
139141

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; See runtime/queries/ecma/README.md for more info.
2+
3+
(jsx_self_closing_element) @xml-element.around @xml-element.inside
4+
5+
(jsx_element (jsx_opening_element) (_)* @xml-element.inside (jsx_closing_element))
6+
7+
(jsx_element) @xml-element.around
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(script_element (start_tag) (_) @xml-element.inside (end_tag)) @xml-element.around
2+
3+
(style_element (start_tag) (_) @xml-element.inside (end_tag)) @xml-element.around
4+
5+
(element (start_tag) (_)* @xml-element.inside (end_tag))
6+
7+
(element) @xml-element.around
8+
9+
(comment) @comment.around
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(element (start_tag) (_)* @xml-element.inside (end_tag))
2+
3+
(element) @xml-element.around
4+
5+
(comment) @comment.around

0 commit comments

Comments
 (0)