nvim/lua/plugins/treesitter.lua
xfy 9c8deeafc8 Reorganize config modules into lua/plugins/ and add comprehensive docs
- Move lsp.lua, pick.lua, treesitter.lua, git.lua to lua/plugins/
- Update all require() paths in pack.lua and init.lua
- Unify code style: tab indentation, line length control, empty braces
- Add detailed Chinese module documentation to all config files
- Add grug-far.nvim plugin with <leader>sr binding
- Enable vim._core.ui2 for Neovim 0.12+ UI enhancements
- Disable additional built-in plugins to reduce startup overhead

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 16:42:02 +08:00

93 lines
2.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- =============================================================================
-- Treesitter 配置 (lua/treesitter.lua)
-- =============================================================================
-- 本模块配置 Neovim 的 Treesitter 集成,提供语法树驱动的语法高亮、
-- 代码折叠和文本对象支持。
--
-- 加载方式:
-- 由 pack.lua 通过 lazy.on_event("treesitter", "VimEnter", "*", ...) 延迟加载。
--
-- 设计要点:
-- 1. Parser 安装延迟 100ms 执行,避免阻塞 startup
-- 2. 高亮按 buffer 动态附加FileType autocmd只安装需要的 parser
-- =============================================================================
local M = {}
-- ---------------------------------------------------------------------------
-- 预安装的 parser 列表
-- ---------------------------------------------------------------------------
-- 这些 parser 会在首次启动后延迟安装。
-- 如果某个 parser 未在此列表中,打开对应文件类型时仍可通过 nvim-treesitter
-- 的 :TSInstall 手动安装。
local ensure_installed = {
"lua",
"vim",
"vimdoc",
-- Web 前端
"javascript",
"typescript",
"tsx", -- TypeScript JSX
"jsdoc", -- JSDoc 注释
"json",
"html",
"css",
"yaml",
-- 后端
"c",
"rust",
"toml",
"go",
"gomod", -- Go Modules
"gosum", -- Go Sum
"gowork", -- Go Workspaces
-- 基础设施
"dockerfile",
"make",
}
-- ---------------------------------------------------------------------------
-- 模块初始化
-- ---------------------------------------------------------------------------
M.setup = function()
-- 加载 nvim-treesitter通过 packadd 激活 opt 插件)
vim.cmd.packadd("nvim-treesitter")
local treesitter = require("nvim-treesitter")
-- 延迟安装 parser在 startup 完成 100ms 后后台安装,
-- 避免在安装过程中阻塞编辑器。
vim.defer_fn(function()
treesitter.install(ensure_installed)
end, 100)
-- 按文件类型动态启用 treesitter 高亮。
-- 当文件的 filetype 被设置时BufRead、:setfiletype 等),
-- 尝试查找并加载对应的 parser成功后启用高亮。
vim.api.nvim_create_autocmd("FileType", {
pattern = "*", -- 匹配所有文件类型
callback = function(args)
local buf = args.buf
local ft = vim.bo[buf].filetype
-- 将文件类型映射到 treesitter 语言名
-- 例如 "javascriptreact" → "javascript"
local lang = vim.treesitter.language.get_lang(ft)
if not lang then
return -- 无对应语言,跳过
end
-- 尝试注册语言(如果 parser 已安装)
-- pcall 用于安全调用parser 未安装时不会报错
local ok_add = pcall(vim.treesitter.language.add, lang)
if not ok_add then
return -- parser 未安装,跳过
end
-- 启用 treesitter 高亮
pcall(vim.treesitter.start, buf, lang)
end,
})
end
return M