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

Smart block comments

Open jandamm opened this issue 4 years ago • 6 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?

jandamm avatar Oct 16 '21 21:10 jandamm

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

numToStr avatar Oct 17 '21 06:10 numToStr

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.

jandamm avatar Oct 17 '21 07:10 jandamm

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.

numToStr avatar Oct 17 '21 07:10 numToStr

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)

bew avatar Aug 25 '22 06:08 bew

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:

  1. This will only work with gc{char-motion}, gb{motion} and gbc i.e., gciw, gb5j etc. to comment and to uncomment press gbuc.
  2. Also It is possible that there is no @comment.outer text object for your language.

numToStr avatar Aug 25 '22 08:08 numToStr

@numToStr that is awesome, feel like that should be in the README

taybart avatar Dec 15 '22 16:12 taybart