Skip to content

Commit 6fae20a

Browse files
authored
feat: implement posinvert (#16)
* feat: implement `posinvert` * fix: tweak nvim's `anchor` setting * docs: update readme
1 parent 5e3bece commit 6fae20a

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Suported Features:
7777
- [x] padding
7878
- [?] pos
7979
- Somewhat implemented. Doesn't work with borders though.
80+
- [x] posinvert
8081
- [x] time
8182
- [x] title
8283
- [x] wrap
@@ -87,7 +88,6 @@ Suported Features:
8788
- firstline
8889
- hidden
8990
- ~ pos
90-
- posinvert
9191
- fixed
9292
- filter
9393
- filtermode

Diff for: lua/popup/init.lua

+22-10
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,8 @@ function popup.create(what, vim_options)
136136
else
137137
win_opts.anchor = popup._pos_map[vim_options.pos]
138138
end
139-
end
140-
141-
-- posinvert, When FALSE the value of "pos" is always used. When
142-
-- , TRUE (the default) and the popup does not fit
143-
-- , vertically and there is more space on the other side
144-
-- , then the popup is placed on the other side of the
145-
-- , position indicated by "line".
146-
if dict_default(vim_options, 'posinvert', option_defaults) then
147-
-- TODO: handle the invert thing
139+
else
140+
win_opts.anchor = "NW" -- This is the default, but makes `posinvert` easier to implement
148141
end
149142

150143
-- , fixed When FALSE (the default), and:
@@ -157,7 +150,7 @@ function popup.create(what, vim_options)
157150

158151
win_opts.style = 'minimal'
159152

160-
-- Feels like maxheigh, minheight, maxwidth, minwidth will all be related
153+
-- Feels like maxheight, minheight, maxwidth, minwidth will all be related
161154
--
162155
-- maxheight Maximum height of the contents, excluding border and padding.
163156
-- minheight Minimum height of the contents, excluding border and padding.
@@ -176,6 +169,25 @@ function popup.create(what, vim_options)
176169
win_opts.width = utils.bounded(width, vim_options.minwidth, vim_options.maxwidth)
177170
win_opts.height = utils.bounded(height, vim_options.minheight, vim_options.maxheight)
178171

172+
-- posinvert, When FALSE the value of "pos" is always used. When
173+
-- , TRUE (the default) and the popup does not fit
174+
-- , vertically and there is more space on the other side
175+
-- , then the popup is placed on the other side of the
176+
-- , position indicated by "line".
177+
if dict_default(vim_options, 'posinvert', option_defaults) then
178+
if win_opts.anchor == "NW" or win_opts.anchor == "NE" then
179+
if win_opts.row + win_opts.height > vim.o.lines and win_opts.row * 2 > vim.o.lines then
180+
-- Don't know why, but this is how vim adjusts it
181+
win_opts.row = win_opts.row - win_opts.height - 2
182+
end
183+
elseif win_opts.anchor == "SW" or win_opts.anchor == "SE" then
184+
if win_opts.row - win_opts.height < 0 and win_opts.row * 2 < vim.o.lines then
185+
-- Don't know why, but this is how vim adjusts it
186+
win_opts.row = win_opts.row + win_opts.height + 2
187+
end
188+
end
189+
end
190+
179191
-- textprop, When present the popup is positioned next to a text
180192
-- , property with this name and will move when the text
181193
-- , property moves. Use an empty string to remove. See

0 commit comments

Comments
 (0)