From fc3f0041fbe01c96f38224d65d27664b22b5ef78 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 27 Sep 2026 11:40:30 -0400 Subject: fix(editor)!: operator target/cgn modifies Visual marks #42119 Problem: An operator on a target that creates a Visual selection (`cgn`, `dj`, an omap text object like `:normal! viw`) sets `'< '>`, `gv` and `visualmode()` to the operator target. This causes at least the following problems: - clobbers the user's previous `'< '>` (annoying and usually unwanted) - E.g. after a custom textobj, `:help text-object-define`. - "cgn" with a `\%V` pattern can't be dot-repeated (".") Solution: Don't save the Visual area if the operator target created it (operator was pending). Also fix `an`/`in` with `'selection'=exclusive` starting from an empty range. --- runtime/lua/vim/treesitter/_range.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/treesitter/_range.lua') diff --git a/runtime/lua/vim/treesitter/_range.lua b/runtime/lua/vim/treesitter/_range.lua index ab322e8188..1526acb8aa 100644 --- a/runtime/lua/vim/treesitter/_range.lua +++ b/runtime/lua/vim/treesitter/_range.lua @@ -186,6 +186,18 @@ function M.visual_select(range) end_col = #vim.fn.getline(end_row + 1) + 1 end + if vim.o.selection == 'exclusive' then + end_col = end_col + 1 + end + + if not vim.fn.mode():find('^[vV\22]') then + -- Operator-pending: select directly, not via "gv", to keep the user's '< '> (|omap-info|). + api.nvim_win_set_cursor(0, { start_row + 1, start_col }) + vim.cmd.normal({ 'v', bang = true }) + api.nvim_win_set_cursor(0, { end_row + 1, end_col - 1 }) + return + end + if vim.fn.visualmode() ~= 'v' then -- Reset visualmode() to 'v' vim.cmd.normal({ 'v\27', bang = true }) @@ -198,10 +210,6 @@ function M.visual_select(range) cursor_other_end_of_selection = true end - if vim.o.selection == 'exclusive' then - end_col = end_col + 1 - end - vim.fn.setpos("'<", { 0, start_row + 1, start_col + 1, 0 }) vim.fn.setpos("'>", { 0, end_row + 1, end_col, 0 }) -- cgit