gemini/lua/gemini/init.lua

82 lines
2.1 KiB
Lua

-- lua/gemini/init.lua
local api = require("gemini.api")
local M = {}
local function gemini_query(prompt)
local response = api.get_response(prompt)
if response then
-- 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,
height = 20,
row = 5,
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)
end
end
-- Make gemini_query available in M so it can be used by the setup function
M.gemini_query = gemini_query
function M.setup()
-- Create the user command
vim.api.nvim_create_user_command("Gemini", function(opts)
local prompt = opts.args
if prompt == "" then
vim.notify("Please provide a prompt for Gemini.", vim.log.levels.WARN)
return
end
M.gemini_query(prompt) -- Use M.gemini_query instead of gemini_query
end, {
desc = "Query Google AI",
nargs = "+",
complete = "shellcmd",
})
-- Set up keymapping
vim.keymap.set("n", "<leader>g", function()
vim.ui.input({ prompt = "Gemini Query: " }, function(input)
if input then
M.gemini_query(input) -- Use M.gemini_query instead of gemini_query
end
end)
end, { desc = "Query Google AI (via Input)" })
end
return M