Open JL102 opened 11 months ago
I think you can just do something like this to set a default commentstring yourself?
vim.api.nvim_create_autocmd("FileType", {
pattern = { "*" },
callback = function(event)
vim.api.nvim_buf_set_option(event.buf, "commentstring", vim.api.nvim_buf_get_option(event.buf, "commentstring") or "# %s")
end,
group = vim.api.nvim_create_augroup("commentstring", { clear = true }),
})
I think you can just do something like this to set a default commentstring yourself?
vim.api.nvim_create_autocmd("FileType", { pattern = { "*" }, callback = function(event) vim.api.nvim_buf_set_option(event.buf, "commentstring", vim.api.nvim_buf_get_option(event.buf, "commentstring") or "# %s") end, group = vim.api.nvim_create_augroup("commentstring", { clear = true }), })
@Amzd Thanks for the suggestion! I just tried this, but it doesn't seem to be working quite as expected. I put it inside my init.lua
, and added a print statement:
vim.api.nvim_create_autocmd("FileType", {
pattern = { "*" },
callback = function(event)
print("Hello world, my commentstring is: " .. (vim.api.nvim_buf_get_option(event.buf, "commentstring") or "# %s"))
vim.api.nvim_buf_set_option(
event.buf,
"commentstring",
vim.api.nvim_buf_get_option(event.buf, "commentstring") or "# %s"
)
end,
group = vim.api.nvim_create_augroup("commentstring", { clear = true }),
})
and I get the "hello world" when loading files like .lua, .py, .js, etc. but if I have a plaintext file, or something like test.conf
or test.filetypeunknown
, the message doesn't print to the console. Could it be that the FileType event doesn't fire for files of an unknown type? Any idea if there's another way to accomplish this?
Hi,
I find it kind of annoying when I frequently encounter filetypes that are unknown to Comment.nvim, including plain text files without a set filetype, where it just says
[Comment.nvim] Invalid commentstring for ! Read :h commentstring\ for help.
The editor Micro just has a default comment string of
# %s
for unknown filetypes, and I think that's especially useful because often times configuration files don't have a specific extension like.conf
, and in that case, most often the comment string is#
.For my own use case, I'd really like to just set the default to
# %s
when the filetype is not known to Comment.nvim. But since this would be significantly changing the plugin's behavior, I'm not sure whether it's appropriate to set this as the default, or if it should be a configuration option, likerequire('Comment').setup({ default_comment_string = '# %s' })
. I'm happy to try and do the implementation myself and update the docs, but I figured I'd ask first to see what you think about which implementation is most appropriate for the way you want the plugin to behave (and if you're fine with the change in the first place).Thanks!