nvim-treesitter-textobjects
nvim-treesitter-textobjects copied to clipboard
Make @comment.outer span multiline comment
Is your feature request related to a problem? Please describe.
Code comments are usually hard-wrapped, so I use gw / gq to reflow long multiline comments when editing them. Unfortunately, when these are not separated by an empty line from preceding and following code, then the ip (inner paragraph) text object includes that as well, so gwip / gqip reflows even the code (which is unwanted and very probably wrong). So you have to resort to first visually selecting the comment and then gw / gq.
Describe the solution you'd like
I'd love to use treesitter + @comment.outer to invoke gw / gq over the appropriate range of text. However, @comment.outer currently doesn't span multiline comments, it only extends to the current line. Would it be realistic to redefine it, so that e.g. gwac (if ac is configured as the @comment.outer text object) reflows the entire multiline comment?
Describe alternatives you've considered
If redefining @comment.outer is doable but not desirable for backwards compatibility reasons, then maybe a new textobject could be provided, something like @multiline_comment.outer?
Additional context I'm specifically interesting in Python, but once a solution exists for one language, it should be fairly easy to extend it to other ones with similar syntax?
Bump for feature request, would help a lot with long doc strings that need to be copied elsewhere.
For python, I have a solution I use for folding, however it runs into an issue #154
Given a comment like this:
# Reverses the contents of a String or IO object. (1)
#
# @param [String, #read] contents the contents to reverse (3)
# @return [String] the contents reversed lexically (4)
def reverse(contents)
end
For @mutline_comment.inner:
- Cursor on line 3 or 4 selects both lines
- Cursor on line 2 selects line 2
- Cursor on line 1 selects only line 1
For @multine_comment.outer:
- Everything is selected.
For @comment.outer:
- Select entire comment for line cursor is on, including the comment character.
For @comment.inner:
- Selects only the commented text for the given line.
That said, there is some stuff like ruby's =begin and =end that create multline comments but have no special marker per line like C's /* and */ markers. I think the semantics still make sense but with some overlap.
All said, I don't know that @comment.inner and @comment.outer are still useful since we can already operate on a line reasonably easily already with f# and F# which is useful even when there is code on a line with a comment.
This would be very useful, has anyone tried it?
I did some research into an implementation for this and came across a shortcoming; most parsers don't support nodes for the content of a comment. It's not possible to detect whether or not a comment is empty.
Luckily, all languages have the capability of a comment content node, they just need to be implemented. For the languages that do support it, we have a few options of how to go about it:
What @liaden suggested:
- I'm not a huge fan of this design, I feel that it introduces some redundancy. Do you ever really need to select a single comment rather than its whole chain? I know the primary reason for this is for backward compatibility, but maybe
@comment.outershould be deprecated, or perhaps extended?
Add @comment_content.outer, @comment_content.inner, @comment.outer, and @comment.inner:
@comment_contentdeals with the contents of a comment (excluding prefix). Theinnerwill select a chain of comments until an empty one, while theouterwill select until the end of the comment chain. The normal@commentwill replicate that behavior, but include comment prefixes.
Add @comment_block.outer, @comment_block.inner, @comment.outer, and @comment.inner:
@comment_blockwill select a chain of comments until a blank one. Theouterincludes whitespace, while theinnerexcludes it. The normal@commentdeals with the entire chain of comments whilst replicating the behavior mentioned above.
To preserve backwards compatibility, the @comment tag could be deprecated and the suggested tag names could be changed to something more specific.
Hi, Just recently joined the Neovim community and found this useful plugins. Want to echo the point that if one comment.outer only support single line, wouldn't <S-v> be sufficient?
@wilsonchen719 Latest nightly version of Neovim has builtin support for comment objects, the mapping for this text object is gc, so you can do dgc for example to delete the comment block.
The mapping for the comment/uncomment action is also gc, so you can gcgc to uncomment a comment block.
It was announced here
And documented here