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 #75

Closed itsramiel closed 1 year ago

itsramiel commented 1 year ago

Minimal reproducible full config

packer.lua

  use {
    'nvim-treesitter/nvim-treesitter',
    run = ':TSUpdate'
  }
....
  use {
    'numToStr/Comment.nvim',
    requires = {
      -- jx/tsx comment support
      'JoosepAlviste/nvim-ts-context-commentstring'
    }
  }

treesitter.lua

require'nvim-treesitter.configs'.setup {
  -- A list of parser names, or "all"
  ensure_installed = { "vimdoc", "javascript", "typescript", "lua", "rust", "java"},

  -- Install parsers synchronously (only applied to `ensure_installed`)
  sync_install = false,

  -- 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 = {
    -- `false` will disable the whole extension
    enable = true,

    -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
    -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
    -- Using this option may slow down your editor, and you may see some duplicate highlights.
    -- Instead of true it can also be a list of languages
    additional_vim_regex_highlighting = false,
  },
  -- https://github.com/JoosepAlviste/nvim-ts-context-commentstring#commentnvim
  context_commentstring = {
    enable = true,
    enable_autocmd = false,
  }
}

comment.lua

require('Comment').setup {
  pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
}

Description

Hi,

I am trying to make comments work in tsx files, but they end up being commented with forward slashes:

image

I am using Comment.nvim and its integration with nvim-ts-context-commentstring

When I run :lua print(require('ts_context_commentstring.internal').calculate_commentstring()) I get nil:

I hope you can point my issue given the config shown above

Steps to reproduce

Setup neovim with the config provided above

Expected behavior

I expect typescript files to be commented properly

Actual behavior

tsx is being commented with two forward slashes

Additional context

No response

JoosepAlviste commented 1 year ago

Hey! From the nvim-treesitter configuration, it looks like the tsx parser isn't installed. Could you try installing it with :TSInstall tsx (or adding it to ensure_installed)?

Another thing to try would be to run :InspectTree and seeing if there even is an existing Treesitter syntax tree.

Let me know how it goes!

itsramiel commented 1 year ago

Hey @JoosepAlviste Thank you for responding. I added tsx to the ensure_installed and did :TSInstall tsx but it still does not work. Running :InspectTree shows Not an editor command error

Here is a video if you want to see what I did https://github.com/JoosepAlviste/nvim-ts-context-commentstring/assets/80689446/7f2cce36-b31f-4f35-8666-d45c391ee400

Here is the nvim version if that helps too:

NVIM v0.8.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by runner@Mac-1668417329633.local

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

Appreciate taking the time to help

JoosepAlviste commented 1 year ago

I see you're running Neovim 0.8, that might cause some problems as I'm usually running version 0.9. I recently made some changes to use some slightly newer Treesitter APIs. Maybe they don't exist in Neovim 0.8?

Is there any chance you could try version 0.9? You could also try pinning the plugin to this commit: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/commit/9bff161dfece6ecf3459e6e46ca42e49f9ed939f as it should be before the changes I made (but I'm not 100% sure as I've used 0.9 for a while now).

I can try out version 0.8 this weekend to see if that's the root cause of the issue, maybe I can conditionally add back using some of the older APIs.

itsramiel commented 1 year ago

Hmmm, it could be because of that. My memory does not serve me well now but I can remember it working before, so that might be it.

I will also try to upgrade to neovim 0.9 this weekend hopefully. Will let you know how it goes

itsramiel commented 1 year ago

@JoosepAlviste Just updated to neovim 0.9.2 and it works 👍. I'll close it

JoosepAlviste commented 1 year ago

Nice to hear that this fixed it! I also bumped the minimal Neovim version in the readme 😅

StoicDeveloper commented 1 year ago

I am experiencing the same issue. I have the following lines in my init.lua file; though there are numerous other configs since I'm using NvChad.

require('nvim-treesitter.configs').setup {
 context_commentstring = {
    enable = true,
    enable_autocmd = false,
  },
}
require('ts_context_commentstring').setup {}
require('Comment').setup {
  pre_hook = function()
    require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook()
    print("hook2")
  end,
}

Triggering comment toggle in a tsx file results in the same problem described above, and also "hook2" is printed, so the prehook function is getting called. Executing :lua print(require('ts_context_commentstring.internal').calculate_commentstring()) results in printing "{/ %s /}".

I am also running nvim 9.4 on debian 11. Any idea how I could get this working?

JoosepAlviste commented 1 year ago

Hey @StoicDeveloper! I think that your issue comes from the pre_hook not returning the value from create_pre_hook. Try this:

require('Comment').setup {
  pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
}

Or, if you need the pre_hook to be a function with your own implementation:

require('Comment').setup {
  pre_hook = function(ctx)
    return require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook()(ctx)
  end,
}

Let me know how it goes!