2022-05-05 17:38:24 +00:00
|
|
|
-- Luapopup - get a popup with lua buffer and run the code in neovim afterwards
|
2022-05-05 18:18:11 +00:00
|
|
|
-- Provides the same for fennel too
|
2022-05-05 17:38:24 +00:00
|
|
|
local popup = require("nui.popup")
|
|
|
|
local event = require("nui.utils.autocmd").event
|
2022-05-05 17:47:49 +00:00
|
|
|
local lua_utils = require("esensar.lua_utils")
|
2022-05-05 17:38:24 +00:00
|
|
|
|
2022-05-05 18:18:11 +00:00
|
|
|
local last_lua_win
|
|
|
|
local last_fennel_win
|
2022-05-05 17:38:24 +00:00
|
|
|
|
2022-05-05 18:18:11 +00:00
|
|
|
local popup_type = {
|
|
|
|
lua = 1,
|
|
|
|
fennel = 2,
|
|
|
|
}
|
|
|
|
local restore_commands = {
|
|
|
|
[popup_type.lua] = ":LuaPopupRestore",
|
|
|
|
[popup_type.fennel] = ":FennelPopupRestore",
|
|
|
|
}
|
|
|
|
local eval_functions = {
|
|
|
|
[popup_type.lua] = lua_utils.eval_lua_buffer,
|
|
|
|
[popup_type.fennel] = lua_utils.eval_fennel_buffer,
|
|
|
|
}
|
2022-05-07 10:33:53 +00:00
|
|
|
local validate_functions = {
|
|
|
|
[popup_type.lua] = lua_utils.is_valid_lua_code_buffer,
|
|
|
|
[popup_type.fennel] = function(_)
|
|
|
|
-- No validation for fennel
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
}
|
2022-05-05 18:18:11 +00:00
|
|
|
|
|
|
|
local function get_last_win(type)
|
|
|
|
if type == popup_type.lua then
|
|
|
|
return last_lua_win
|
|
|
|
elseif type == popup_type.fennel then
|
|
|
|
return last_fennel_win
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_last_win(type, win)
|
|
|
|
if type == popup_type.lua then
|
|
|
|
last_lua_win = win
|
|
|
|
elseif type == popup_type.fennel then
|
|
|
|
last_fennel_win = win
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function popup_and_run(type)
|
|
|
|
local last_win = get_last_win(type)
|
2022-05-05 17:38:24 +00:00
|
|
|
if last_win then
|
|
|
|
last_win:unmount()
|
|
|
|
end
|
|
|
|
|
|
|
|
local hidden = false
|
|
|
|
local win = popup({
|
|
|
|
enter = true,
|
|
|
|
focusable = true,
|
|
|
|
border = {
|
|
|
|
style = "rounded",
|
|
|
|
},
|
|
|
|
position = "50%",
|
|
|
|
size = {
|
|
|
|
width = "80%",
|
|
|
|
height = "60%",
|
|
|
|
},
|
|
|
|
buf_options = {
|
|
|
|
modifiable = true,
|
|
|
|
readonly = false,
|
|
|
|
filetype = "lua",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
local function cancel()
|
2022-05-05 18:18:11 +00:00
|
|
|
vim.notify("Cancelled operation, code not run! Use " .. restore_commands[type] .. " to restore buffer")
|
2022-05-05 17:38:24 +00:00
|
|
|
win:hide()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function run()
|
2022-05-05 18:18:11 +00:00
|
|
|
local st, r = eval_functions[type](win.bufnr)
|
2022-05-05 17:38:24 +00:00
|
|
|
if st == false then
|
2022-05-05 19:40:04 +00:00
|
|
|
vim.notify("Execution failed: \n" .. r, vim.log.levels.ERROR)
|
2022-05-05 17:38:24 +00:00
|
|
|
else
|
|
|
|
vim.notify("Successfully executed operation!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- mount/open the component
|
|
|
|
win:mount()
|
|
|
|
|
|
|
|
-- map keys
|
|
|
|
win:map("n", "<CR>", function(_)
|
2022-05-07 10:33:53 +00:00
|
|
|
local valid, message = validate_functions[type](win.bufnr)
|
|
|
|
if not valid then
|
|
|
|
vim.notify("Parsing code failed: \n" .. vim.inspect(message), vim.log.levels.ERROR)
|
|
|
|
else
|
|
|
|
hidden = true
|
|
|
|
win:hide()
|
|
|
|
run()
|
|
|
|
end
|
2022-05-05 17:38:24 +00:00
|
|
|
end, { noremap = true })
|
|
|
|
win:map("n", "<Esc>", function(_)
|
|
|
|
cancel()
|
|
|
|
end, { noremap = true })
|
|
|
|
|
|
|
|
-- unmount component when cursor leaves buffer
|
|
|
|
win:on(event.BufLeave, function()
|
|
|
|
-- Do not cancel if already hidden
|
|
|
|
if not hidden then
|
|
|
|
cancel()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-05-05 18:18:11 +00:00
|
|
|
set_last_win(type, win)
|
2022-05-05 17:38:24 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 18:18:11 +00:00
|
|
|
local function restore_popup(type)
|
|
|
|
get_last_win(type):show()
|
2022-05-05 17:38:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
vim.api.nvim_create_user_command("LuaPopup", function()
|
2022-05-05 18:18:11 +00:00
|
|
|
popup_and_run(popup_type.lua)
|
2022-05-12 19:12:01 +00:00
|
|
|
end, { desc = "Show a popup for writing lua code that will be executed after closing" })
|
2022-05-05 17:38:24 +00:00
|
|
|
|
|
|
|
vim.api.nvim_create_user_command("LuaPopupRestore", function()
|
2022-05-05 18:18:11 +00:00
|
|
|
if get_last_win(popup_type.lua) then
|
|
|
|
restore_popup(popup_type.lua)
|
2022-05-05 17:38:24 +00:00
|
|
|
else
|
|
|
|
vim.notify("No popup to restore! Use :LuaPopup first!", vim.log.levels.WARN)
|
|
|
|
end
|
2022-05-12 19:12:01 +00:00
|
|
|
end, { desc = "Restore last popup displayed with LuaPopup command" })
|
2022-05-05 18:18:11 +00:00
|
|
|
|
|
|
|
vim.api.nvim_create_user_command("FennelPopup", function()
|
|
|
|
popup_and_run(popup_type.fennel)
|
2022-05-12 19:12:01 +00:00
|
|
|
end, { desc = "Show a popup for writing fennel code that will be executed after closing" })
|
2022-05-05 18:18:11 +00:00
|
|
|
|
|
|
|
vim.api.nvim_create_user_command("FennelPopupRestore", function()
|
|
|
|
if get_last_win(popup_type.fennel) then
|
|
|
|
restore_popup(popup_type.fennel)
|
|
|
|
else
|
|
|
|
vim.notify("No popup to restore! Use :FennelPopup first!", vim.log.levels.WARN)
|
|
|
|
end
|
2022-05-12 19:12:01 +00:00
|
|
|
end, { desc = "Restore last popup displayed with FennelPopup command" })
|