Add fennel support to luapopup thing

main
Ensar Sarajčić 2022-05-05 20:18:11 +02:00
parent 6a67d9251f
commit 47f625593d
2 changed files with 69 additions and 10 deletions

View File

@ -1,3 +1,5 @@
local aeval = require("aniseed.eval")
local M = {}
function M.eval_lua_buffer(bufnr)
@ -19,4 +21,13 @@ function M.eval_lua_code(code_string)
return unpack(res)
end
function M.eval_fennel_buffer(bufnr)
local buffer_content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
return M.eval_fennel_code(table.concat(buffer_content, "\n"))
end
function M.eval_fennel_code(code_string)
return aeval.str(code_string, {})
end
return M

View File

@ -1,11 +1,44 @@
-- Luapopup - get a popup with lua buffer and run the code in neovim afterwards
-- Provides the same for fennel too
local popup = require("nui.popup")
local event = require("nui.utils.autocmd").event
local lua_utils = require("esensar.lua_utils")
local last_win
local last_lua_win
local last_fennel_win
local function popup_and_run()
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,
}
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)
if last_win then
last_win:unmount()
end
@ -29,12 +62,15 @@ local function popup_and_run()
},
})
local function cancel()
vim.notify("Cancelled operation, code not run! Use :LuaPopupRestore to restore buffer")
vim.notify("Cancelled operation, code not run! Use " .. restore_commands[type] .. " to restore buffer")
win:hide()
end
local function run()
local st, r = lua_utils.eval_lua_buffer(win.bufnr)
print(vim.inspect(eval_functions))
print(vim.inspect(eval_functions[type]))
print(vim.inspect(eval_functions[type](win.bufnr)))
local st, r = eval_functions[type](win.bufnr)
if st == false then
vim.notify("Execution failed: \n" .. r)
else
@ -63,21 +99,33 @@ local function popup_and_run()
end
end)
last_win = win
set_last_win(type, win)
end
local function restore_popup()
last_win:show()
local function restore_popup(type)
get_last_win(type):show()
end
vim.api.nvim_create_user_command("LuaPopup", function()
popup_and_run()
popup_and_run(popup_type.lua)
end, {})
vim.api.nvim_create_user_command("LuaPopupRestore", function()
if last_win then
restore_popup()
if get_last_win(popup_type.lua) then
restore_popup(popup_type.lua)
else
vim.notify("No popup to restore! Use :LuaPopup first!", vim.log.levels.WARN)
end
end, {})
vim.api.nvim_create_user_command("FennelPopup", function()
popup_and_run(popup_type.fennel)
end, {})
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
end, {})