ts-comments.nvim icon indicating copy to clipboard operation
ts-comments.nvim copied to clipboard

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

Open ybc37 opened this issue 1 year ago • 3 comments
trafficstars

Did you check docs and existing issues?

  • [X] I have read all the ts-comments.nvim docs
  • [X] I have updated the plugin to the latest version before submitting this issue
  • [X] I have searched the existing issues of ts-comments.nvim
  • [X] I have searched the existing issues of plugins related to this issue

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...

  • ...the TS parsers php and phpdoc are installed
  • ...TS highlighting is enabled
  • ...the cursor is in the comment while uncommenting (when the cursor is on whitespace before // ..., it works as expected).

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
    },
  }
})

ybc37 avatar Aug 09 '24 13:08 ybc37

Happening to me too 😿

mauchchhash avatar Sep 06 '24 09:09 mauchchhash

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.
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 avatar Sep 20 '24 08:09 TymekDev

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.

TymekDev avatar Sep 20 '24 08:09 TymekDev

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Oct 21 '24 02:10 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Oct 28 '24 02:10 github-actions[bot]