nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects
JS "generator function" expression are not recognised
Nobody has claimed this yet.
- Dominant language
- Tree-sitter Query
- Stars
- 2.8k
- Forks
- 271
- Avg merge
- 8d 8h
- Merged PRs (30d)
- 1
Description
Hi,
I am stumbling on an issue similar to https://github.com/nvim-treesitter/nvim-treesitter-textobjects/issues/633, more specifically in the case where the generator function is not declared as a statement but as an expression (for example as an argument of another function).
The command :TSTextobjectSelect @function.outer selects nothing when the cursor is placed inside a generator function, when that function is not declared with a statement.
Thanks for the help!
To reproduce
- Open a new file, in a neovim instance with TS installed
- Write/paste a generator function and assign it to a variable
const fn = function* () { yield 1; }; - Put cursor inside and try select the whole function with
:TSTextobjectSelect @function.outer
Expected behaviour
The full function is selected instead of nothing happening.
Note that the code is correctly parsed and the "function" is parsed as a generator_function:
Additional context
I have tried to add new text objects definitions in the file <config folder>/queries/ecma/textobjects.scm:
(generator_function
body: (statement_block)) @function.outer
(generator_function
body: (statement_block
.
"{"
.
(_) @_start @_end
(_)? @_end
.
"}"
(#make-range! "function.inner" @_start @_end)))
But it does not work as expected (now, the :TSTextobjectSelect @function.outer command only selected generator functions and not regular functions anymore π
). I guess I am missing something...
Output of :checkhealth nvim-treesitter
==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()
Installation ~
- OK `tree-sitter` found 0.25.2 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v20.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 16.0.0 (clang-1600.0.26.6)
- 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.3.0",
sysname = "Darwin",
version = "Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000"
} ~
Parser/Features H L F I J
- bash β β β . β
- c β β β β β
- diff β . β . β
- dockerfile β . . . β
- graphql β . . β β
- html β β β β β
- javascript β β β β β
- json β β β β .
- lua β β β β β
- markdown β . β β β
- markdown_inline β . . . β
- prisma β . β . β
- python β β β β β
- query β β β β β
- regex β . . . .
- svelte β β β β β
- tsx β β β β β
- typescript β β β β β
- vim β β β . β
- vimdoc β . . . β
- 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.0-dev-1878+g45d7aa3301
Build type: RelWithDebInfo
LuaJIT 2.1.1736781742
Run "nvim -V1 -v" for more info
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 with /queries/ecma/textobjects.scm and inspect how the function.outer captures cover regular functions versus generator_function nodes. Reproduce the issue with the provided JavaScript generator expression and :TSTextobjectSelect @function.outer, then verify that both expression generators and regular functions select the full function without breaking existing text objects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100