Comment.nvim
Comment.nvim copied to clipboard
Smart block comments
I was thinking if it makes sense to remove the current block comment with gbc.
What I mean:
let name = /* som|ething */ else
-> (gbc) ->
let name = som|ething else
let name = |/* something */ else
-> (gbc) ->
/* let name = |/* something */ else */
Maybe another mapping would make more sense, but I think this feature is handy. What do you think?
You spolied my secret 😅. Anyway thanks for bringing this up.
This is very complicated to achieve now but this might be possible with treesitter which I am working on. But don't get your hopes high this might take a while 😉.
Sorry 😅 Just considering the current line it would be doable already. Scan left from the cursor, count /* and */. If count /* is larger scan to right and find the next */. With TreeSitter it would obviously be easier and wouldn't have false positives where the block comment is part of a string.
Scan left from the cursor, count /* and /. If count / is larger scan to right and find the next */.
I'll consider this 😄
With TreeSitter it would obviously be easier and wouldn't have false positives where the block comment is part of a string.
In fact, I am getting a hard time differentiating the b/w line comment and the subline block comment. Just from looking at the tree and querying from it, both seem to have the same characteristics like both are on the same line. To make this work I might also need the text of the node but we'll see.
Just for mention as a workaround, I've found that using a in-line motion or visual selection and doing a linewise (un)comment/toggle does the right thing (make a blockwise comment of that region/selection).
It can be used with https://github.com/glts/vim-textobj-comment (vimscript) for easy selection of the comment!
For example with the default mappings for both plugins, it looks like gcac (toggle comment (with motion), around comment)
I am using a proper implementation using https://github.com/nvim-treesitter/nvim-treesitter-textobjects but never shared it XD
require('nvim-treesitter.configs').setup({
-- nvim-treesitter/nvim-treesitter-textobjects
textobjects = {
select = {
enable = true,
keymaps = {
['uc'] = '@comment.outer', -- <-- This one does the magic
},
},
},
})
Keep in Mind:
- This will only work with
gc{char-motion},gb{motion}andgbci.e.,gciw,gb5jetc. to comment and to uncomment pressgbuc. - Also It is possible that there is no
@comment.outertext object for your language.
@numToStr that is awesome, feel like that should be in the README