mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
feat(lua): vim.region accepts getpos() arg (#22635)
This commit is contained in:
@@ -1516,9 +1516,10 @@ region({bufnr}, {pos1}, {pos2}, {regtype}, {inclusive}) *vim.region()*
|
|||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {bufnr} (integer) number of buffer
|
• {bufnr} (integer) number of buffer
|
||||||
• {pos1} integer[] (line, column) tuple marking beginning of
|
• {pos1} integer[]|string start of region as a (line, column)
|
||||||
region
|
tuple or string accepted by |getpos()|
|
||||||
• {pos2} integer[] (line, column) tuple marking end of region
|
• {pos2} integer[]|string end of region as a (line, column) tuple
|
||||||
|
or string accepted by |getpos()|
|
||||||
• {regtype} (string) type of selection, see |setreg()|
|
• {regtype} (string) type of selection, see |setreg()|
|
||||||
• {inclusive} (boolean) indicating whether column of pos2 is inclusive
|
• {inclusive} (boolean) indicating whether column of pos2 is inclusive
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@ CHANGED FEATURES *news-changed*
|
|||||||
|
|
||||||
The following changes to existing APIs or features add new behavior.
|
The following changes to existing APIs or features add new behavior.
|
||||||
|
|
||||||
• ...
|
• |vim.region()| can use a string accepted by |getpos()| as position.
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
REMOVED FEATURES *news-removed*
|
REMOVED FEATURES *news-removed*
|
||||||
|
@@ -402,8 +402,8 @@ end
|
|||||||
--- Input and output positions are (0,0)-indexed and indicate byte positions.
|
--- Input and output positions are (0,0)-indexed and indicate byte positions.
|
||||||
---
|
---
|
||||||
---@param bufnr integer number of buffer
|
---@param bufnr integer number of buffer
|
||||||
---@param pos1 integer[] (line, column) tuple marking beginning of region
|
---@param pos1 integer[]|string start of region as a (line, column) tuple or string accepted by |getpos()|
|
||||||
---@param pos2 integer[] (line, column) tuple marking end of region
|
---@param pos2 integer[]|string end of region as a (line, column) tuple or string accepted by |getpos()|
|
||||||
---@param regtype string type of selection, see |setreg()|
|
---@param regtype string type of selection, see |setreg()|
|
||||||
---@param inclusive boolean indicating whether column of pos2 is inclusive
|
---@param inclusive boolean indicating whether column of pos2 is inclusive
|
||||||
---@return table region Table of the form `{linenr = {startcol,endcol}}`.
|
---@return table region Table of the form `{linenr = {startcol,endcol}}`.
|
||||||
@@ -414,6 +414,24 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
|
|||||||
vim.fn.bufload(bufnr)
|
vim.fn.bufload(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if type(pos1) == 'string' then
|
||||||
|
local pos = vim.fn.getpos(pos1)
|
||||||
|
pos1 = { pos[2] - 1, pos[3] - 1 + pos[4] }
|
||||||
|
end
|
||||||
|
if type(pos2) == 'string' then
|
||||||
|
local pos = vim.fn.getpos(pos2)
|
||||||
|
pos2 = { pos[2] - 1, pos[3] - 1 + pos[4] }
|
||||||
|
end
|
||||||
|
|
||||||
|
if pos1[1] > pos2[1] or (pos1[1] == pos2[1] and pos1[2] > pos2[2]) then
|
||||||
|
pos1, pos2 = pos2, pos1
|
||||||
|
end
|
||||||
|
|
||||||
|
-- getpos() may return {0,0,0,0}
|
||||||
|
if pos1[1] < 0 or pos1[2] < 0 then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
-- check that region falls within current buffer
|
-- check that region falls within current buffer
|
||||||
local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
|
local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
|
||||||
pos1[1] = math.min(pos1[1], buf_line_count - 1)
|
pos1[1] = math.min(pos1[1], buf_line_count - 1)
|
||||||
|
@@ -15,8 +15,8 @@ M.priorities = {
|
|||||||
---@param bufnr integer Buffer number to apply highlighting to
|
---@param bufnr integer Buffer number to apply highlighting to
|
||||||
---@param ns integer Namespace to add highlight to
|
---@param ns integer Namespace to add highlight to
|
||||||
---@param higroup string Highlight group to use for highlighting
|
---@param higroup string Highlight group to use for highlighting
|
||||||
---@param start { [1]: integer, [2]: integer } Start position {line, col}
|
---@param start integer[]|string Start of region as a (line, column) tuple or string accepted by |getpos()|
|
||||||
---@param finish { [1]: integer, [2]: integer } Finish position {line, col}
|
---@param finish integer[]|string End of region as a (line, column) tuple or string accepted by |getpos()|
|
||||||
---@param opts table|nil Optional parameters
|
---@param opts table|nil Optional parameters
|
||||||
-- - regtype type of range (see |setreg()|, default charwise)
|
-- - regtype type of range (see |setreg()|, default charwise)
|
||||||
-- - inclusive boolean indicating whether the range is end-inclusive (default false)
|
-- - inclusive boolean indicating whether the range is end-inclusive (default false)
|
||||||
@@ -27,11 +27,6 @@ function M.range(bufnr, ns, higroup, start, finish, opts)
|
|||||||
local inclusive = opts.inclusive or false
|
local inclusive = opts.inclusive or false
|
||||||
local priority = opts.priority or M.priorities.user
|
local priority = opts.priority or M.priorities.user
|
||||||
|
|
||||||
-- sanity check
|
|
||||||
if start[2] < 0 or finish[1] < start[1] then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local region = vim.region(bufnr, start, finish, regtype, inclusive)
|
local region = vim.region(bufnr, start, finish, regtype, inclusive)
|
||||||
for linenr, cols in pairs(region) do
|
for linenr, cols in pairs(region) do
|
||||||
local end_row
|
local end_row
|
||||||
@@ -104,18 +99,12 @@ function M.on_yank(opts)
|
|||||||
yank_timer:close()
|
yank_timer:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos1 = vim.fn.getpos("'[")
|
|
||||||
local pos2 = vim.fn.getpos("']")
|
|
||||||
|
|
||||||
pos1 = { pos1[2] - 1, pos1[3] - 1 + pos1[4] }
|
|
||||||
pos2 = { pos2[2] - 1, pos2[3] - 1 + pos2[4] }
|
|
||||||
|
|
||||||
M.range(
|
M.range(
|
||||||
bufnr,
|
bufnr,
|
||||||
yank_ns,
|
yank_ns,
|
||||||
higroup,
|
higroup,
|
||||||
pos1,
|
"'[",
|
||||||
pos2,
|
"']",
|
||||||
{ regtype = event.regtype, inclusive = event.inclusive, priority = M.priorities.user }
|
{ regtype = event.regtype, inclusive = event.inclusive, priority = M.priorities.user }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -2297,6 +2297,10 @@ describe('lua stdlib', function()
|
|||||||
insert([[αα]])
|
insert([[αα]])
|
||||||
eq({0,5}, exec_lua[[ return vim.region(0,{0,0},{0,4},'3',true)[0] ]])
|
eq({0,5}, exec_lua[[ return vim.region(0,{0,0},{0,4},'3',true)[0] ]])
|
||||||
end)
|
end)
|
||||||
|
it('getpos() input', function()
|
||||||
|
insert('getpos')
|
||||||
|
eq({0,6}, exec_lua[[ return vim.region(0,{0,0},'.','v',true)[0] ]])
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('vim.on_key', function()
|
describe('vim.on_key', function()
|
||||||
|
Reference in New Issue
Block a user