nvim-treesitter / nvim-treesitter-textobjects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request: Add another query for elements?

unphased opened this issue · comments

Is your feature request related to a problem? Please describe.
I want to have swap functionality within more than just parameter lists. I want it on array elements and object constructor listings in all languages.

Describe the solution you'd like
I've already worked out how to set the treesitter queries for this for javascript. See below the highlights correspond to the query I just wrote for @element there on the top left:

image

I'm just not sure what the procedure is to introduce another semantic query to textobjects. I originally wanted to just extend @parameter.inner to also match these, but I figured that would probably not be the right way to go about it.

I realized probably this is more correct:

(object (pair) @element)

(array (_) @element)

You can put that in ~/.config/nvim/after/queries/javascript/textobjects.scm and make the file start with ;; extends

Thanks. I assume I will need to define these as @element.inner and also define @element.outer somehow. And I just don't know if there is anything else I need to add to make element a thing like parameter is.

You don't necessarily have to if you only care about swapping. I think if you just have inner it should work fine. And you can just name @element without inner if you want.
But if you want that functionality in select or move, you'd better define both inner and outer.

I wonder if its worth standardizing this and upstreaming/merging the queries from iswap.nvim into textobjects? I think its worth adding @list and @element as textobjects since those are pretty universal concepts and are the the kind of thing people will want to operate on often

The problem is the sheer number of textobjects that can be possibly defined. Note that you can use queries from other plugins as well. You can use iswap queries or find what you want from nvim-treesitter

In general how do you decide what textobjects should be in core and what should be the responsibility of other plugins or users?

I wasn't part of that decision before, but in general if it's too generic it cpuld be part of nvim-treesitter. Some languages select elements with @parameter.inner because they've been requested a lot.

FYI I have been using to good effect some portions of the STS plugin https://github.com/ziontee113/syntax-tree-surfer (which if you look inside is really just a pretty thin wrapper on top of basic treesitter API) -- Especially for swaps it works really well, the same bind can swap lines of code, elements in a list, classes in a namespace, etc.

I'm honestly still a bit undecided w.r.t. whether i prefer generic AST node manipulation or hotkeys to target specific types of AST nodes (e.g. f for function via this textobjects plugin, i do like to use that). I guess the answer is both. I would like to be able to do both.