From 980751083bc9e492d103a05e0ecbae60a070ccd2 Mon Sep 17 00:00:00 2001 From: Marco <95558991+marqmitk@users.noreply.github.com> Date: Sat, 24 Feb 2024 13:18:06 +0100 Subject: [PATCH] [Fix] Better support for the Visual Line Mode * [FIX] Better support for the Visual Line Mode * [FIX] Removed VBlockMode + listen for ModeChanged now --- lua/codesnap/utils/visual.lua | 19 +++++++++++++++++++ plugin/codesnap.lua | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/codesnap/utils/visual.lua b/lua/codesnap/utils/visual.lua index e06eb08..1c9afa0 100644 --- a/lua/codesnap/utils/visual.lua +++ b/lua/codesnap/utils/visual.lua @@ -1,5 +1,20 @@ local visual_utils = {} +-- Get all the lines from "from" to "to" and return them as a single string +-- If "from" and "to" are the same, return the line at "from" + +local function get_whole_lines(from, to) + local lines = {} + if from == to then + table.insert(lines, vim.api.nvim_buf_get_lines(0, from - 1, from, false)[1]) + else + for i = from, to do + table.insert(lines, vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]) + end + end + return table.concat(lines, "\n") +end + function visual_utils.get_selected_text() local start_pos = vim.fn.getpos("v") local end_pos = vim.fn.getpos(".") @@ -10,6 +25,10 @@ function visual_utils.get_selected_text() start_pos, end_pos = end_pos, start_pos end + if vim.api.nvim_get_mode().mode == 'V' then + return get_whole_lines(start_pos[2], end_pos[2]) + 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 diff --git a/plugin/codesnap.lua b/plugin/codesnap.lua index 5f81fe5..b09fd0a 100644 --- a/plugin/codesnap.lua +++ b/plugin/codesnap.lua @@ -12,9 +12,8 @@ end, {}) local validModes = { ["v"] = true, ["V"] = true, - ["^V"] = true, } -vim.api.nvim_create_autocmd({ "CursorMoved" }, { +vim.api.nvim_create_autocmd({ "CursorMoved", "ModeChanged" }, { callback = function() local mode = vim.api.nvim_get_mode().mode