Skip to content

Commit

Permalink
Add popup.close(), popup.list(), popup.hide(), popup.show()
Browse files Browse the repository at this point in the history
  • Loading branch information
errael committed Sep 27, 2024
1 parent 41c4d03 commit 3e64044
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions lua/plenary/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ popup._borders = {}
popup._callback_fn = {}

-- Result is passed to the callback. Indexed by win_id. See popup_win_closed.
-- Only active popups are in table; used to check if a win_id is an active popup.
popup._result = {}

local function dict_default(options, key, default)
Expand Down Expand Up @@ -120,7 +121,7 @@ end
---
---@param winnr integer window id of popup window
---@param bufnrs table|nil optional list of ignored buffers
local function close_window(winnr, bufnrs)
local function close_window_for_aucmd(winnr, bufnrs)
vim.schedule(function()
-- exit if we are in one of ignored buffers
if bufnrs and vim.list_contains(bufnrs, vim.api.nvim_get_current_buf()) then
Expand Down Expand Up @@ -149,7 +150,7 @@ local function close_window_autocmd(events, winnr, bufnrs)
vim.api.nvim_create_autocmd("BufEnter", {
group = augroup,
callback = function()
close_window(winnr, bufnrs)
close_window_for_aucmd(winnr, bufnrs)
end,
})

Expand All @@ -158,7 +159,7 @@ local function close_window_autocmd(events, winnr, bufnrs)
group = augroup,
buffer = bufnrs[2],
callback = function()
close_window(winnr)
close_window_for_aucmd(winnr)
end,
})
end
Expand Down Expand Up @@ -188,8 +189,10 @@ function popup.create(what, vim_options)
bufnr = vim.api.nvim_create_buf(false, true)
assert(bufnr, "Failed to create buffer")

vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
vim.api.nvim_set_option_value("bufhidden", "wipe", {buf = bufnr})
vim.api.nvim_set_option_value("modifiable", true, {buf = bufnr})
-- vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
-- vim.api.nvim_buf_set_option(bufnr, "modifiable", true)

-- TODO: Handle list of lines
if type(what) == "string" then
Expand Down Expand Up @@ -302,10 +305,9 @@ function popup.create(what, vim_options)

local win_id
if vim_options.hidden then
assert(false, "hidden: not implemented yet and don't know how")
else
win_id = vim.api.nvim_open_win(bufnr, false, win_opts)
win_opts.hide = vim_options.hidden
end
win_id = vim.api.nvim_open_win(bufnr, false, win_opts)

-- Set the default result. Also serves to indicate active popups.
popup._result[win_id] = -1
Expand Down Expand Up @@ -515,6 +517,55 @@ function popup.create(what, vim_options)
}
end

--- Close the specified popup window; the "result" is available through callback.
---
---@param win_id integer window id of popup window
---@param result any? value to return in a callback
function popup.close(win_id, result)
-- Only save the result if there is a popup with that window id.
assert(popup._result[win_id] ~= nil, "popup.close: no such popup window")
-- update the result as specified
if result == nil then
result = 0
end
popup._result[win_id] = result
Window.try_close(win_id, true)
end

--- Return a list of the window id of existing popups
---
---@return integer[]
function popup.list()
local ids = {}
for k, _ in pairs(popup._result) do
if type(k) == 'number' then
ids[#ids+1] = k
end
end
return ids
end

--- Hide the popup.
---
---@param win_id integer window id of popup window
function popup.hide(win_id)
if not vim.api.nvim_win_is_valid(win_id) then
return
end
assert(popup._result[win_id] ~= nil, "popup.hide: not a popup window")
vim.api.nvim_win_set_config(win_id, { hide = true })
end

--- Show the popup.
---
---@param win_id integer window id of popup window
function popup.show(win_id)
if not vim.api.nvim_win_is_valid(win_id) or not popup._result[win_id] then
return
end
vim.api.nvim_win_set_config(win_id, { hide = false })
end

-- Move popup with window id {win_id} to the position specified with {vim_options}.
-- {vim_options} may contain the following items that determine the popup position/size:
-- - line
Expand Down

0 comments on commit 3e64044

Please sign in to comment.