forked from mirror/codesnap.nvim
a9ff9852be
Changes made: Now, when users select upward, I swapped start and end positions to match. Also, I noticed a bug in visual block mode, you always show the whole line instead of just the selected characters. Couldn't figure out how to fix it though, since I'm not too familiar with the Vim API :(
37 lines
1.3 KiB
Lua
37 lines
1.3 KiB
Lua
local visual_utils = {}
|
|
|
|
function visual_utils.get_selected_text()
|
|
local start_pos = vim.fn.getpos("v")
|
|
local end_pos = vim.fn.getpos(".")
|
|
|
|
if start_pos[2] == end_pos[2] then
|
|
return vim.api.nvim_buf_get_lines(0, start_pos[2] - 1, start_pos[2], false)[1]:sub(start_pos[3], end_pos[3] - 1)
|
|
else
|
|
-- 如果选中的是多行文本,则需要分别获取每一行的文本
|
|
local selected_text = {}
|
|
|
|
-- We switch the start and end positions if the start position is after the end position
|
|
-- This way we can always select from the top down
|
|
if start_pos[2] > end_pos[2] then
|
|
start_pos, end_pos = end_pos, start_pos
|
|
end
|
|
|
|
for i = start_pos[2], end_pos[2] do
|
|
-- 使用 vim.api.nvim_buf_get_lines() 函数获取选中的文本
|
|
local line_text = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]
|
|
-- 如果是选中的第一行,需要从 mark 'v' 的列开始获取
|
|
if i == start_pos[2] then
|
|
line_text = line_text:sub(start_pos[3])
|
|
end
|
|
-- 如果是选中的最后一行,需要获取到当前光标的列
|
|
if i == end_pos[2] then
|
|
line_text = line_text:sub(1, end_pos[3] - 1)
|
|
end
|
|
table.insert(selected_text, line_text)
|
|
end
|
|
-- 输出当前选中的文本
|
|
return table.concat(selected_text, "\n")
|
|
end
|
|
end
|
|
|
|
return visual_utils
|