From 600c04437c99852ee7bae7129c67a43cb6b6fbce Mon Sep 17 00:00:00 2001 From: Marco <95558991+marqmitk@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:34:51 +0100 Subject: [PATCH] [Fix] Fixed left to right (#11) Fixed reversed selections not only working from bottom to top --- lua/codesnap/utils/visual.lua | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lua/codesnap/utils/visual.lua b/lua/codesnap/utils/visual.lua index 4ba6e2d..e2225ee 100644 --- a/lua/codesnap/utils/visual.lua +++ b/lua/codesnap/utils/visual.lua @@ -4,18 +4,17 @@ function visual_utils.get_selected_text() local start_pos = vim.fn.getpos("v") 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 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 - + local selected_text = {} 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]