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

Willing to accept a PR if I add a setup option "default commentstring"? #416

Open
JL102 opened this issue Dec 5, 2023 · 2 comments
Open

Comments

@JL102
Copy link
Contributor

JL102 commented Dec 5, 2023

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, like require('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!

@Amzd
Copy link

Amzd commented Jan 31, 2024

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 }),
})

@JL102
Copy link
Contributor Author

JL102 commented Feb 1, 2024

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?

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

2 participants