Skip to content

Commit

Permalink
fix(utils): Add backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AgatZan committed Nov 7, 2024
1 parent 13a1fe0 commit b0e7afb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
28 changes: 27 additions & 1 deletion lua/scratch/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,35 @@ local table_length = function(T)
return count
end

local getregion = vim.fn.has("nvim-0.10") == 1 and vim.fn.getregion
or function(pos1, pos2, opts)
local lines = {}
local start_row, start_col, end_row, end_col = pos1[2], pos1[3], pos2[2], pos2[3]
local selection_mode = opts.type
local text = vim.api.nvim_buf_get_lines(0, start_row, end_row, true)
end_row = end_row - start_row + 1
start_row = 1
if selection_mode == "v" then
table.insert(lines, text[1]:sub(start_col))
for i = start_row + 1, end_row do
table.insert(lines, text[i])
end
lines[end_row] = lines[end_row]:sub(1, end_col)
elseif selection_mode == "V" then
for i = start_row, end_row do
table.insert(lines, text[i])
end
elseif selection_mode == vim.api.nvim_replace_termcodes("<C-V>", true, true, true) then
for i = start_row, end_row do
table.insert(lines, text[i]:sub(start_col, end_col))
end
end
return lines
end
---@return string[]
local function getSelectedText(mark, mode)
return vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos(mark), { type = mode })
mode = vim.api.nvim_replace_termcodes(mode, true, true, true)
return getregion(vim.fn.getpos("v"), vim.fn.getpos(mark), { type = mode })
end

---@param msg string
Expand Down
14 changes: 8 additions & 6 deletions tests/test_select.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local child = MiniTest.new_child_neovim()
local getSelectedText
local function table_select(text, coord, selection_mode)
local lines = {}
local start_row, start_col, end_row, end_col = coord[1], coord[2], coord[3], coord[4]
Expand Down Expand Up @@ -34,11 +35,11 @@ local select_wise = function(coord, selection_mode)
vim.api.nvim_win_set_cursor(0, { end_row, end_col - 1 })
end

local function new_real(mark, mode)
mode = vim.api.nvim_replace_termcodes(mode, true, true, true)
local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos(mark), { type = mode })
return lines
end
-- local function new_real(mark, mode)
-- mode = vim.api.nvim_replace_termcodes(mode, true, true, true)
-- local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos(mark), { type = mode })
-- return lines
-- end
local function old_real(mark, mode)
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
Expand Down Expand Up @@ -99,6 +100,7 @@ T["param"]["new"] = new_set({
pre_case = function()
child.restart({ "-u", "scripts/minimal_init.lua" })
child.api.nvim_buf_set_lines(0, 0, -1, false, BUFFER_TEXT)
getSelectedText = require("scratch.utils").getSelectedText
end,
post_once = function()
child.stop()
Expand All @@ -116,7 +118,7 @@ T["param"]["new"]["workd"] = function(selection_mode, coord)
select_wise(coord, selection_mode)
MiniTest.expect.equality(
table_select(BUFFER_TEXT, coord, selection_mode),
child.lua_func(new_real, ".", selection_mode)
child.lua_func(getSelectedText, ".", selection_mode)
)
end

Expand Down

0 comments on commit b0e7afb

Please sign in to comment.