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

tsx is not properly commenting even when ts-context-commentstring works #93

Closed grassleaf99 closed 10 months ago

grassleaf99 commented 10 months ago

Hi, I'm using Comment.nvim and its integration with nvim-ts-context-commentstring to comment in tsx files I follow the docs and here's my config

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1692716794
   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/__w/neovim/neovim/build/nvim.AppDir/usr/share/nvim"

plugin.lua

-- Treesitter
{
        -- Highlight, edit, and navigate code
        'nvim-treesitter/nvim-treesitter',
        dependencies = {
                'nvim-treesitter/nvim-treesitter-textobjects',
        },
        build = ':TSUpdate',
},

-- Comment
{
        'numToStr/Comment.nvim',
        opts = {
                -- add any options here
        },
        lazy = false,
},
"JoosepAlviste/nvim-ts-context-commentstring",

nvim_treesitter.lua

local status_ok_configs, nvim_treesitter_configs = pcall(require, 'nvim-treesitter.configs')
if not status_ok_configs then
        return
end

local status_ok_install, nvim_treesitter_install = pcall(require, 'nvim-treesitter.install')
if not status_ok_install then
        return
end

-- cc for linux, gcc for windows
nvim_treesitter_install.compilers = { "cc", "gcc" }

-- See `:help nvim-treesitter`
-- Defer Treesitter setup after first render to improve startup time of 'nvim {filename}'
vim.defer_fn(function()
  nvim_treesitter_configs.setup {
    -- Add languages to be installed here that you want installed for treesitter
    -- https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#supported-languages
    ensure_installed = { 
            'tsx'
    },

    -- Automatically install missing parsers when entering buffer
    -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
    auto_install = true,

    highlight = { enable = true },
    indent = { enable = true },
    incremental_selection = {
      enable = true,
      keymaps = {
        init_selection = '<c-space>',
        node_incremental = '<c-space>',
        scope_incremental = '<c-s>',
        node_decremental = '<M-space>',
      },
    },
    textobjects = {
      select = {
        enable = true,
        lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
        keymaps = {
          -- You can use the capture groups defined in textobjects.scm
          ['aa'] = '@parameter.outer',
          ['ia'] = '@parameter.inner',
          ['af'] = '@function.outer',
          ['if'] = '@function.inner',
          ['ac'] = '@class.outer',
          ['ic'] = '@class.inner',
        },
      },
      move = {
        enable = true,
        set_jumps = true, -- whether to set jumps in the jumplist
        goto_next_start = {
          [']m'] = '@function.outer',
          [']]'] = '@class.outer',
        },
        goto_next_end = {
          [']M'] = '@function.outer',
          [']['] = '@class.outer',
        },
        goto_previous_start = {
          ['[m'] = '@function.outer',
          ['[['] = '@class.outer',
        },
        goto_previous_end = {
          ['[M'] = '@function.outer',
          ['[]'] = '@class.outer',
        },
      },
      swap = {
        enable = true,
        swap_next = {
          ['<leader>a'] = '@parameter.inner',
        },
        swap_previous = {
          ['<leader>A'] = '@parameter.inner',
        },
      },
    },
  }
end, 0)

comment.lua

local status_ok_comment, comment = pcall(require, 'Comment')
if not status_ok_comment then
        return
end

local  status_ok_ts_context_commentstring, ts_context_commentstring = pcall(require, 'ts_context_commentstring')
if not status_ok_ts_context_commentstring then
        return
end

local status_ok_ts_context_commentstring_integration_comment, ts_context_commentstring_integration_comment = pcall(require, 'ts_context_commentstring.integrations.comment_nvim')
if not status_ok_ts_context_commentstring_integration_comment then
        return
end

ts_context_commentstring.setup {
  enable_autocmd = false,
}

comment.setup {
        ---Function to call before (un)comment
        pre_hook = ts_context_commentstring_integration_comment.create_pre_hook(),
}

When I run :lua print(require('ts_context_commentstring.internal').calculate_commentstring()) I get {/* %s */}, which means it works, but the comment applied still forward slashes

image

Did I make a mistake somewhere in my config, or because I'm using nvim 0.9.4 (I saw a comment that someone using nvim 0.9.4 has the issue like mine)?

grassleaf99 commented 10 months ago

My bad, I'm not importing the config for comment properly... Closing issue now