Skip to content

Commit 732ab8e

Browse files
authored
feat: Add handling of cursor(+-<num>) for row and column (#2)
* Add handling of cursor(+-<num>) for row and column * Move common code to function Move common code to get cursore-relative position to a local function. Also use relative='cursor' for window options instead of computing relative to editor
1 parent c1bcf50 commit 732ab8e

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lua/popup/init.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,38 @@ function popup.create(what, vim_options)
9191
}
9292

9393
local win_opts = {}
94+
win_opts.relative = "editor"
95+
96+
local cursor_relative_pos = function(pos_str, dim)
97+
assert(string.find(pos_str,'^cursor'), "Invalid value for " .. dim)
98+
win_opts.relative = 'cursor'
99+
local line = 0
100+
if (pos_str):match "cursor%+(%d+)" then
101+
line = line + tonumber((pos_str):match "cursor%+(%d+)")
102+
elseif (pos_str):match "cursor%-(%d+)" then
103+
line = line - tonumber((pos_str):match "cursor%-(%d+)")
104+
end
105+
return line
106+
end
94107

95108
if vim_options.line then
96-
-- TODO: Need to handle "cursor", "cursor+1", ...
97-
win_opts.row = vim_options.line
109+
if type(vim_options.line) == 'string' then
110+
win_opts.row = cursor_relative_pos(vim_options.line, "row")
111+
else
112+
win_opts.row = vim_options.line
113+
end
98114
else
99115
-- TODO: It says it needs to be "vertically cenetered"?...
100116
-- wut is that.
101117
win_opts.row = 0
102118
end
103119

104120
if vim_options.col then
105-
-- TODO: Need to handle "cursor", "cursor+1", ...
106-
win_opts.col = vim_options.col
121+
if type(vim_options.col) == 'string' then
122+
win_opts.col = cursor_relative_pos(vim_options.col, "col")
123+
else
124+
win_opts.col = vim_options.col
125+
end
107126
else
108127
-- TODO: It says it needs to be "horizontally cenetered"?...
109128
win_opts.col = 0
@@ -157,8 +176,6 @@ function popup.create(what, vim_options)
157176
-- textpropwin
158177
-- textpropid
159178

160-
win_opts.relative = "editor"
161-
162179
local win_id
163180
if vim_options.hidden then
164181
assert(false, "I have not implemented this yet and don't know how")

0 commit comments

Comments
 (0)