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