Skip to content

fix(nvim): restart treesitter highlighting after new parsers install - #202

Draft
aguil wants to merge 1 commit into
masterfrom
fix/nvim-treesitter-install-restart
Draft

aguil wants to merge 1 commit into
masterfrom
fix/nvim-treesitter-install-restart

Conversation

@aguil

@aguil aguil commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Why the change

When a new treesitter parser compiles in the background at startup, buffers that are already open never pick it up, so this restarts their highlighting once the install finishes.

Special things to note

  • The restart only runs when a parser was actually missing, so normal startups don't change apart from skipping the install module.
  • Every loaded buffer with a managed filetype gets reparsed, not just buffers that use the new language. That's simpler, and it only happens on a launch that installs something.

Change outline

Startup install step in the nvim-treesitter config (dot_config/nvim/init.lua):

 nvim-treesitter config
-  if tree-sitter CLI available
-    install(all parsers)               # background; open buffers never told
+  missing = parsers not yet installed
+  if missing and tree-sitter CLI available
+    install(missing):await
+      on done (scheduled)
+        for each loaded buffer with a managed filetype
+          invalidate its parser       # re-evaluates injected code blocks
+          stop + start highlighting   # attaches buffers that had none

Details

Summary

Treesitter parsers compile in the background at startup. Buffers that
load before a new parser finishes never pick it up. A .rs buffer gets
no highlighter, and a markdown buffer's ```rust block stays
unhighlighted until the next edit or :edit. This showed up on the first
launch after #201 added the rust parser.

Changes

  • Install only the parsers that are missing, instead of passing the full list every time.
  • When that install finishes, reparse and restart highlighting in every loaded buffer whose filetype the config manages.

Testing

In an isolated data dir with the Rust parser and queries removed, a
markdown buffer and a .rs buffer were open while the parser compiled.
No edits or forced reloads afterwards:

markdown injected languages .rs highlighter
master, after install markdown_inline, lua, typescript off
this PR, after install rust, lua, markdown_inline, typescript on

Startup with nothing to install (randomized A/B, n=80): master 55.8 ms
median, this PR 53.5 ms. The install module is no longer loaded when no
parser is missing. get_installed() takes 0.17 ms.

Gates: pre-commit --all-files, tests/nvim/smoke.lua,
tests/nvim/run_tests.lua, and both bats suites pass.

🤖 Generated with Claude Code

Parsers compile in the background at startup. Buffers that load before
the compile finishes never pick the new language up: a .rs buffer keeps
no highlighter, and a markdown buffer keeps its ```rust block parsed
without Rust until the next edit or :edit. Install only the missing
parsers and, once that task completes, reparse and restart highlighting
in every loaded buffer whose filetype this config manages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant