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

Can't use // line comments in C #489

Open
realh opened this issue Oct 24, 2024 · 12 comments
Open

Can't use // line comments in C #489

realh opened this issue Oct 24, 2024 · 12 comments

Comments

@realh
Copy link

realh commented Oct 24, 2024

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'}
@sharpchen
Copy link

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.

@realh
Copy link
Author

realh commented Oct 28, 2024

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?

@sharpchen
Copy link

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

function U.parse_cstr(cfg, ctx)
-- 1. We ask `pre_hook` for a commentstring
local inbuilt = U.is_fn(cfg.pre_hook, ctx)
-- 2. Calculate w/ the help of treesitter
or F.calculate(ctx)
assert(inbuilt or (ctx.ctype ~= U.ctype.blockwise), {
msg = vim.bo.filetype .. " doesn't support block comments!",
})
-- 3. Last resort to use native commentstring
return U.unwrap_cstr(inbuilt or vim.bo.commentstring)
end

@realh
Copy link
Author

realh commented Oct 29, 2024

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?

@sharpchen
Copy link

I never noticed that usage, it actually should work since it uses set too.

---@export ft
return setmetatable(ft, {
__newindex = function(this, k, v)
this.set(k, v)
end,

But it's set only, you can't get the value with the same form.

@git-dimiz
Copy link

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 #489 (comment) kind of works but the block comments still do not work. So is this a plugin bug or did I not configure something correctly?

@sharpchen
Copy link

sharpchen commented Dec 19, 2024

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

@git-dimiz
Copy link

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.

@sharpchen
Copy link

sharpchen commented Dec 19, 2024

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.

@git-dimiz
Copy link

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.

@sharpchen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants