Skip to content

Commit

Permalink
[Fix] Better support for the Visual Line Mode
Browse files Browse the repository at this point in the history
* [FIX] Better support for the Visual Line Mode

* [FIX] Removed VBlockMode + listen for ModeChanged now
  • Loading branch information
marqmitk authored Feb 24, 2024
1 parent d3fdcd3 commit 9807510
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 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
3 changes: 1 addition & 2 deletions plugin/codesnap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9807510

Please sign in to comment.