@@ -1,6 +1,7 @@
11 local M = {}
22
33 local PROMPT_PLACEHOLDER = '{prompt}'
4+local active_prompt
45
56 local config = {
67 command = {
@@ -138,6 +139,166 @@ local function run_llm_command(argv)
138139 return output
139140 end
140141
142+local function notify_error(message)
143+ vim.notify('ai-complete: ' .. message, vim.log.levels.ERROR)
144+end
145+
146+local function has_visual_range(opts)
147+ if opts.range ~= 2 then
148+ return false
149+ end
150+
151+ local start_mark = vim.fn.getpos("'<")
152+ local end_mark = vim.fn.getpos("'>")
153+
154+ return start_mark[2] > 0
155+ and end_mark[2] > 0
156+ and opts.line1 == start_mark[2]
157+ and opts.line2 == end_mark[2]
158+end
159+
160+local function close_prompt(state, restore_focus)
161+ if state.closed then
162+ return
163+ end
164+
165+ state.closed = true
166+
167+ if active_prompt == state then
168+ active_prompt = nil
169+ end
170+
171+ if vim.api.nvim_get_current_win() == state.prompt_win then
172+ pcall(vim.cmd, 'stopinsert')
173+ end
174+
175+ if vim.api.nvim_win_is_valid(state.prompt_win) then
176+ pcall(vim.api.nvim_win_close, state.prompt_win, true)
177+ end
178+
179+ if vim.api.nvim_buf_is_valid(state.prompt_buf) then
180+ pcall(vim.api.nvim_buf_delete, state.prompt_buf, { force = true })
181+ end
182+
183+ if restore_focus and vim.api.nvim_win_is_valid(state.source_win) then
184+ pcall(vim.api.nvim_set_current_win, state.source_win)
185+ end
186+end
187+
188+local function source_is_valid(state)
189+ return vim.api.nvim_win_is_valid(state.source_win)
190+ and vim.api.nvim_buf_is_valid(state.source_buf)
191+ and vim.api.nvim_win_get_buf(state.source_win) == state.source_buf
192+ and vim.api.nvim_buf_get_changedtick(state.source_buf) == state.changedtick
193+ and has_visual_range(state.opts)
194+end
195+
196+function M.prompt(opts)
197+ if not has_visual_range(opts) then
198+ notify_error('select text visually first')
199+ return
200+ end
201+
202+ if active_prompt then
203+ close_prompt(active_prompt, false)
204+ end
205+
206+ local source_win = vim.api.nvim_get_current_win()
207+ local source_buf = vim.api.nvim_get_current_buf()
208+ local selection_end = vim.fn.getpos("'>")
209+ local source_width = vim.api.nvim_win_get_width(source_win)
210+ local source_height = vim.api.nvim_win_get_height(source_win)
211+ local available_width = math.max(1, source_width - 2)
212+ local available_height = math.max(1, source_height - 2)
213+ local prompt_width = math.min(80, math.max(20, math.floor(source_width * 0.7)), available_width)
214+ local prompt_height = math.min(5, available_height)
215+ local prompt_buf = vim.api.nvim_create_buf(false, true)
216+
217+ vim.bo[prompt_buf].bufhidden = 'wipe'
218+ vim.bo[prompt_buf].filetype = 'ai-complete-prompt'
219+
220+ local prompt_win = vim.api.nvim_open_win(prompt_buf, true, {
221+ relative = 'win',
222+ win = source_win,
223+ bufpos = { selection_end[2] - 1, math.max(0, selection_end[3] - 1) },
224+ row = 1,
225+ col = 0,
226+ anchor = 'NW',
227+ width = prompt_width,
228+ height = prompt_height,
229+ border = 'single',
230+ style = 'minimal',
231+ })
232+
233+ local state = {
234+ changedtick = vim.api.nvim_buf_get_changedtick(source_buf),
235+ closed = false,
236+ opts = opts,
237+ prompt_buf = prompt_buf,
238+ prompt_win = prompt_win,
239+ source_buf = source_buf,
240+ source_win = source_win,
241+ }
242+
243+ active_prompt = state
244+
245+ local function cancel(restore_focus)
246+ close_prompt(state, restore_focus)
247+ end
248+
249+ local function submit()
250+ if state.closed then
251+ return
252+ end
253+
254+ local prompt = table.concat(vim.api.nvim_buf_get_lines(prompt_buf, 0, -1, false), '\n')
255+ close_prompt(state, true)
256+
257+ -- Let the submit mapping finish so Neovim can leave Insert mode and redraw
258+ -- before the blocking LLM command starts.
259+ vim.schedule(function()
260+ if not source_is_valid(state) then
261+ notify_error('the selected text changed while entering the prompt')
262+ return
263+ end
264+
265+ M.complete(prompt, opts.range)
266+ end)
267+ end
268+
269+ vim.keymap.set({ 'i', 'n' }, '<S-CR>', submit, { buffer = prompt_buf, nowait = true })
270+ vim.keymap.set({ 'i', 'n' }, '<Esc>', function()
271+ cancel(true)
272+ end, { buffer = prompt_buf, nowait = true })
273+ vim.keymap.set({ 'i', 'n' }, '<C-c>', function()
274+ cancel(true)
275+ end, { buffer = prompt_buf, nowait = true })
276+
277+ vim.api.nvim_create_autocmd('WinLeave', {
278+ buffer = prompt_buf,
279+ once = true,
280+ callback = function()
281+ cancel(false)
282+ end,
283+ })
284+
285+ vim.cmd('startinsert')
286+end
287+
288+function M.dispatch(opts)
289+ if not has_visual_range(opts) then
290+ notify_error('select text visually first')
291+ return
292+ end
293+
294+ if opts.args == '' then
295+ M.prompt(opts)
296+ return
297+ end
298+
299+ M.complete(opts.args, opts.range)
300+end
301+
141302 local function prompt_for_llm(user_prompt, selected_text)
142303 return table.concat({
143304 'filename: ' .. vim.fn.expand('%:t'),
@@ -174,7 +335,7 @@ function M.complete(user_prompt, has_range)
174335 return
175336 end
176337
177- vim.notify('ai-complete: generating...', vim.log.levels.INFO)
338+ vim.notify('ai-complete: Generating...', vim.log.levels.INFO)
178339 vim.cmd('redraw')
179340
180341 local output, output_error = run_llm_command(command)