From 170ff4b24487e7350248d1a745b69979ada185ce Mon Sep 17 00:00:00 2001 From: altermo <107814000+altermo@users.noreply.github.com> Date: Wed, 25 Mar 2026 10:20:42 +0100 Subject: [PATCH] refactor(treesitter): use same visual-select as lsp #38475 Problem treesitter select over-complicates visual selection. Solution make it use same visual selection logic as lsp. --- runtime/lua/vim/treesitter/_select.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/runtime/lua/vim/treesitter/_select.lua b/runtime/lua/vim/treesitter/_select.lua index 38d3b32de6..4fdbf2bd9b 100644 --- a/runtime/lua/vim/treesitter/_select.lua +++ b/runtime/lua/vim/treesitter/_select.lua @@ -353,23 +353,23 @@ local function visual_select(range) local srow, scol, erow, ecol = Range.unpack4(range) local cursor_other_end_of_visual = false - if vim.fn.mode() == 'v' then - local vcol, vrow = vim.fn.col('v'), vim.fn.line('v') - local ccol, cline = vim.fn.col('.'), vim.fn.line('.') - if vrow > cline or (vrow == cline and vcol > ccol) then - cursor_other_end_of_visual = true - end + local vcol, vrow = vim.fn.col('v'), vim.fn.line('v') + local ccol, cline = vim.fn.col('.'), vim.fn.line('.') + if vrow > cline or (vrow == cline and vcol > ccol) then + cursor_other_end_of_visual = true end - vim.api.nvim_win_set_cursor(0, { srow + 1, scol }) - vim.api.nvim_feedkeys(vim.keycode('v'), 'nx', true) - - if not pcall(vim.api.nvim_win_set_cursor, 0, { erow + 1, ecol - 1 }) then - vim.api.nvim_win_set_cursor(0, { erow, #vim.fn.getline(erow) }) + if ecol == 0 then + erow = erow - 1 + ecol = #vim.fn.getline(erow + 1) + 1 end + vim.fn.setpos("'<", { 0, srow + 1, scol + 1, 0 }) + vim.fn.setpos("'>", { 0, erow + 1, ecol, 0 }) if cursor_other_end_of_visual then - vim.api.nvim_feedkeys('o', 'nx', true) + vim.cmd.normal({ 'gvo', bang = true }) + else + vim.cmd.normal({ 'gv', bang = true }) end end