folke / ts-comments.nvim

Tiny plugin to enhance Neovim's native comments
Apache License 2.0
321 stars 23 forks source link

bug: didn't work at all for `javascriptreact` and `typescriptreact` filetype #33

Closed t18n closed 3 months ago

t18n commented 3 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

v0.10.0

Operating system/version

Mac OS 14.4.1 (23E224)

Describe the bug

I've verified with Lazy and the plugin is installed properly. However, when I use it inside my tsx and jsx files, they don't work at all. All the element comments just added // %s. I used the command :set filetype? to and and they returns javascriptreact and typescriptreact as the filetype instead, so I suspect that this is the issue.

Steps To Reproduce

  1. Install folke/ts-comments.nvim
  2. Open a tsx file and check :set filetype? if it is typescriptreact
  3. Go to a React element and toggle comment

Expected Behavior

Go to a React element and toggle comment should respect {/* %s */} format

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  { "folke/ts-comments.nvim", opts = {
lang = {
         tsx = {
          "// %s",                   -- default commentstring when no treesitter node matches
          "/* %s */",
          call_expression = "// %s", -- specific commentstring for call_expression
          jsx_attribute = "// %s",
          jsx_element = "{/* %s */}",
          jsx_fragment = "{/* %s */}",
          spread_element = "// %s",
          statement_block = "// %s",
        },
        typescriptreact = {
          -- "// %s",                   -- default commentstring when no treesitter node matches
          -- "/* %s /",
          call_expression = "// %s", -- specific commentstring for call_expression
          jsx_attribute = "// %s",
          jsx_element = "{/* %s */}",
          jsx_fragment = "{/* %s */}",
          spread_element = "// %s",
          statement_block = "// %s",
        },

}
} },
    {
    -- Highlight, edit, and navigate code
    'nvim-treesitter/nvim-treesitter',
    dependencies = {
      'nvim-treesitter/nvim-treesitter-textobjects',
      {
        -- Rainbow brackets
        "HiPhish/rainbow-delimiters.nvim",
        config = function()
          vim.api.nvim_create_user_command("RainbowDelimitersToggle", function()
            require("rainbow-delimiters").toggle(0)
          end, { desc = "Toggle Rainbow Delimiters" })

          vim.g.rainbow_delimiters = {
            -- log = {
            --   level = vim.log.levels.TRACE,
            -- },
            blacklist = { "html", "lua", "json" },
          }
        end,
      },
    },
    build = ':TSUpdate',
    config = function()
      -- Defer Treesitter setup after first render to improve startup time of 'nvim {filename}'
      vim.defer_fn(function()
        require('nvim-treesitter.configs').setup {
          -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
          auto_install = true,
          -- Install languages synchronously (only applied to `ensure_installed`)
          sync_install = true,
          -- List of parsers to ignore installing
          ignore_install = {},
          -- You can specify additional Treesitter modules here: -- For example: -- playground = {--enable = true,-- },
          modules = {},

          ensure_installed = {
            'javascript',
            'typescript',
            'tsx',
            'liquid',
          },
        }
      end, 0)
    end
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
t18n commented 3 months ago

This issue may be related: https://github.com/neovim/neovim/issues/24531

folke commented 3 months ago

Can't reproduce this. Your repro is also far from minimal and setup ts-comments twice

t18n commented 3 months ago

@folke I apologize for the minimal code, I overlooked it and the existing ts-comment.

I have recorded my issue here. You can see the file type in the bottom right lualine. https://github.com/folke/ts-comments.nvim/assets/14198542/27df4a37-db5a-402e-a845-e38c118ad3b7

What does your tsx file return when you run :set filetype??

folke commented 3 months ago
$ nvim -u repro.lua foo.tsx

Steps:

repro.lua ```lua -- DO NOT change the paths and don't remove the colorscheme local root = vim.fn.fnamemodify("./.repro", ":p") -- set stdpaths to use .repro for _, name in ipairs({ "config", "data", "state", "cache" }) do vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name end -- bootstrap lazy local lazypath = root .. "/plugins/lazy.nvim" if not vim.loop.fs_stat(lazypath) then vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { "folke/tokyonight.nvim", { "folke/ts-comments.nvim", opts = {}, }, { -- Highlight, edit, and navigate code "nvim-treesitter/nvim-treesitter", build = ":TSUpdate", config = function() require("nvim-treesitter.configs").setup({ ensure_installed = { "javascript", "typescript", "tsx", }, }) end, }, } require("lazy").setup(plugins, { root = root .. "/plugins", }) vim.cmd.colorscheme("tokyonight") -- add anything else here ```
foo.tsx ```tsx import type { Component, ParentProps } from "solid-js"; export const Content: Component = (props) => { return (
{props.children}
); }; ```
folke commented 3 months ago

Since the repro works just fine, there's something else wrong with your installation. Are you using either stable or a recent nightly?

t18n commented 3 months ago

Thanks @folke! I use the stable version from homebrew

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1716656478
Run "nvim -V1 -v" for more info

I ran the repro as you showed and it worked correctly on my machine too, so I bet tha my installation is the problem. The repro filetype is also typescript react. Hmm, weird! Anything on top of your mind that you can think of?

t18n commented 3 months ago

So, turns out I am an idiot. I had been using comment.nvim prior to 0.10.0 for commenting. Then I upgraded to the latest version and using ts-comment while forgetting to delete comment.nvim. Now I deleted and it works perfectly. Thank you for your help and the awesome plugin @folke.