mirror of
https://github.com/neovim/neovim.git
synced 2026-08-27 09:31:47 +00:00
refactor: combine all visual_selection funcions
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -348,36 +348,6 @@ local function node_normalize_down(node)
|
||||
return node
|
||||
end
|
||||
|
||||
local function visual_select(range)
|
||||
assert(type(range) == 'table')
|
||||
local srow, scol, erow, ecol = Range.unpack4(range)
|
||||
local cursor_other_end_of_visual = false
|
||||
|
||||
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
|
||||
|
||||
if ecol == 0 then
|
||||
erow = erow - 1
|
||||
ecol = #vim.fn.getline(erow + 1) + 1
|
||||
end
|
||||
|
||||
if vim.fn.visualmode() ~= 'v' then
|
||||
-- reset visualmode() to 'v'
|
||||
vim.cmd.normal({ 'v\27', bang = true })
|
||||
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.cmd.normal({ 'gvo', bang = true })
|
||||
else
|
||||
vim.cmd.normal({ 'gv', bang = true })
|
||||
end
|
||||
end
|
||||
|
||||
--- @return Range4
|
||||
local function get_selection()
|
||||
local pos1 = vim.fn.getpos('v')
|
||||
@@ -596,7 +566,7 @@ local function repeate_apply_range(count, fn)
|
||||
end
|
||||
|
||||
if range and count ~= 0 then
|
||||
visual_select(range)
|
||||
Range.visual_select(range)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user