Skip to content

fix: parse() call for Neovim 0.10+ API#674

Draft
eric-chapdelaine wants to merge 3 commits into
nvim-treesitter:masterfrom
eric-chapdelaine:ericchapdelaine_fix_parse_range_api
Draft

fix: parse() call for Neovim 0.10+ API#674
eric-chapdelaine wants to merge 3 commits into
nvim-treesitter:masterfrom
eric-chapdelaine:ericchapdelaine_fix_parse_range_api

Conversation

@eric-chapdelaine
Copy link
Copy Markdown

@eric-chapdelaine eric-chapdelaine commented May 18, 2026

Summary

  • Fixes the "attempt to call method range (a nil value)" error when moving the cursor in Neovim 0.10+
  • Uses a version check to determine which parse() API to use

Details

In Neovim 0.10+, LanguageTree:parse() requires an array of ranges instead of a single Range4 table. Added a version check (vim.version().minor >= 10) to use:

  • { range } array for Neovim 0.10+
  • range single value for Neovim 0.9

This ensures full backwards compatibility with both versions.

@eric-chapdelaine eric-chapdelaine force-pushed the ericchapdelaine_fix_parse_range_api branch 2 times, most recently from f73fd40 to e776576 Compare May 18, 2026 01:43
In Neovim 0.10+, LanguageTree:parse() requires an array of ranges
instead of a single Range4 table. Wrapping 'range' in braces fixes
the 'attempt to call method range (a nil value)' error when moving
the cursor in buffers with treesitter parsing.

Added explicit type annotation to satisfy lua-language-server.
@eric-chapdelaine eric-chapdelaine force-pushed the ericchapdelaine_fix_parse_range_api branch from e776576 to 537f336 Compare May 18, 2026 01:44
Neovim 0.9 and earlier don't accept an array for parse(), only
a single range. Add a version check to use array syntax only for
0.10+ while keeping backwards compatibility with 0.9.
Try Neovim 0.10+ array syntax first, fall back to single range
for older versions. This avoids version checks that may not work
reliably across different test environments.
Comment on lines +238 to +242
local function try_parse(tree, r, cb)
local ok = pcall(function() tree:parse({ r }, cb) end)
if not ok then tree:parse(r, cb) end
end
try_parse(root_tree, range, function(...) end)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So pcall() is just to fix some test environment error? Feels wrong to use it :/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! I didn't mean to set this PR as ready to review just yet. Let me play around with this more and I will message you when it's ready for review.

@eric-chapdelaine eric-chapdelaine marked this pull request as draft May 20, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants