Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Text object for comments #11

Open
ouuan opened this issue Oct 8, 2021 · 18 comments
Open

[Feature request] Text object for comments #11

ouuan opened this issue Oct 8, 2021 · 18 comments
Labels
feature New feature help wanted Extra attention is needed

Comments

@ouuan
Copy link

ouuan commented Oct 8, 2021

Basically the same as tomtom/tcomment_vim#237

@numToStr numToStr added feature New feature help wanted Extra attention is needed labels Oct 9, 2021
@numToStr
Copy link
Owner

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

@numToStr
Copy link
Owner

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 :)

@ouuan
Copy link
Author

ouuan commented Oct 16, 2021

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.

@numToStr
Copy link
Owner

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.

@astier
Copy link

astier commented Oct 17, 2021

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

@RRethy
Copy link

RRethy commented Jan 27, 2022

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.

@numToStr
Copy link
Owner

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

@ouuan
Copy link
Author

ouuan commented Jan 30, 2022

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

(Also mentioned at #77 (comment))

@ouuan
Copy link
Author

ouuan commented Jan 30, 2022

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 */.

@numToStr
Copy link
Owner

@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 #11 (comment) behavior then I would recommend using that plugin.

@ouuan
Copy link
Author

ouuan commented Jan 30, 2022

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
Copy link
Author

ouuan commented Jan 30, 2022

There's also a problem with multiline block comments.

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

/**
 * multiline comment
 */

@numToStr
Copy link
Owner

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

@numToStr
Copy link
Owner

@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.

@clason
Copy link

clason commented Jan 30, 2022

Yes, and help is welcome :)

@RRethy
Copy link

RRethy commented Jan 31, 2022

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.

@xeluxee
Copy link

xeluxee commented Oct 9, 2022

Temporary solution for linewise comments: #22 (comment)

@Ciel-MC
Copy link

Ciel-MC commented Nov 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants