fix(treesitter): attach textobject keymaps to existing buffers on load. Closes #6642. Closes #6639

This commit is contained in:
Folke Lemaitre 2025-10-18 15:26:38 +02:00
parent 561da43c43
commit 92a7728732
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -19,7 +19,6 @@ return {
TS.update(nil, { summary = true }) TS.update(nil, { summary = true })
end) end)
end, end,
lazy = vim.fn.argc(-1) == 0, -- load treesitter early when opening a file from the cmdline
event = { "LazyFile", "VeryLazy" }, event = { "LazyFile", "VeryLazy" },
cmd = { "TSUpdate", "TSInstall", "TSLog", "TSUninstall" }, cmd = { "TSUpdate", "TSInstall", "TSLog", "TSUninstall" },
opts_extend = { "ensure_installed" }, opts_extend = { "ensure_installed" },
@ -162,10 +161,9 @@ return {
end end
TS.setup(opts) TS.setup(opts)
vim.api.nvim_create_autocmd("FileType", { local function attach(buf)
group = vim.api.nvim_create_augroup("lazyvim_treesitter_textobjects", { clear = true }), local ft = vim.bo[buf].filetype
callback = function(ev) if not (vim.tbl_get(opts, "move", "enable") and LazyVim.treesitter.have(ft, "textobjects")) then
if not (vim.tbl_get(opts, "move", "enable") and LazyVim.treesitter.have(ev.match, "textobjects")) then
return return
end end
---@type table<string, table<string, string>> ---@type table<string, table<string, string>>
@ -181,15 +179,22 @@ return {
vim.keymap.set({ "n", "x", "o" }, key, function() vim.keymap.set({ "n", "x", "o" }, key, function()
require("nvim-treesitter-textobjects.move")[method](query, "textobjects") require("nvim-treesitter-textobjects.move")[method](query, "textobjects")
end, { end, {
buffer = ev.buf, buffer = buf,
desc = desc, desc = desc,
silent = true, silent = true,
}) })
end end
end end
end end
end
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("lazyvim_treesitter_textobjects", { clear = true }),
callback = function(ev)
attach(ev.buf)
end, end,
}) })
vim.tbl_map(attach, vim.api.nvim_list_bufs())
end, end,
}, },