Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow pos="center", row=0, line=0 #227

Merged
merged 8 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions POPUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ stablization and any required features are merged into Neovim, we can upstream
this and expose the API in vimL to create better compatibility.

## Notices
- **2021-09-05:** we now follow Vim's convention of the first line/column of the screen being indexed 1, so that 0 can be used for centering.
- **2021-08-19:** we now follow Vim's default to `noautocmd` on popup creation. This can be overriden with `vim_options.noautocmd=false`

## List of Neovim Features Required:
Expand Down
81 changes: 43 additions & 38 deletions lua/plenary/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,43 @@ function popup.create(what, vim_options)
local win_opts = {}
win_opts.relative = "editor"

-- Feels like maxheight, minheight, maxwidth, minwidth will all be related
--
-- maxheight Maximum height of the contents, excluding border and padding.
-- minheight Minimum height of the contents, excluding border and padding.
-- maxwidth Maximum width of the contents, excluding border, padding and scrollbar.
-- minwidth Minimum width of the contents, excluding border, padding and scrollbar.
local width = vim_options.width or 1
local height
if type(what) == "number" then
height = vim.api.nvim_buf_line_count(what)
else
for _, v in ipairs(what) do
width = math.max(width, #v)
end
height = #what
end
win_opts.width = utils.bounded(width, vim_options.minwidth, vim_options.maxwidth)
win_opts.height = utils.bounded(height, vim_options.minheight, vim_options.maxheight)

-- pos
--
-- Using "topleft", "topright", "botleft", "botright" defines what corner of the popup "line"
-- and "col" are used for. When not set "topleft" behaviour is used.
-- Alternatively "center" can be used to position the popup in the center of the Neovim window,
-- in which case "line" and "col" are ignored.
if vim_options.pos then
if vim_options.pos == "center" then
vim_options.line = 0
vim_options.col = 0
win_opts.anchor = "NW"
else
win_opts.anchor = popup._pos_map[vim_options.pos]
end
else
win_opts.anchor = "NW" -- This is the default, but makes `posinvert` easier to implement
end

local cursor_relative_pos = function(pos_str, dim)
assert(string.find(pos_str, "^cursor"), "Invalid value for " .. dim)
win_opts.relative = "cursor"
Expand All @@ -110,37 +147,24 @@ function popup.create(what, vim_options)
return line
end

if vim_options.line then
if vim_options.line and vim_options.line ~= 0 then
if type(vim_options.line) == "string" then
win_opts.row = cursor_relative_pos(vim_options.line, "row")
else
win_opts.row = vim_options.line
win_opts.row = vim_options.line - 1
end
else
-- TODO: It says it needs to be "vertically cenetered"?...
-- wut is that.
win_opts.row = 0
win_opts.row = math.floor((vim.o.lines - win_opts.height) / 2)
end

if vim_options.col then
if vim_options.col and vim_options.col ~= 0 then
if type(vim_options.col) == "string" then
win_opts.col = cursor_relative_pos(vim_options.col, "col")
else
win_opts.col = vim_options.col
win_opts.col = vim_options.col - 1
end
else
-- TODO: It says it needs to be "horizontally cenetered"?...
win_opts.col = 0
end

if vim_options.pos then
if vim_options.pos == "center" then
-- TODO: Do centering..
else
win_opts.anchor = popup._pos_map[vim_options.pos]
end
else
win_opts.anchor = "NW" -- This is the default, but makes `posinvert` easier to implement
win_opts.col = math.floor((vim.o.columns - win_opts.width) / 2)
end

-- , fixed When FALSE (the default), and:
Expand All @@ -153,25 +177,6 @@ function popup.create(what, vim_options)

win_opts.style = "minimal"

-- Feels like maxheight, minheight, maxwidth, minwidth will all be related
--
-- maxheight Maximum height of the contents, excluding border and padding.
-- minheight Minimum height of the contents, excluding border and padding.
-- maxwidth Maximum width of the contents, excluding border, padding and scrollbar.
-- minwidth Minimum width of the contents, excluding border, padding and scrollbar.
local width = vim_options.width or 1
local height
if type(what) == "number" then
height = vim.api.nvim_buf_line_count(what)
else
for _, v in ipairs(what) do
width = math.max(width, #v)
end
height = #what
end
win_opts.width = utils.bounded(width, vim_options.minwidth, vim_options.maxwidth)
win_opts.height = utils.bounded(height, vim_options.minheight, vim_options.maxheight)

-- posinvert, When FALSE the value of "pos" is always used. When
-- , TRUE (the default) and the popup does not fit
-- , vertically and there is more space on the other side
Expand Down