Enhance nvim-treesitter config: runtimepath, conditional installs, autotag

This commit is contained in:
RingOfStorms (Joshua Bell) 2026-01-22 17:27:00 -06:00
parent cc9a5dc0f5
commit 712ad71984

View file

@ -1,7 +1,7 @@
return { return {
"nvim-treesitter/nvim-treesitter", "nvim-treesitter/nvim-treesitter",
dependencies = { "windwp/nvim-ts-autotag" }, dependencies = { "windwp/nvim-ts-autotag" },
lazy = false, -- nvim-treesitter does not support lazy-loading per docs lazy = false,
build = ":TSUpdate", build = ":TSUpdate",
init = function() init = function()
U.cmd_executable("tree-sitter", { U.cmd_executable("tree-sitter", {
@ -11,39 +11,39 @@ return {
}) })
end, end,
config = function() config = function()
-- New nvim-treesitter API (post-rewrite) -- The new nvim-treesitter has queries under runtime/queries/
-- Setup is optional and only needed for non-default install directory -- We need to add this to runtimepath for vim.treesitter to find them
local ts = require("nvim-treesitter") local ts_path = vim.fn.stdpath("data") .. "/lazy/nvim-treesitter"
if NIX then
-- In nix mode, parsers are provided by the nix store, no installation needed -- Find the nvim-treesitter plugin path in nix store
if not NIX then for _, p in ipairs(vim.api.nvim_list_runtime_paths()) do
-- Install common parsers (async) if p:match("nvim%-treesitter%-[0-9]") or p:match("nvim%-treesitter%.[0-9]") then
ts.install({ ts_path = p
"bash", break
"c", end
"css", end
"dockerfile",
"go",
"html",
"javascript",
"json",
"lua",
"markdown",
"markdown_inline",
"nix",
"python",
"rust",
"svelte",
"toml",
"tsx",
"typescript",
"vim",
"vimdoc",
"yaml",
})
end end
-- Enable treesitter highlighting for all filetypes -- Add the runtime subdirectory to runtimepath if it exists and has queries
local runtime_path = ts_path .. "/runtime"
if vim.fn.isdirectory(runtime_path .. "/queries") == 1 then
vim.opt.runtimepath:prepend(runtime_path)
end
-- In non-nix mode, install parsers
if not NIX then
local ts = require("nvim-treesitter")
if ts.install then
ts.install({
"bash", "c", "css", "dockerfile", "go", "html", "javascript",
"json", "lua", "markdown", "markdown_inline", "nix", "python",
"rust", "svelte", "toml", "tsx", "typescript", "vim", "vimdoc", "yaml",
})
end
end
-- Enable treesitter highlighting via vim.treesitter.start()
-- This works with both nix-provided parsers and installed ones
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("myconfig-treesitter-highlight", { clear = true }), group = vim.api.nvim_create_augroup("myconfig-treesitter-highlight", { clear = true }),
callback = function(args) callback = function(args)
@ -61,19 +61,14 @@ return {
end end
-- Enable treesitter highlighting -- Enable treesitter highlighting
-- This will silently fail if no parser is available for the filetype
pcall(vim.treesitter.start, bufnr) pcall(vim.treesitter.start, bufnr)
end, end,
}) })
-- Enable treesitter-based folding -- Configure nvim-ts-autotag if available
vim.api.nvim_create_autocmd("FileType", { U.safeRequire("nvim-ts-autotag", function(autotag)
group = vim.api.nvim_create_augroup("myconfig-treesitter-fold", { clear = true }), autotag.setup()
callback = function(args) end)
local win = vim.api.nvim_get_current_win()
vim.wo[win][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.wo[win][0].foldmethod = "expr"
vim.wo[win][0].foldenable = false -- Start with folds open
end,
})
end, end,
} }