dot-files/dot_config/nvim/lua/plugins/treesitter.lua
Buddy 3e0402178c fix: resolve lazy.nvim lock crash and erb treesitter crash
Move lazy.nvim's own install outside the lazy plugin root so it's
treated as local and skipped during git-based lock file writes -
installing it as a tarball left it without a .git dir, which crashed
lazy's lock.update() on every sync (assert on Git.info of a non-git
dir).

Also patch nvim-treesitter's set-lang-from-mimetype! directive, which
crashes on any <script type="..."> tag because it reads match[id] as
a single node when Neovim core now always returns an array there.
Hooked on LazyDone so it overrides nvim-treesitter's own registration
instead of being clobbered by it. Added ruby/html/embedded_template
to ensure_installed so TSUpdate actually keeps those parsers current
instead of relying on one-time auto_install.
2026-10-24 14:45:26 -07:00

138 lines
3.8 KiB
Lua

local M = { "nvim-treesitter/nvim-treesitter" }
M.name = "treesitter"
M.build = ":TSUpdate"
M.dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects"
}
function M.config ()
vim.defer_fn(M.treesitter_configs, 0)
end
function M.treesitter_configs()
require("nvim-treesitter.configs").setup(M.treesitter_config_setup)
end
-- Works around a crash in nvim-treesitter's bundled `set-lang-from-mimetype!`
-- directive (queries/html_tags/injections.scm, used by html/erb/vue/svelte):
-- it reads `match[capture_id]` as a single TSNode, but current Neovim core
-- always returns an array of nodes there, so `<script type="...">` tags
-- crash the whole buffer's treesitter parse. Re-register the directive with
-- the same behavior, reading the first node out of the array instead.
--
-- Must run after nvim-treesitter's own `plugin/query_predicates.lua` has
-- registered its (broken) version, or that clobbers this one right back.
-- `LazyDone` fires once after all plugins (incl. nvim-treesitter's own
-- plugin/ scripts) have finished loading, so it's the reliable hook.
vim.api.nvim_create_autocmd("User", {
pattern = "LazyDone",
once = true,
callback = function()
M.patch_mimetype_directive()
end,
})
function M.patch_mimetype_directive()
local html_script_type_languages = {
["importmap"] = "json",
["module"] = "javascript",
["application/ecmascript"] = "javascript",
["text/ecmascript"] = "javascript",
}
vim.treesitter.query.add_directive("set-lang-from-mimetype!", function(match, _, bufnr, pred, metadata)
local nodes = match[pred[2]]
local node = nodes and nodes[1]
if not node then
return
end
local type_attr_value = vim.treesitter.get_node_text(node, bufnr)
local configured = html_script_type_languages[type_attr_value]
if configured then
metadata["injection.language"] = configured
else
local parts = vim.split(type_attr_value, "/", {})
metadata["injection.language"] = parts[#parts]
end
end, { force = true })
end
M.treesitter_config_setup = {
-- Add languages to be installed here that you want installed for treesitter
ensure_installed = {
"bash", "c", "cpp", "go", "lua", "python", "rust",
"tsx", "javascript", "typescript", "vimdoc", "vim",
"ruby", "html", "embedded_template",
},
auto_install = true,
sync_install = false,
modules = {},
ignore_install = {},
highlight = { enable = true },
indent = { enable = true },
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<c-space>",
node_incremental = "<c-space>",
scope_incremental = "<c-s>",
node_decremental = "<M-space>",
},
},
textobjects = {
select = {
enable = true,
-- Automatically jump forward to textobj, similar to targets.vim
lookahead = true,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
},
},
swap = {
enable = true,
swap_next = {
["<leader>a"] = "@parameter.inner",
},
swap_previous = {
["<leader>A"] = "@parameter.inner",
},
},
move = {
enable = true,
-- whether to set jumps in the jumplist
set_jumps = true,
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
},
},
}
return M