aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/_editor.lua
diff options
context:
space:
mode:
authorNAKAI Tsuyoshi <82267684+uga-rosa@users.noreply.github.com>2023-04-11 23:28:46 +0900
committerGitHub <noreply@github.com>2023-04-11 16:28:46 +0200
commit9e86f473e0f4e21c5f40bf990c53194d593a0f9f (patch)
tree39f52084142ee87eb1e23a0aaa04eb337bca8f93 /runtime/lua/vim/_editor.lua
parentaab95ec67e4d80e63cc5c5acc42f3832e76e0781 (diff)
downloadrneovim-9e86f473e0f4e21c5f40bf990c53194d593a0f9f.tar.gz
rneovim-9e86f473e0f4e21c5f40bf990c53194d593a0f9f.tar.bz2
rneovim-9e86f473e0f4e21c5f40bf990c53194d593a0f9f.zip
feat(lua): vim.region accepts getpos() arg (#22635)
Diffstat (limited to 'runtime/lua/vim/_editor.lua')
-rw-r--r--runtime/lua/vim/_editor.lua22
1 files changed, 20 insertions, 2 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua
index fa0980563a..c922ec93db 100644
--- a/runtime/lua/vim/_editor.lua
+++ b/runtime/lua/vim/_editor.lua
@@ -402,8 +402,8 @@ end
--- Input and output positions are (0,0)-indexed and indicate byte positions.
---
---@param bufnr integer number of buffer
----@param pos1 integer[] (line, column) tuple marking beginning of region
----@param pos2 integer[] (line, column) tuple marking end of region
+---@param pos1 integer[]|string start of region as a (line, column) tuple or string accepted by |getpos()|
+---@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 inclusive boolean indicating whether column of pos2 is inclusive
---@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)
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
local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
pos1[1] = math.min(pos1[1], buf_line_count - 1)