nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects
bug: trailing comma not working with `@parameter.outer`
A pull request for this has already been merged.
- #767 by @kiyoon — merged
- Dominant language
- Tree-sitter Query
- Stars
- 2.8k
- Forks
- 271
- Avg merge
- 8d 8h
- Merged PRs (30d)
- 1
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))
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting the language-specific parameter queries for the listed Rust, ECMA, Python, and Lua cases, using the provided Lua reproduction and proposed Tree-sitter Query patterns as references. Done means @parameter.outer consistently includes the relevant leading or trailing comma for elements with and without commas, without breaking existing selections.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, lua, python, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100