Commonise floating-window logic (#41)

This commit is contained in:
Patrick Stevens
2024-03-26 09:31:39 +00:00
committed by GitHub
parent a734e7f73f
commit 07b3034bc0
4 changed files with 163 additions and 216 deletions

View File

@@ -65,108 +65,17 @@ function CreateVenv()
-- Install requirements
if requirements_path then
print("Installing requirements from " .. requirements_path)
local handle
local stdout = vim.uv.new_pipe(false)
local stderr = vim.uv.new_pipe(false)
local function on_output(context, prefix, err, data)
if err or data then
vim.schedule(function()
if err then
-- Append the error message to the buffer
local count = vim.api.nvim_buf_line_count(context.buf)
vim.api.nvim_buf_set_lines(
context.buf,
count,
count,
false,
{ "error " .. prefix .. ": " .. err }
)
end
if data then
-- Append the data to the buffer
local count = vim.api.nvim_buf_line_count(context.buf)
vim.api.nvim_buf_set_lines(
context.buf,
count,
count,
false,
vim.tbl_map(function(line)
return prefix .. ": " .. line
end, vim.split(data, "\n"))
)
end
if vim.api.nvim_win_is_valid(context.window) then
local cur_win = vim.api.nvim_get_current_win()
local cur_buf = vim.api.nvim_win_get_buf(cur_win)
if cur_buf ~= context.buf then
local new_line_count = vim.api.nvim_buf_line_count(context.buf)
vim.api.nvim_win_set_cursor(context.window, { new_line_count, 0 })
end
end
end)
end
end
-- TODO: commonise wth what's in ionide-vim
-- Create a new buffer for build output
local buf = vim.api.nvim_create_buf(false, true) -- No listed, scratch buffer
-- Calculate window size and position here (example: full width, 10 lines high at the bottom)
local width = vim.api.nvim_get_option_value("columns", {})
local height = vim.api.nvim_get_option_value("lines", {})
local win_height = math.min(10, math.floor(height * 0.2)) -- 20% of total height or 10 lines
local original_win = vim.api.nvim_get_current_win()
local win_opts = {
relative = "editor",
width = width,
height = win_height,
col = 0,
row = height - win_height,
style = "minimal",
border = "single",
}
local win = vim.api.nvim_open_win(buf, true, win_opts)
-- Switch back to the original window
vim.api.nvim_set_current_win(original_win)
local context = {
window = win,
buf = buf,
}
handle, _ = vim.uv.spawn(
-- TODO: do we need to escape this? Don't know whether spawn goes via a shell
local context = BuildUtils.create_window()
BuildUtils.run(
venv_dir .. "/bin/python",
{
-- TODO: and do we need to escape this?
args = { "-m", "pip", "install", "-r", requirements_path },
stdio = { nil, stdout, stderr },
},
vim.schedule_wrap(function(code, signal)
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
handle:close()
print("Venv creation completed, exit code " .. code .. " and signal " .. signal)
{ "-m", "pip", "install", "-r", requirements_path },
"venv creation",
context,
function(_, _, _) end,
function(_, _, _)
load_venv(venv_dir)
end)
end
)
if not handle then
print("Failed to start venv install process.")
return
end
vim.uv.read_start(stdout, function(err, data)
on_output(context, "OUT", err, data)
end)
vim.uv.read_start(stderr, function(err, data)
on_output(context, "ERR", err, data)
end)
else
load_venv(venv_dir)
end