Scripts2026年7月28日·1 分钟阅读

nvim-treesitter — Treesitter Configurations and Abstraction Layer for Neovim

Provides Tree-sitter parser integration for Neovim, enabling accurate syntax highlighting, incremental selection, code folding, and indentation based on real parse trees instead of regex patterns.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
nvim-treesitter
先审查命令
npx -y tokrepo@latest install d358e1a0-8a1b-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

nvim-treesitter brings Tree-sitter parsing to Neovim, replacing regex-based syntax highlighting with incremental, grammar-based parsing. It provides a unified interface for installing language parsers and configuring modules like highlighting, indentation, and incremental selection across 200+ languages.

What nvim-treesitter Does

  • Installs and manages Tree-sitter language parsers for 200+ languages
  • Provides syntax highlighting based on real abstract syntax trees
  • Enables incremental selection that expands to the next syntactic node
  • Offers indentation computed from the parse tree rather than heuristics
  • Supports code folding using Tree-sitter node boundaries

Architecture Overview

nvim-treesitter acts as a configuration and abstraction layer on top of Neovim's built-in Tree-sitter integration. It ships query files (highlights.scm, indents.scm, folds.scm) for each supported language, manages parser compilation and installation via :TSInstall, and exposes a module system where each feature (highlight, indent, incremental_selection) can be independently enabled or configured.

Self-Hosting & Configuration

  • Install via any Neovim plugin manager (lazy.nvim, packer, vim-plug)
  • Run :TSUpdate after installation to compile parsers
  • Configure modules in your init.lua with require('nvim-treesitter.configs').setup({})
  • Set ensure_installed to a list of languages or "all" for automatic parser installation
  • Enable specific modules: highlight = { enable = true }, indent = { enable = true }

Key Features

  • Grammar-based highlighting that handles nested languages correctly
  • Incremental selection expanding from cursor to enclosing AST nodes
  • Automatic parser installation and updates via :TSInstall / :TSUpdate
  • Query-based text objects for selecting functions, classes, and parameters
  • Playground module for inspecting the syntax tree interactively

Comparison with Similar Tools

  • Regex-based syntax — Traditional Vim highlighting uses regex patterns that break on edge cases; nvim-treesitter uses real parsers for accuracy
  • vim-polyglot — Bundles many filetype plugins but still uses regex; nvim-treesitter provides structural parsing
  • Tree-sitter CLI — The standalone CLI parses files; nvim-treesitter integrates parsing directly into the editor
  • LSP semantic tokens — Complements treesitter by adding type-aware highlighting from the language server

FAQ

Q: Does nvim-treesitter replace LSP? A: No. Tree-sitter handles syntax-level features (highlighting, folding, text objects) while LSP provides semantic features (go-to-definition, diagnostics, rename). They work together.

Q: How do I add a language parser? A: Run :TSInstall inside Neovim. The parser is compiled locally and cached.

Q: Does it slow down Neovim? A: Tree-sitter uses incremental parsing, so only changed portions of the buffer are re-parsed. Performance is comparable to or better than regex highlighting on large files.

Q: Can I write custom queries? A: Yes. Place custom query files in after/queries// in your Neovim config directory to extend or override default queries.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产