refactor(treesitter): use native neovim APIs

This commit is contained in:
Bryan Ramos 2026-05-01 08:59:38 -04:00
parent 6176457340
commit 34c52305af
5 changed files with 57 additions and 185 deletions

View file

@ -14,182 +14,7 @@ local mason_ensure_installed = {
"yamlls", -- YAML
}
local treesitter_parsers = {
"lua",
"c",
"cpp",
"python",
"nix",
"rust",
"bash",
"markdown",
"markdown_inline",
"html",
"javascript",
"css",
"vim",
"git_config",
"git_rebase",
"gitattributes",
"gitcommit",
"gitignore",
}
return {
{
"neovim-treesitter/nvim-treesitter",
name = "nvim-treesitter",
dependencies = {
"neovim-treesitter/treesitter-parser-registry",
"williamboman/mason.nvim",
},
lazy = false,
build = ":TSUpdate",
init = function()
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("config_treesitter", { clear = true }),
pattern = {
"lua",
"c",
"cpp",
"python",
"nix",
"rust",
"bash",
"markdown",
"html",
"javascript",
"javascriptreact",
"css",
"vim",
"gitconfig",
"gitrebase",
"gitattributes",
"gitcommit",
"gitignore",
},
callback = function(event)
if pcall(vim.treesitter.start, event.buf) then
vim.bo[event.buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end
end,
})
end,
config = function()
local treesitter = require("nvim-treesitter")
treesitter.setup()
local function has_c_compiler()
return vim.fn.executable("cc") == 1
or vim.fn.executable("gcc") == 1
or vim.fn.executable("clang") == 1
end
local function install_missing_parsers(missing)
if not has_c_compiler() then
vim.notify_once(
"A C compiler is required to install or update Treesitter parsers",
vim.log.levels.WARN
)
return
end
treesitter.install(missing)
end
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()
if result.code == 0 then
return true
end
vim.notify_once(
"tree-sitter CLI is installed but cannot run. On NixOS, enable nix-ld so Mason-installed binaries can execute.",
vim.log.levels.WARN
)
return false
end
local function ensure_treesitter_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 or update parsers; install tree-sitter-cli or enable mason.nvim",
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; install tree-sitter-cli before running :TSUpdate",
vim.log.levels.WARN
)
return
end
if package:is_installed() then
if tree_sitter_cli_works() then
callback()
end
return
end
if package:is_installing() then
vim.notify_once("tree-sitter-cli is already being installed by Mason", vim.log.levels.INFO)
return
end
vim.notify_once("Installing tree-sitter-cli with Mason for Treesitter parser updates", vim.log.levels.INFO)
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
local installed = treesitter.get_installed("parsers")
local missing = vim.iter(treesitter_parsers)
:filter(function(parser)
return not vim.tbl_contains(installed, parser)
end)
:totable()
if #missing == 0 then
return
end
ensure_treesitter_cli(function()
if #missing > 0 then
install_missing_parsers(missing)
end
end)
end
},
{
"m4xshen/autoclose.nvim",
config = function()