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

Can't use // line comments in C

Open realh opened this issue 1 year ago • 12 comments

I want to use // for my C comments, but the plugin seems to be hardwired to use /* */ for C. This config has no effect:

local ft = require('Comment.ft')

ft.c = {'//%s', '//%s'}

realh avatar Oct 24 '24 14:10 realh

should use get and set instead

require('Comment.ft').set('c', {'//%s', '//%s'})

And I don't think this make any sense? the second element is for block commenting, you can never use // to do that because that uses a different string format. Simply use gc to do line commenting for each line you selected.

sharpchen avatar Oct 28 '24 18:10 sharpchen

I got it working with this:

vim.api.nvim_create_autocmd('Filetype', {
  pattern = 'c',
  callback = function()
		vim.bo.commentstring = '//%s'
  end,
  group = comment_augroup
})

Does Comment.ft get ignored for filetypes which have a commentstring?

realh avatar Oct 28 '24 20:10 realh

vim.bo.commentstring is the last resort, pre_hook and ft has higher priority

https://github.com/numToStr/Comment.nvim/blob/e30b7f2008e52442154b66f7c519bfd2f1e32acb/lua/Comment/utils.lua#L173-L185

sharpchen avatar Oct 29 '24 06:10 sharpchen

ft.set works, thanks. I thought I could assign to ft.c because of the examples for javascript and yaml in README.md, where it says "Metatable magic". Are those examples wrong? If not, why does that syntax work for yaml and javascript, but not c?

realh avatar Oct 29 '24 12:10 realh

I never noticed that usage, it actually should work since it uses set too. https://github.com/numToStr/Comment.nvim/blob/e30b7f2008e52442154b66f7c519bfd2f1e32acb/lua/Comment/ft.lua#L310-L314 But it's set only, you can't get the value with the same form.

sharpchen avatar Oct 29 '24 14:10 sharpchen

you can use a plug: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/wiki/Integrations#commentnvim

A-caibird avatar Nov 21 '24 09:11 A-caibird

Hi,

I'm quite new to nvim. And I have a similar issue with cpp files. They do not work.

If I understand this code correctly cpp should be supported -> https://github.com/numToStr/Comment.nvim/blob/master/lua/Comment/ft.lua#L58

adding https://github.com/numToStr/Comment.nvim/issues/489#issuecomment-2442532050 kind of works but the block comments still do not work. So is this a plugin bug or did I not configure something correctly?

git-dimiz avatar Dec 19 '24 09:12 git-dimiz

@git-dimiz can you provide more details of how you do block commenting? I don't quite understand what is do not work. From what I can tell, you can test by following step:

  • := require('Comment.ft').get('c') should return { '//%s', '/*%s*/' }(just in case it's been modified somewhere else)
  • Ensure treesitter parser for cpp is installed(this is only useful for context-aware commenting, such as cpp code block in markdown file.)
  • Neovim has its own commenting util since certain release few months ago which might conflict with this plugin; I'm not sure whether does it cause the problem, but it's worth to try:
    -- run this before plugin load
    vim.keymap.del({ 'n', 'x', 'o' }, 'gc')
    vim.keymap.del('n', 'gcc')
    

Edit: I see, the dafault vim.bo.commentstring for both c and cpp in nvim --clean is /*%s*/. Do you want to comment as //%s with gc but result in /*%s*/? If that's the case, I think it's a conflict with builtin commenting util in neovim.

sharpchen avatar Dec 19 '24 10:12 sharpchen

So, by not working I refer to the same problem that @realh mentioned but for C++ files. it always is using /*%s*/ comments if i type gcc even tough I it configured to use //%s.

If I execute: :lua vim.print(require('Comment.ft').get('cpp')) it shows { "//%s", "/*%s*/" } so it seems to be correct?

Deleting the keymaps causes some strange behavior where it delets the line.

I've added cpp to the ensure_installed field in treesitter. Seems to not change the result.

git-dimiz avatar Dec 19 '24 11:12 git-dimiz

Deleting the keymaps causes some strange behavior where it delets the line.

Make sure they're deleted before comment.nvim loads, in your case they seems to be removed completely as well as keymaps from comment.nvim. And I forgot to mention that you can check the source of keymap by :map gc and :map gcc. If they're from vim/_defaults.lua, they're builtin.

sharpchen avatar Dec 19 '24 11:12 sharpchen

you can check the source of keymap by :map gc and :map gcc. If they're from vim/_defaults.lua, they're builtin.

Ahh! Thanks! So there I see that it is using the default vim bindings and not the ones from the plugin. Apparently I've used this plugin wrong in combination with lazy plugin manager. In the README of this project it just states:

-- add this to your lua/plugins.lua, lua/plugins/init.lua,  or the file you keep your other plugins:
{
    'numToStr/Comment.nvim',
    opts = {
        -- add any options here
    }
}

I thought this is sufficient:

{
    'numToStr/Comment.nvim',
}

And it would use some default values. But I had to populate the opts field with values. Now it is working properly.

git-dimiz avatar Dec 19 '24 12:12 git-dimiz

Congrats :)

The reason is as the readme states:

First you need to call the setup() method to create the default mappings.

opts will be passed to setup behind the scene in lazy. If you don't specify opts, then you should manually call require('Comment').setup() at config or somewhere else.

sharpchen avatar Dec 19 '24 12:12 sharpchen