[Fix] Fixed left to right (#11)

Fixed reversed selections not only working from bottom to top
pull/13/head
Marco 2024-02-23 10:34:51 +01:00 committed by GitHub
parent b9ee4897fa
commit 600c04437c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 8 deletions

View File

@ -4,18 +4,17 @@ function visual_utils.get_selected_text()
local start_pos = vim.fn.getpos("v") local start_pos = vim.fn.getpos("v")
local end_pos = vim.fn.getpos(".") local end_pos = vim.fn.getpos(".")
-- We switch the start and end positions if the start is after the end line or character
-- This way we can always select from the top down and from left to right
if start_pos[2] > end_pos[2] or start_pos[3] > end_pos[3] then
start_pos, end_pos = end_pos, start_pos
end
if start_pos[2] == end_pos[2] then 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) 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 else
-- 如果选中的是多行文本,则需要分别获取每一行的文本 -- 如果选中的是多行文本,则需要分别获取每一行的文本
local selected_text = {} 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 for i = start_pos[2], end_pos[2] do
-- 使用 vim.api.nvim_buf_get_lines() 函数获取选中的文本 -- 使用 vim.api.nvim_buf_get_lines() 函数获取选中的文本
local line_text = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1] local line_text = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]