JoosepAlviste / nvim-ts-context-commentstring

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

Svelte commenting not working #16

Closed gitneeraj closed 3 years ago

gitneeraj commented 3 years ago

Hey,

I know this plugin as built-in support for Svelte files but for some reason it is not using the right comment string for me on my svelte project. I am using 'b3nj5m1n/kommentary' plugin for commenting along. I have a config for JSX and it works fine though. Not sure what I am doing wrong as I could not find much info online on solving my issue so raising this issue here. svelte-comment-error

Here is my config - Treesitter - https://github.com/gitneeraj/dotfiles/blob/develop/nvim.lua/lua/modules/treesitter/init.lua kommentary - https://github.com/gitneeraj/dotfiles/blob/develop/nvim.lua/lua/modules/commentString/init.lua

Any help much appreciated!

Cheers & Peace!

JoosepAlviste commented 3 years ago

Hey @gitneeraj!

It looks like your kommentary hook_function is only configured for javascriptreact:

require('kommentary.config').configure_language('javascriptreact', {

Could you try adding another configure_language call for svelte? Something like this:

require('kommentary.config').configure_language('svelte', {
    hook_function = function()
        require('ts_context_commentstring.internal').update_commentstring()
    end
})

Configuring each language separately seems a bit annoying though. Kommentary has a way to set a default configuration: configure_language('default', ...). Maybe that would also work? If it does, then do you think that I should update the kommentary integration example in the README to use default?

Let me know if that fixes it!

gitneeraj commented 3 years ago

@JoosepAlviste thank you for getting back. Although I tried your suggestions, they don't seem to be working well for me. Here are some screenshots of my config but feel free to grab it from the link above. svelte-comment-string-4july21 treesitter-commentstring-4july21

Edit: I tried digging into your code and found it does get the right commentstring for svelte files here in https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/main/lua/ts_context_commentstring/internal.lua#L96. I think 'b3nj5m1n/kommentary' is not picking up that to apply. I very well may be wrong here as I am not a lua developer.

JoosepAlviste commented 3 years ago

@gitneeraj I managed to reproduce the problem with your kommentary config. The problem is that kommentary has a default config which is used to fill language configs if you run configure_language:

local default = {"//", {"/*", "*/"}, false, false, true, true, nil}

So, svelte ends up with a config like {"//", {"/*", "*/"}, false, false, true, true, <configured hook_function>}. Since the comment strings are explicitly configured there ("//", {"/*", "*/"}), then kommentary will use those and not the commentstring setting.

The fix is to configure single_line_comment_string and multi_line_comment_strings to "auto":

require('kommentary.config').configure_language('svelte', {
  single_line_comment_string = 'auto',
  multi_line_comment_strings = 'auto',
  hook_function = function()
    require('ts_context_commentstring.internal').update_commentstring()
  end
})

Now, kommentary should use the commentstring setting in svelte files. The reason it worked with javascriptreact is that the kommentary config for javascriptreact already includes auto as the comment strings by default: ["javascriptreact"] = {"auto", "auto"},.

Hopefully this time your problem will be fixed 😄 Let me know if there's anything else. I'll also update the kommentary example in the README.

gitneeraj commented 3 years ago

Bingo! That WORKS!! @JoosepAlviste thanks a lot for helping and for this wonderful plugin/extension :) Feel free to close this issue!