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 · 6 comments
Open

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

realh opened this issue Oct 24, 2024 · 6 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.

@A-caibird
Copy link

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

3 participants