diff options
| author | altermo <107814000+altermo@users.noreply.github.com> | 2026-08-01 18:04:09 +0200 |
|---|---|---|
| committer | altermo <107814000+altermo@users.noreply.github.com> | 2026-08-01 18:20:13 +0200 |
| commit | 77dfa02f3ec74555039a20b9c68ea9e27eebf03c (patch) | |
| tree | d9d4560d9c37c936817112937134bb33c996b7c6 /runtime/lua/vim/treesitter/_range.lua | |
| parent | fe8e6a7efa33dc9ea1c57aa3f65be9469f05f287 (diff) | |
| download | rneovim-77dfa02f3ec74555039a20b9c68ea9e27eebf03c.tar.gz rneovim-77dfa02f3ec74555039a20b9c68ea9e27eebf03c.tar.bz2 rneovim-77dfa02f3ec74555039a20b9c68ea9e27eebf03c.zip | |
refactor: combine all visual_selection funcions
Diffstat (limited to 'runtime/lua/vim/treesitter/_range.lua')
| -rw-r--r-- | runtime/lua/vim/treesitter/_range.lua | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/runtime/lua/vim/treesitter/_range.lua b/runtime/lua/vim/treesitter/_range.lua index b37135efc7..3f1238d036 100644 --- a/runtime/lua/vim/treesitter/_range.lua +++ b/runtime/lua/vim/treesitter/_range.lua @@ -172,4 +172,36 @@ function M.add_bytes(source, range) return { start_row, start_col, start_byte, end_row, end_col, end_byte } end +---@param range Range +function M.visual_select(range) + local start_row, start_col, end_row, end_col = M.unpack4(range) + + -- If the selection ends at column 0, adjust the position to the end of the previous line. + if end_col == 0 then + end_row = end_row - 1 + end_col = #vim.fn.getline(end_row + 1) + 1 + end + + if vim.fn.visualmode() ~= 'v' then + -- Reset visualmode() to 'v' + vim.cmd.normal({ 'v\27', bang = true }) + end + + local cursor_other_end_of_selection = false + local visual_col, visual_row = vim.fn.col('v'), vim.fn.line('v') + local cursor_col, cursor_row = vim.fn.col('.'), vim.fn.line('.') + if M.cmp_pos.gt(visual_row, visual_col, cursor_row, cursor_col) then + cursor_other_end_of_selection = true + end + + vim.fn.setpos("'<", { 0, start_row + 1, start_col + 1, 0 }) + vim.fn.setpos("'>", { 0, end_row + 1, end_col, 0 }) + + if cursor_other_end_of_selection then + vim.cmd.normal({ 'gvo', bang = true }) + else + vim.cmd.normal({ 'gv', bang = true }) + end +end + return M |