JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.13k stars 34 forks source link

This plugin doesn't set the `comments` var. #81

Open rwblokzijl opened 10 months ago

rwblokzijl commented 10 months ago

From :h comments

                                          'comments' 'com' E524 E525
  'comments' 'com'        string  (default
                                  "s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-")
                          local to buffer
          A comma-separated list of strings that can start a comment line.  See
          format-comments.  See option-backslash about using backslashes to
          insert a space.

                                          'commentstring' 'cms' E537
  'commentstring' 'cms'   string  (default "")
                          local to buffer
          A template for a comment.  The "%s" in the value is replaced with the
          comment text.  Currently only used to add markers for folding, see
          fold-marker.

This plugin should also set the comments var. That way OG plugins like https://github.com/glts/vim-textobj-comment will also work.

JoosepAlviste commented 9 months ago

Hey! I also considered that at some point, but didn't really use any of the comments features, so decided against it (didn't want to bother with it 😅).

I wouldn't mind supporting it, though, as comments is heavily related to commentstring. Do you have any ideas on how this would work and how it would be configured? Maybe we could somehow automatically guess the comments value based on the commentstring?

rwblokzijl commented 9 months ago

Had chatgpt do some research for me, and came up with the following function:

function get_comments_for_filetype(ft)
    -- Create a hidden buffer in the background
    local temp_buf = vim.api.nvim_create_buf(true, true)

    -- Set the desired filetype in the temporary buffer
    vim.api.nvim_buf_set_option(temp_buf, 'filetype', ft)

    -- Get the comments var from the temporary buffer
    local comments = vim.api.nvim_buf_get_option(temp_buf, 'comments')

    -- Delete the temporary buffer
    vim.api.nvim_buf_delete(temp_buf, { force = true })

    return comments
end

You could also do something similar to this for the commentstring to avoid having to support them manually.

However if you're married to the idea of supporting them manually, you can find all commentstring and comments vars in the neovim source code: https://github.com/neovim/neovim/tree/master/runtime/ftplugin