Comment.nvim icon indicating copy to clipboard operation
Comment.nvim copied to clipboard

[Feature request] Text object for comments

Open ouuan opened this issue 2 years ago • 18 comments

Basically the same as https://github.com/tomtom/tcomment_vim/issues/237

ouuan avatar Oct 08 '21 11:10 ouuan

This is interesting and also useful. I probably go with the treesitter to implement this :)

numToStr avatar Oct 10 '21 05:10 numToStr

I made some progress on treesitter but now I am stuck. And I need ideas. Consider the following comment regions, they all are separated by empty lines. Should I treat them as one or three different regions?

-- Hello treesitter
-- a bunch of comment

-- stack together
-- so we can catch
-- them at once

--[[
    One block comment
--]]

@gegoune @astier I need your brain :)

numToStr avatar Oct 16 '21 13:10 numToStr

I made some progress on treesitter but now I am stuck. And I need ideas. Consider the following comment regions, they all are separated by empty lines. Should I treat them as one or three different regions?

-- Hello treesitter
-- a bunch of comment

-- stack together
-- so we can catch
-- them at once

--[[
    One block comment
--]]

We need gcgc to revert a gc. So if gc doesn't add -- for empty lines, they should be treated as a single region.

ouuan avatar Oct 16 '21 13:10 ouuan

gc does adds -- to empty lines by default. So by that, it means 1) this will be three different regions

-- Hello treesitter
-- a bunch of comment

-- stack together
-- so we can catch
-- them at once

--[[
    One block comment
--]]

and 2) this will be a single region

-- Hello treesitter
-- a bunch of comment
--
-- stack together
-- so we can catch
-- them at once
--
--[[
    One block comment
--]]

Regarding blockwise comments, I am considering all of them as separate regions. Even if they are attached. In the following, they will be treated as 3 different regions.

--[[
    1 block comment
--]]
--[[
    2 block comment
--]]

--[[
    3 block comment
--]]

The only exception is when a block comment starts immediately after a linewise comment region then it will be treated as a part of the line comment region. Just like 2) example of line comment above.

numToStr avatar Oct 16 '21 14:10 numToStr

I think this should be three regions. I agree with the examples you made.

astier avatar Oct 17 '21 14:10 astier

https://github.com/RRethy/nvim-treesitter-textsubjects has single and multiline comment support in the textsubjects-smart query, another query could be added specifically for comments.

RRethy avatar Jan 27 '22 18:01 RRethy

@RRethy If that is possible then it would be great ;)

numToStr avatar Jan 27 '22 18:01 numToStr

I just discovered that https://github.com/nvim-treesitter/nvim-treesitter-textobjects also have @comment.outer.

(Also mentioned at https://github.com/numToStr/Comment.nvim/issues/77#issuecomment-974798712)

ouuan avatar Jan 30 '22 12:01 ouuan

I just discovered that nvim-treesitter/nvim-treesitter-textobjects also have @comment.outer.

(Also mentioned at #77 (comment))

Unfortunately, it doesn't work with line comments very well:

  1. @comment.outer only matches a single line when there are multiple line comments in a row.
  2. @comment.outer matches exactly the characters within the comment instead of the whole line, so Comment.nvim will turn a line comment into the content of a block comment. e.g. it turns // comment to /* // comment */.

ouuan avatar Jan 30 '22 12:01 ouuan

@ouuan Yes, it doesn't work with line comments. Although it works very well with block comments.

Regarding the feature itself, I am still confused as to whether should I implement this myself or let the other plugins like https://github.com/nvim-treesitter/nvim-treesitter-textobjects or https://github.com/RRethy/nvim-treesitter-textsubjects provide the text objects. If any of the plugins can achieve this https://github.com/numToStr/Comment.nvim/issues/11#issuecomment-944922532 behavior then I would recommend using that plugin.

numToStr avatar Jan 30 '22 12:01 numToStr

I agree that Comment.nvim doesn't need to implement this feature. It could be a part of another plugin or a new plugin.

ouuan avatar Jan 30 '22 13:01 ouuan

There's also a problem with multiline block comments.

Comment.nvim adds // at the beginning of each line:

/**
 * multiline comment
 */

ouuan avatar Jan 30 '22 13:01 ouuan

I think you are using the gc bindings. Try with gb.

numToStr avatar Jan 30 '22 13:01 numToStr

@ouuan Also make sure that the plugin contains queries for comments. The last time I checked https://github.com/nvim-treesitter/nvim-treesitter-textobjects was missing comments queries for some languages.

numToStr avatar Jan 30 '22 13:01 numToStr

Yes, and help is welcome :)

clason avatar Jan 30 '22 13:01 clason

There's a bug in tree-sitter queries which is blocking on this, you can see it with this query:

(((_) @head . (comment) @_start . (comment)+ @_end (_) @tail)
    (#not-has-type? @tail "comment")
    (#not-has-type? @head "comment")
    (#make-range! "range" @_start @_end))

It will find a match in this file:

def foo
  # foo
  # foo
  # foo
  puts 'hello'
end

But it will fail to find a match in this file with more comments, even though there should be a match.

def foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  # foo
  puts 'hello'
end

I haven't filed anything yet, just been busy.

Edit: I came across this when looking to implement a textsubjects-comment query, but stopped after encountering this.

RRethy avatar Jan 31 '22 00:01 RRethy

Temporary solution for linewise comments: https://github.com/numToStr/Comment.nvim/issues/22#issuecomment-1272569139

xeluxee avatar Oct 09 '22 15:10 xeluxee

mini.comment already has this, perhaps inspirations can be drawn there? I'm not aware of the architecture but this would be really helpful, since as of now I have to choose between block comments or comment text objects, or make some Chimera of the two, none of which is ideal.

Ciel-MC avatar Nov 06 '23 10:11 Ciel-MC