ts-comments.nvim
ts-comments.nvim copied to clipboard
bug: Uncommenting in PHP fails if TS parsers `php` and `phpdoc` are installed
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:
$foo = 1;- Comment with
gcc:// $foo = 1; - Uncomment with
gcc:/*// $foo = 1;*/
This happens when...
- ...the TS parsers
phpandphpdocare 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 = "";
}
nvim -u repro.lua repro.php/priv<CR>gcc-> line is commented; cursor is on the first slash of// private...gcc-> line is now wrapped with a blockwise commentundo -> cursor is on the first slash, again0gcc-> 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
},
}
})
Happening to me too 😿
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
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
commentstringas 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.
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.
This issue was closed because it has been stalled for 7 days with no activity.