helix icon indicating copy to clipboard operation
helix copied to clipboard

textobjects for html/xml tags

Open ff2400t opened this issue 3 years ago • 6 comments

I bundled the html and jsx queries with this pr to show how it work in practice

mae - will select the element and all it's child element, this is can also be used to select self closing element like we see in jsx mie - will select the all the child nodes of an element

ff2400t avatar Aug 01 '22 12:08 ff2400t

Please do not change the behavior of t as it's probably part of muscle memory for multiple people already (myself included).

If e is available, maybe use that instead as element?

AlexanderBrevig avatar Aug 01 '22 13:08 AlexanderBrevig

Yeah I don't see a compelling reason for a breaking change here: more languages have queryable test conventions than XML tags. We could use T for xml tags?

the-mikedavis avatar Aug 01 '22 13:08 the-mikedavis

There are also the navigation commands ]e/[e to add: https://github.com/helix-editor/helix/blob/0c08ff1596b398d53c8c48cc96cfbd24ddec303b/helix-term/src/keymap/default.rs#L100-L121

the-mikedavis avatar Aug 01 '22 20:08 the-mikedavis

Thanks so much for this PR!

I tested the changes (rebased the current main) and it works reliably for around-queries.

For inside-queries it works when the cursor is already on the content nested in a tag. When the cursor is resting on an html attribute such as class="sr-only", mie selects nothing. When the cursor rests on the tag name of a closing tag, mie selects the tag name itself.

I would expect the element the cursor is in to be the context the selection is applied to:

<section>
  <p [CURSOR] class="sr-only"> text </p>
</section>

mie selects text mae selects <p class="sr-only"> text </p>

Currently only mae works that way

Ansimorph avatar Apr 12 '23 17:04 Ansimorph

The mei queries still don't work as I want them to.

<section>
  <p [CURSOR] class="sr-only"> text </p>
</section>

It would be ideal in the above case for mei to select the text inside the paragraph element. But for now it selects the whole paragraph element. Hopefully someone who understand treesitter queries can help in that.

Furthermore, i think it would be great if the Text selection is trimmed of whitespace by default but that should be part of another PR

ff2400t avatar Apr 26 '23 05:04 ff2400t

So in the end i had to modify how textobjects are captured from rust to capture the element.inner as intended. This is also how nvim-treesitter-textobjects does the capture for these too. Hopefully some with better knowledge of rust can help me remove the extra QueryCursor allocation.

ff2400t avatar May 01 '23 04:05 ff2400t

closing this one out as stale, thank you for contributing!

pascalkuthe avatar Apr 13 '24 23:04 pascalkuthe