nvim-treesitter-textobjects icon indicating copy to clipboard operation
nvim-treesitter-textobjects copied to clipboard

Add documentation.inner / documentation.outer as text objects

Open ColinKennedy opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe. I'd like to be able to use did and dad to delete the inner and outer docstring of various languages.

Now that treesitter supports documentation capture groups it'd be great if nvim-treesitter-textobjects could follow suit.

Describe the solution you'd like I have a fork of this repo where I've implemented one for Python. I can contribute that as a PR, if you're up for it.

Describe alternatives you've considered None.

Additional context Here's a GIF showing the fork I've been using so far. Seems to be working.

dad dad_text_object

did did_text_object

Using configuration

                    select = {
                        enable = true,

                        -- Automatically jump forward to textobj, similar to targets.vim
                        lookahead = true,

                        -- ...

                        keymaps = {
                            ["ad"] = {
                                desc = "Select around an entire docstring",
                                query = "@documentation.outer",
                            },
                            ["id"] = {
                                desc = "Select the inside of a docstring",
                                query = "@documentation.inner",
                            },

                        -- ...

ColinKennedy avatar Apr 12 '23 05:04 ColinKennedy

If the query is already defined, why not just use that?

["ad"] = {
  desc = "Select around an entire docstring",
  query = "@string.documentation",
  query_group = "highlights",
},

I believe this should work. Unfortunately, it's only for the outer, but this supports various languages.

kiyoon avatar Apr 12 '23 14:04 kiyoon

Oh, nice. I'm new to nvim-treesitter-textobjects so I just missed query_group in the docs. I can give it a try later, @string.documentation will probably work for ad. I do want a @string.documentation.inner for my own benefit but I can just maintain that in my fork if others aren't interested.

My impression originally was that https://github.com/nvim-treesitter/nvim-treesitter-textobjects/#built-in-textobjects is 100% of the text objects that are supported for various languages but it sounds like that list is actually in addition to the categories that query_group="highlights" / query_group="folds" / query_group="locals" provides. Is that right?

ColinKennedy avatar Apr 12 '23 15:04 ColinKennedy

Let's see how many people find it (inner) interesting.

Yes, you can use any queries from any plugins. So you can use queries defined in nvim-treesitter which are locals etc. You can also install any plugin like aerial.nvim and give a query group "aerial". This is a new feature to select from another group, by the way.

You can create a plugin (github repo) and only add after/queries/python/textobjects.scm and maintain @documentation.inner there. Just by installing your plugin, you'll be able to use the textobjects directly without further configuring. I mean, you shouldn't need to fork this plugin entirely to achieve this.

kiyoon avatar Apr 12 '23 15:04 kiyoon

Interesting, I'll re-read the documentation. It sounds like I don't have a full grasp of what's possible yet but you've given me some better methods of approach to use. Thank you!

ColinKennedy avatar Apr 12 '23 17:04 ColinKennedy