Comment.nvim
Comment.nvim copied to clipboard
[Feature request] Text object for comments
Basically the same as https://github.com/tomtom/tcomment_vim/issues/237
This is interesting and also useful. I probably go with the treesitter to implement this :)
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 :)
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.
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.
I think this should be three regions. I agree with the examples you made.
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 If that is possible then it would be great ;)
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)
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:
-
@comment.outer
only matches a single line when there are multiple line comments in a row. -
@comment.outer
matches exactly the characters within the comment instead of the whole line, soComment.nvim
will turn a line comment into the content of a block comment. e.g. it turns// comment
to/* // comment */
.
@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.
I agree that Comment.nvim doesn't need to implement this feature. It could be a part of another plugin or a new plugin.
There's also a problem with multiline block comments.
Comment.nvim
adds //
at the beginning of each line:
/**
* multiline comment
*/
I think you are using the gc
bindings. Try with gb
.
@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.
Yes, and help is welcome :)
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.
Temporary solution for linewise comments: https://github.com/numToStr/Comment.nvim/issues/22#issuecomment-1272569139
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.