folke / ts-comments.nvim

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

bug: Uncommenting in PHP fails if TS parsers `php` and `phpdoc` are installed #55

Open ybc37 opened 1 month ago

ybc37 commented 1 month ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.10.1

Operating system/version

Linux 6.10.3-arch1-2

Describe the bug

First, I'm not actually sure where this bug belongs, because I don't quite understand every part of it.

I noticed, that uncommenting a line in PHP sometimes results in commenting the commented code with block style comments:

  1. $foo = 1;
  2. Comment with gcc: // $foo = 1;
  3. Uncomment with gcc: /*// $foo = 1;*/

This happens when...

I can imagine, it's expected behavior somehow, but practically it's unintuitive at best. I wonder if this is something ts-comments.nvim can fix/improve or if there's some other approach to change this behavior.

Steps To Reproduce

Example PHP file (repro.php):

<?php

class Foo
{
    private $bar = "";
}
  1. nvim -u repro.lua repro.php
  2. /priv<CR>gcc -> line is commented; cursor is on the first slash of // private...
  3. gcc -> line is now wrapped with a blockwise comment
  4. undo -> cursor is on the first slash, again
  5. 0gcc -> line is uncommented

Expected Behavior

The code is uncommented.

Repro

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    { "folke/ts-comments.nvim", opts = {} },
    {
      'nvim-treesitter/nvim-treesitter',
      build = ':TSUpdate',
      config = function()
        require('nvim-treesitter.configs').setup({
          ensure_installed = {'php', 'phpdoc'},
          highlight = {
            enable = true,
          },
      })
      end
    },
  }
})
mauchchhash commented 2 weeks ago

Happening to me too 😿

TymekDev commented 2 days ago

I have recently browser through nvim-treesitter and noticed it injects comment parser into multiple languages. This also breaks uncommenting (and might make for a simpler repro).

Currently, 236 out of 297 parsers have comment injected. ```sh git -C ~/.local/share/nvim/lazy/nvim-treesitter/ rev-parse @ # 1fbc25fc111cf5c7e73819657c8968dc5fa302ad rg -l '(#set! injection.language "comment")' ~/.local/share/nvim/lazy/nvim-treesitter/queries/ | wc -l # 236 ls -1 ~/.local/share/nvim/lazy/nvim-treesitter/queries/ | wc -l # 297 ```
TymekDev commented 2 days ago

I did a bit of testing, and I am not even sure if this is ts-comments.nvim issue. I would say it's a matter of proper configuration and/or missing defaults.

This particular issue is caused by the commented line being treated as phpdoc. Notice the tags on right-hand side of the tree changing. If we are in php, then it works as expected. If we are in phpdoc, then it "breaks".

https://github.com/user-attachments/assets/825f3162-910b-4ddb-adb1-f3a3a6688c8e

There is no default ts-comments.nvim config for php. The README states:

ts-comments.nvim uses the default Neovim commentstring as a fallback, so there's no need to configure every language.

And the default commentstring is /*%s*/, so it works as intended.

Updating your reproduction script with:

{
  "folke/ts-comments.nvim",
  opts = {
    lang = {
      phpdoc = { "// %s" },
    },
  },
}

Changes the behavior to:

https://github.com/user-attachments/assets/91a58553-f0bc-4ec4-b638-bd60ae3c43a1

Aside: notice that the deafult commentstring is always included. That's why both // %s and /*%s*/ work for php.