From 1935486ff143dd5cf100b8c89204dc01d8e06021 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sat, 7 Oct 2023 15:06:13 +0200 Subject: [PATCH] fix(treesitter-textobjects): use normal ]c,]C,[c,[C when in diff-mode instead of goto class. Fixes #1610 --- lua/lazyvim/plugins/treesitter.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index 753cfb7d..ed8bf964 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -11,16 +11,26 @@ return { { "nvim-treesitter/nvim-treesitter-textobjects", config = function() - -- Disable class keymaps in diff mode - vim.api.nvim_create_autocmd("BufReadPost", { - callback = function(event) - if vim.wo.diff then - for _, key in ipairs({ "[c", "]c", "[C", "]C" }) do - pcall(vim.keymap.del, "n", key, { buffer = event.buf }) + -- When in diff mode, we want to use the default + -- vim text objects c & C instead of the treesitter ones. + local move = require("nvim-treesitter.textobjects.move") ---@type table + local configs = require("nvim-treesitter.configs") + for name, fn in pairs(move) do + if name:find("goto") == 1 then + move[name] = function(q, ...) + if vim.wo.diff then + local config = configs.get_module("textobjects.move")[name] ---@type table + for key, query in pairs(config or {}) do + if q == query and key:find("[%]%[][cC]") then + vim.cmd("normal! " .. key) + return + end + end end + return fn(q, ...) end - end, - }) + end + end end, }, },