mirror of
https://github.com/itme-brain/nvim.git
synced 2026-05-08 07:00:13 -04:00
feat(treesitter): use community fork provisioner
This commit is contained in:
parent
34c52305af
commit
ef4972deb9
5 changed files with 152 additions and 32 deletions
|
|
@ -20,7 +20,7 @@ git clone --recurse-submodules git@github.com:itme-brain/nixos.git
|
||||||
- **Smart LSP picker** (`<leader>css`) - auto-detects installed servers for current filetype
|
- **Smart LSP picker** (`<leader>css`) - auto-detects installed servers for current filetype
|
||||||
- **Neovim 0.12 compatible** - uses built-in `:lsp` commands and keeps legacy `:Lsp*` aliases working
|
- **Neovim 0.12 compatible** - uses built-in `:lsp` commands and keeps legacy `:Lsp*` aliases working
|
||||||
- **Portable** - Mason is the source of truth for LSP servers and debug adapters
|
- **Portable** - Mason is the source of truth for LSP servers and debug adapters
|
||||||
- **Treesitter** - uses Neovim's native `vim.treesitter.*` APIs without `nvim-treesitter`
|
- **Treesitter** - uses native `vim.treesitter.*` APIs with community fork provisioning
|
||||||
- **Debugging** - lazy-loaded `nvim-dap` stack with Mason-managed adapters
|
- **Debugging** - lazy-loaded `nvim-dap` stack with Mason-managed adapters
|
||||||
|
|
||||||
## LSP Setup
|
## LSP Setup
|
||||||
|
|
@ -46,11 +46,11 @@ On Neovim 0.12+, start/stop/restart uses the built-in `:lsp` commands under the
|
||||||
Treesitter uses Neovim's native API:
|
Treesitter uses Neovim's native API:
|
||||||
- Highlighting starts through `vim.treesitter.start`
|
- Highlighting starts through `vim.treesitter.start`
|
||||||
- Parser/filetype mappings use `vim.treesitter.language.register`
|
- Parser/filetype mappings use `vim.treesitter.language.register`
|
||||||
- Parser and query installation is intentionally outside lazy.nvim and Mason
|
- Parsers and queries are provisioned by the community `neovim-treesitter/nvim-treesitter` fork
|
||||||
|
|
||||||
Neovim loads compiled parser libraries from `parser/` directories on `runtimepath` and query files from `queries/<language>/`.
|
Neovim loads compiled parser libraries from `parser/` directories on `runtimepath` and query files from `queries/<language>/`.
|
||||||
`tree-sitter-cli` is only needed by tools that generate or compile parsers from grammar sources; it is not required at runtime for highlighting.
|
`tree-sitter-cli` is only needed by the provisioner when installing or updating parsers; it is not required at runtime for highlighting.
|
||||||
Prefer providing parsers and queries through Neovim's runtime or a reproducible system/development environment.
|
Mason bootstraps `tree-sitter-cli` when parser installation is needed.
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
|
|
@ -99,6 +99,7 @@ Mason installs:
|
||||||
- **nvim-cmp** - completion
|
- **nvim-cmp** - completion
|
||||||
- **telescope.nvim** - fuzzy finder
|
- **telescope.nvim** - fuzzy finder
|
||||||
- **Native Treesitter** - syntax highlighting
|
- **Native Treesitter** - syntax highlighting
|
||||||
|
- **neovim-treesitter** - parser and query provisioning
|
||||||
- **nvim-dap** - debug adapter client
|
- **nvim-dap** - debug adapter client
|
||||||
- **nvim-dap-ui** - debugging UI panes
|
- **nvim-dap-ui** - debugging UI panes
|
||||||
- **mason-nvim-dap.nvim** - Mason debug adapter integration
|
- **mason-nvim-dap.nvim** - Mason debug adapter integration
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
"mason-lspconfig.nvim": { "branch": "main", "commit": "0c2823e0418f3d9230ff8b201c976e84de1cb401" },
|
"mason-lspconfig.nvim": { "branch": "main", "commit": "0c2823e0418f3d9230ff8b201c976e84de1cb401" },
|
||||||
"mason-nvim-dap.nvim": { "branch": "main", "commit": "9a10e096703966335bd5c46c8c875d5b0690dade" },
|
"mason-nvim-dap.nvim": { "branch": "main", "commit": "9a10e096703966335bd5c46c8c875d5b0690dade" },
|
||||||
"mason.nvim": { "branch": "main", "commit": "cb8445f8ce85d957416c106b780efd51c6298f89" },
|
"mason.nvim": { "branch": "main", "commit": "cb8445f8ce85d957416c106b780efd51c6298f89" },
|
||||||
|
"neovim-treesitter": { "branch": "main", "commit": "df7489eeea351bece7fd0f9c825be5cb6a1438f0" },
|
||||||
"nvim-dap": { "branch": "master", "commit": "45a69eba683a2c448dd9ecfc4de89511f0646b5f" },
|
"nvim-dap": { "branch": "master", "commit": "45a69eba683a2c448dd9ecfc4de89511f0646b5f" },
|
||||||
"nvim-lspconfig": { "branch": "master", "commit": "31026a13eefb20681124706a79fc1df6bf11ab27" },
|
"nvim-lspconfig": { "branch": "master", "commit": "31026a13eefb20681124706a79fc1df6bf11ab27" },
|
||||||
"nvim-web-devicons": { "branch": "master", "commit": "4fc505ac7bd7692824a142e96e5f529c133862f8" },
|
"nvim-web-devicons": { "branch": "master", "commit": "4fc505ac7bd7692824a142e96e5f529c133862f8" },
|
||||||
|
|
@ -18,5 +19,6 @@
|
||||||
"plenary.nvim": { "branch": "master", "commit": "74b06c6c75e4eeb3108ec01852001636d85a932b" },
|
"plenary.nvim": { "branch": "master", "commit": "74b06c6c75e4eeb3108ec01852001636d85a932b" },
|
||||||
"render-markdown.nvim": { "branch": "main", "commit": "3f3eea97b80839f629c951ca660ffd125bfa5b34" },
|
"render-markdown.nvim": { "branch": "main", "commit": "3f3eea97b80839f629c951ca660ffd125bfa5b34" },
|
||||||
"todo-comments.nvim": { "branch": "main", "commit": "31e3c38ce9b29781e4422fc0322eb0a21f4e8668" },
|
"todo-comments.nvim": { "branch": "main", "commit": "31e3c38ce9b29781e4422fc0322eb0a21f4e8668" },
|
||||||
|
"treesitter-parser-registry": { "branch": "main", "commit": "6eb15358bb9fc88f0d3401d8538d56652e9bdf3c" },
|
||||||
"which-key.nvim": { "branch": "main", "commit": "3aab2147e74890957785941f0c1ad87d0a44c15a" }
|
"which-key.nvim": { "branch": "main", "commit": "3aab2147e74890957785941f0c1ad87d0a44c15a" }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,12 @@
|
||||||
local treesitter_filetypes = {
|
local treesitter_languages = require("config.treesitter_languages")
|
||||||
"lua",
|
|
||||||
"c",
|
|
||||||
"cpp",
|
|
||||||
"python",
|
|
||||||
"nix",
|
|
||||||
"rust",
|
|
||||||
"bash",
|
|
||||||
"markdown",
|
|
||||||
"html",
|
|
||||||
"javascript",
|
|
||||||
"javascriptreact",
|
|
||||||
"css",
|
|
||||||
"vim",
|
|
||||||
"gitconfig",
|
|
||||||
"gitrebase",
|
|
||||||
"gitattributes",
|
|
||||||
"gitcommit",
|
|
||||||
"gitignore",
|
|
||||||
}
|
|
||||||
|
|
||||||
local language_by_filetype = {
|
for filetype, language in pairs(treesitter_languages.language_by_filetype) do
|
||||||
gitconfig = "git_config",
|
|
||||||
gitrebase = "git_rebase",
|
|
||||||
javascriptreact = "javascript",
|
|
||||||
}
|
|
||||||
|
|
||||||
for filetype, language in pairs(language_by_filetype) do
|
|
||||||
vim.treesitter.language.register(language, filetype)
|
vim.treesitter.language.register(language, filetype)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
group = vim.api.nvim_create_augroup("config_treesitter", { clear = true }),
|
group = vim.api.nvim_create_augroup("config_treesitter", { clear = true }),
|
||||||
pattern = treesitter_filetypes,
|
pattern = treesitter_languages.filetypes,
|
||||||
callback = function(event)
|
callback = function(event)
|
||||||
local language = vim.treesitter.language.get_lang(vim.bo[event.buf].filetype)
|
local language = vim.treesitter.language.get_lang(vim.bo[event.buf].filetype)
|
||||||
if not language then
|
if not language then
|
||||||
|
|
|
||||||
52
lua/config/treesitter_languages.lua
Normal file
52
lua/config/treesitter_languages.lua
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
M.install_parsers = {
|
||||||
|
"bash",
|
||||||
|
"cpp",
|
||||||
|
"css",
|
||||||
|
"git_config",
|
||||||
|
"git_rebase",
|
||||||
|
"gitattributes",
|
||||||
|
"gitcommit",
|
||||||
|
"gitignore",
|
||||||
|
"html",
|
||||||
|
"javascript",
|
||||||
|
"json",
|
||||||
|
"nix",
|
||||||
|
"python",
|
||||||
|
"rust",
|
||||||
|
"toml",
|
||||||
|
"yaml",
|
||||||
|
}
|
||||||
|
|
||||||
|
M.filetypes = {
|
||||||
|
"lua",
|
||||||
|
"c",
|
||||||
|
"cpp",
|
||||||
|
"python",
|
||||||
|
"nix",
|
||||||
|
"rust",
|
||||||
|
"bash",
|
||||||
|
"markdown",
|
||||||
|
"html",
|
||||||
|
"javascript",
|
||||||
|
"javascriptreact",
|
||||||
|
"css",
|
||||||
|
"json",
|
||||||
|
"toml",
|
||||||
|
"yaml",
|
||||||
|
"vim",
|
||||||
|
"gitconfig",
|
||||||
|
"gitrebase",
|
||||||
|
"gitattributes",
|
||||||
|
"gitcommit",
|
||||||
|
"gitignore",
|
||||||
|
}
|
||||||
|
|
||||||
|
M.language_by_filetype = {
|
||||||
|
gitconfig = "git_config",
|
||||||
|
gitrebase = "git_rebase",
|
||||||
|
javascriptreact = "javascript",
|
||||||
|
}
|
||||||
|
|
||||||
|
return M
|
||||||
90
lua/plugins/treesitter.lua
Normal file
90
lua/plugins/treesitter.lua
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
local treesitter_languages = require("config.treesitter_languages")
|
||||||
|
|
||||||
|
local function tree_sitter_cli_works()
|
||||||
|
if vim.fn.executable("tree-sitter") == 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local result = vim.system({ "tree-sitter", "--version" }, { text = true }):wait()
|
||||||
|
return result.code == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ensure_tree_sitter_cli(callback)
|
||||||
|
if tree_sitter_cli_works() then
|
||||||
|
callback()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if #vim.api.nvim_list_uis() == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local mason_ok, mason = pcall(require, "mason")
|
||||||
|
local registry_ok, registry = pcall(require, "mason-registry")
|
||||||
|
if not mason_ok or not registry_ok then
|
||||||
|
vim.notify_once("tree-sitter CLI is required to install Treesitter parsers", vim.log.levels.WARN)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
mason.setup()
|
||||||
|
registry.refresh(function()
|
||||||
|
local package_ok, package = pcall(registry.get_package, "tree-sitter-cli")
|
||||||
|
if not package_ok then
|
||||||
|
vim.notify_once("Mason registry does not include tree-sitter-cli", vim.log.levels.WARN)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if package:is_installed() then
|
||||||
|
if tree_sitter_cli_works() then
|
||||||
|
callback()
|
||||||
|
else
|
||||||
|
vim.notify_once("tree-sitter CLI is installed but cannot run", vim.log.levels.WARN)
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if package:is_installing() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
package:install({}, function(success, error)
|
||||||
|
if success and tree_sitter_cli_works() then
|
||||||
|
callback()
|
||||||
|
else
|
||||||
|
vim.notify("Failed to install tree-sitter-cli with Mason: " .. tostring(error), vim.log.levels.WARN)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
{
|
||||||
|
"neovim-treesitter/nvim-treesitter",
|
||||||
|
name = "neovim-treesitter",
|
||||||
|
dependencies = {
|
||||||
|
"neovim-treesitter/treesitter-parser-registry",
|
||||||
|
"williamboman/mason.nvim",
|
||||||
|
},
|
||||||
|
lazy = false,
|
||||||
|
build = ":TSUpdate",
|
||||||
|
config = function()
|
||||||
|
local treesitter = require("nvim-treesitter")
|
||||||
|
treesitter.setup()
|
||||||
|
|
||||||
|
local installed = treesitter.get_installed("parsers")
|
||||||
|
local missing = vim.iter(treesitter_languages.install_parsers)
|
||||||
|
:filter(function(parser)
|
||||||
|
return not vim.tbl_contains(installed, parser)
|
||||||
|
end)
|
||||||
|
:totable()
|
||||||
|
|
||||||
|
if #missing == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
ensure_tree_sitter_cli(function()
|
||||||
|
treesitter.install(missing)
|
||||||
|
end)
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue