-
Notifications
You must be signed in to change notification settings - Fork 225
Description
Describe the bug
In many languages, I noticed that the trailing comma is not selected with @parameter.outer
.
List of languages to be fixed. Tick the box to indicate they are fixed:
- Rust
- ECMA
- Python
- Lua
- C? C++? Go? And more.. (comment to report, I will update the issue.)
To Reproduce
In Lua, for example,
local a = {
1,
2,
3, -- <- cursor at comma
}
local b = {
4,
5,
6,
}
selecting @parameter.outer
will select 4,
but we want 3,
.
Expected behavior
selecting @parameter.outer
will select 4,
but we want 3,
.
Output of :checkhealth nvim-treesitter
==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()
Installation ~
- ✅ OK
tree-sitter
found 0.25.3 (parser generator, only needed for :TSInstallFromGrammar)
- ✅ OK
node
found v23.11.0 (only needed for :TSInstallFromGrammar)
- ✅ OK
git
executable found.
- ✅ OK
cc
executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
Version: Apple clang version 17.0.0 (clang-1700.0.13.3)
- ✅ OK Neovim was compiled with tree-sitter runtime ABI version 15 (required >=13). Parsers must be compatible with runtime ABI.
OS Info:
{
machine = "arm64",
release = "24.5.0",
sysname = "Darwin",
version = "Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000"
} ~
Parser/Features H L F I J
- bash ✓ ✓ ✓ . ✓
- bibtex ✓ . ✓ ✓ ✓
- c ✓ ✓ ✓ ✓ ✓
- c_sharp ✓ ✓ ✓ . ✓
- cmake ✓ . ✓ ✓ ✓
- cpp ✓ ✓ ✓ ✓ ✓
- css ✓ . ✓ ✓ ✓
- csv ✓ . . . .
- diff ✓ . ✓ . ✓
- dockerfile ✓ . . . ✓
- editorconfig ✓ . ✓ . ✓
- fortran ✓ . ✓ ✓ ✓
- git_config ✓ . ✓ . ✓
- git_rebase ✓ . . . ✓
- gitattributes ✓ ✓ . . ✓
- gitcommit ✓ . . . ✓
- gitignore ✓ . . . ✓
- haskell ✓ ✓ ✓ . ✓
- html ✓ ✓ ✓ ✓ ✓
- ini ✓ . ✓ . ✓
- java ✓ ✓ ✓ ✓ ✓
- javascript ✓ ✓ ✓ ✓ ✓
- json ✓ ✓ ✓ ✓ .
- jsonc ✓ ✓ ✓ ✓ ✓
- latex ✓ . ✓ . ✓
- lua ✓ ✓ ✓ ✓ ✓
- make ✓ . ✓ . ✓
- markdown ✓ . ✓ ✓ ✓
- markdown_inline ✓ . . . ✓
- perl ✓ . ✓ . ✓
- php ✓ ✓ ✓ ✓ ✓
- po ✓ . ✓ . ✓
- powershell ✓ ✓ ✓ ✓ ✓
- prolog ✓ . ✓ ✓ ✓
- pymanifest ✓ . . . ✓
- python ✓ ✓ ✓ ✓ ✓
- query ✓ ✓ ✓ ✓ ✓
- requirements ✓ . . . ✓
- rst ✓ ✓ . . ✓
- ruby ✓ ✓ ✓ ✓ ✓
- rust ✓ ✓ ✓ ✓ ✓
- sql ✓ . ✓ ✓ ✓
- ssh_config ✓ ✓ ✓ ✓ ✓
- tmux ✓ . . . ✓
- toml ✓ ✓ ✓ ✓ ✓
- tsv ✓ . . . .
- tsx ✓ ✓ ✓ ✓ ✓
- typescript ✓ ✓ ✓ ✓ ✓
- vim ✓ ✓ ✓ . ✓
- vimdoc ✓ . . . ✓
- xml ✓ ✓ ✓ ✓ ✓
- yaml ✓ ✓ ✓ ✓ ✓
Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
+) multiple parsers found, only one will be used
x) errors found in the query, try to run :TSUpdate {lang} ~
Output of nvim --version
NVIM v0.11.1
Build type: Release
LuaJIT 2.1.1744318430
Run "nvim -V1 -v" for more info
Additional Context
Make sure one element with and without comma are supported (don't break this)
local a = {1,}
local b = {2}
Selecting both 1,
and 2
should still work, and not to select 1
without the comma.
Solution
Many existing implementations look like this:
((parameters
"," @_start
.
(_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((parameters
.
(_) @parameter.inner
.
","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
I propose to change to this. It's more intuitive separated by cases, although, a little longer.
(parameters
(_) @parameter.inner)
; first element, with or without comma
((parameters
.
(_) @_start
.
","? @_end)
(#make-range! "parameter.outer" @_start @_end))
; second to last element (with leading comma)
((parameters
"," @_start
.
(_) @_end)
(#make-range! "parameter.outer" @_start @_end))
; last element, with trailing comma
((parameters
(_) @_start
.
"," @_end
(comment)* .) ; ignore trailing comments
(#make-range! "parameter.outer" @_start @_end))
In fact, due to #768, I think it may make sense to match trailing comma in all cases, not only for the first element. The below example do that but the last element fails to grab the leading comma.
; WARNING: not working properly with the last element.
(parameters
(_) @parameter.inner)
; with or without trailing comma
; NOTE: this fails to select the leading comma at the last element..
((parameters
(_) @_start
.
","? @_end)
(#make-range! "parameter.outer" @_start @_end))
; last element, with leading comma
((parameters
"," @_start
.
(_) @_start
.
(comment)* .) ; ignore trailing comments
(#make-range! "parameter.outer" @_start @_end))