JoosepAlviste / nvim-ts-context-commentstring

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

Comments inside <script> tags use <!-- --> #19

Closed xbc5 closed 3 years ago

xbc5 commented 3 years ago

Treesitter parsers html and javascript are installed; the module is loaded:

TSConfigInfo (modules)

context_commentstring = {                                                                         
      disable = {},                                                                                   
      enable = true,                                                                                  
      enable_autocmd = false,                                                                         
      loaded = true,                                                                                  
      module_path = "ts_context_commentstring.internal"                                               
}

Demo

index.html after commenting:

<!DOCTYPE html>
<html lang="en">
  <script>
    <!-- console.log("foo"); -->
  </script>
</html>

Config

packer:

use 'JoosepAlviste/nvim-ts-context-commentstring'
use {
  'b3nj5m1n/kommentary',
  config = require("config.kommentary").config,
}

config/kommentary.lua:

local M = {}

function M.config()
  require('kommentary.config').configure_language('default', {
    prefer_single_line_comments = true,
  })

  local langs = require("kommentary.config").config -- all default supported langs
  for lang, _ in pairs(langs) do
    require('kommentary.config').configure_language(lang, {
        single_line_comment_string = 'auto',
        multi_line_comment_strings = 'auto',
        hook_function = function()
          require('ts_context_commentstring.internal').update_commentstring()
        end,
      })
  end
end

return M

config.nvim-treesitter.lua:

return function()
  require("nvim-treesitter.configs").setup {
    ensure_installed = {
      "html", "javascript",
    },
    context_commentstring = {
      enable = true,
      enable_autocmd = false, -- required for kommentary
    }
  }
end
JoosepAlviste commented 3 years ago

Hey @xbc5! Does the commentstring get updated correctly when you set enable_autocmd = true?

If so, you might have an issue in your kommentary config, you could try printing it out to debug it.

xbc5 commented 3 years ago

It does. What do you suspect the issue might be?

require('kommentary.config').configure_language('default', {
  prefer_single_line_comments = true,
})

local langs = require("kommentary.config").config -- all default supported langs
for lang, _ in pairs(langs) do
  require('kommentary.config').configure_language(lang, {
      single_line_comment_string = 'auto',
      multi_line_comment_strings = 'auto',
      hook_function = function()
        require('ts_context_commentstring.internal').update_commentstring()
      end,
    })
end
JoosepAlviste commented 3 years ago

Hey @xbc5! Sorry it took a while to answer. I think I see what's going on with the config.

  1. require("kommentary.config").config does not return html, so html config relies on the default config
  2. Kommentary doesn't use the hook function from the default config, get_default_config is called after the hook function is called from the explicit config

The combination of these two things means that the hook_function is not called for html even if it were configured in the default config.

An "easy" fix would be to just add another explicit config for html:

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

A more correct fix would probably be to call the hook function after using the default config inside kommentary, then you could just configure the hook_function in the default config. But that would require making a PR to kommentary or creating an issue there.

Hope this helps!

JoosepAlviste commented 3 years ago

Hey! I'll close this for now, feel free to re-open if you're still having trouble getting it to work.