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