Skip to content

Commit

Permalink
[FIX] Better support for the Visual Line Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
marqmitk committed Feb 23, 2024
1 parent 1a46426 commit 164c83e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lua/codesnap/utils/visual.lua
Original file line number Diff line number Diff line change
@@ -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(".")
Expand All @@ -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
Expand Down

0 comments on commit 164c83e

Please sign in to comment.