Skip to content

fix(view): guard treesitter parser against missing/invalid parsers - #56

Open
thenomadlad wants to merge 1 commit into
folke:mainfrom
thenomadlad:fix/treesitter-parser-crash
Open

thenomadlad wants to merge 1 commit into
folke:mainfrom
thenomadlad:fix/treesitter-parser-crash

Conversation

@thenomadlad

@thenomadlad thenomadlad commented May 15, 2026 •

Copy link
Copy Markdown

Fixes #54.

Problem

Two crash paths in get_node:

  1. vim.treesitter.get_parser(buf) was called without error handling — throws for filetypes with no registered parser, crashing the whole dimming update.
  2. root:descendant_for_range can return nil when the cursor position falls outside the tree's range (e.g. empty or truncated lines), and :parent() was called on it immediately without a nil check.

Solution

  • Single pcall gate in get_context: acquires the parser once with pcall and checks both the config flag and parser validity before entering the treesitter path. Falls back to line-based context if either fails.
  • get_node(parser, buf, line): receives the parser as an argument — pure tree traversal, no error handling, no config checks. Guards against nil return from descendant_for_range before dereferencing.
  • expand(parser, buf, ...): threads the parser through to get_node, same logic.

The safety boundary is unambiguous: one pcall in get_context gates everything, and get_node handles the nil node case internally.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the size/l Large PR (<100 lines changed) label May 15, 2026
…d parsers

Wraps vim.treesitter.get_parser in a single pcall in get_context, which
acts as the sole gate for the treesitter path. get_node and expand now
receive the parser as an argument, keeping them as pure tree-traversal
functions with no error handling of their own. Falls back to line-based
context when treesitter is disabled or no valid parser exists for the
buffer. Fixes folke#54.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@thenomadlad
thenomadlad force-pushed the fix/treesitter-parser-crash branch from a681a55 to 24e8007 Compare May 15, 2026 05:56
@mdedonno1337

Copy link
Copy Markdown

hi. any update ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/l Large PR (<100 lines changed)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: pcall(treesitter.get_parser) returns 'true' when parser is nil

2 participants