Make it possible to close the response window

This commit is contained in:
Jonas Widen 2025-03-16 14:27:10 +01:00
parent 18b4d06508
commit 4533db1784

@ -7,9 +7,15 @@ local function gemini_query(prompt)
local response = api.get_response(prompt)
if response then
-- Display the response in a new buffer
local new_buf = vim.api.nvim_create_buf(false, true) -- Create a scratch buffer
-- Create a scratch buffer
local new_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(new_buf, 0, 0, false, vim.split(response, "\n"))
-- Set buffer options
vim.api.nvim_buf_set_option(new_buf, 'modifiable', false)
vim.api.nvim_buf_set_option(new_buf, 'buftype', 'nofile')
-- Create the window
local new_win = vim.api.nvim_open_win(new_buf, true, {
relative = "editor",
width = 80,
@ -18,6 +24,26 @@ local function gemini_query(prompt)
col = vim.o.columns / 2 - 40,
border = "rounded",
title = "Google AI Response",
style = "minimal"
})
-- Set window-local keymaps
local close_keys = {'q', '<Esc>', '<CR>'}
for _, key in ipairs(close_keys) do
vim.keymap.set('n', key, function()
vim.api.nvim_win_close(new_win, true)
end, { buffer = new_buf, nowait = true })
end
-- Add autocmd to enable closing with :q
vim.api.nvim_create_autocmd("BufWinLeave", {
buffer = new_buf,
callback = function()
if vim.api.nvim_win_is_valid(new_win) then
vim.api.nvim_win_close(new_win, true)
end
end,
once = true,
})
else
vim.notify("Failed to get a response from Gemini API", vim.log.levels.ERROR)