xonecas.io
7415
Va

vim-ai-complete / commit c6e9143

feat: add multiline prompt window

xonecas2026-08-17 c6e91432cbf32d2a35361970b877a7d78a879981 parent 21caac1

5 files changed

AGENTS.md+8−6
@@ -4,14 +4,16 @@ Repository notes for coding agents working on `vim-ai-complete`.
44
55 ## Project overview
66
7-This is a minimal Neovim plugin that exposes `:Ai <prompt>` for AI-assisted edits. The user visually selects text, runs `:Ai`, and the plugin replaces the selection with stdout from the `pi` coding agent.
7+This is a minimal Neovim plugin that exposes `:Ai <prompt>` for AI-assisted edits. The user visually selects text, runs `:Ai`, and the plugin replaces the selection with standard output from the configured assistant command.
88
9-The plugin intentionally invokes `pi` in a constrained way:
9+The plugin uses `pi` by default and invokes it with these constraints:
1010
11-- read-only tools only: `read,find,ls,grep`
12-- `--thinking minimal` for speed
13-- no edit/write tools; Neovim performs the actual replacement
14-- prompt asks for replacement text only
11+- read-only tools: `read,find,ls,grep`
12+- `--thinking high`
13+- no edit or write tools; Neovim performs the replacement
14+- a prompt that requests replacement text only
15+
16+Users can configure another assistant with `require("ai_complete").setup()`. The command can be an argument list or a function that returns one. It must accept the generated prompt and write the replacement text to standard output.
1517
1618 ## Repository layout
1719
README.md+2−0
@@ -8,6 +8,8 @@ A small Neovim plugin for applying an LLM instruction to selected text.
88 2. Run `:Ai <prompt>`.
99 3. The selected text is replaced when generation succeeds.
1010
11+Run `:Ai` without a prompt to open a multiline input window below the last selected line. Press `<Enter>` to add a line and `<S-Enter>` to submit, including when the prompt is empty. Press `<Esc>` or `<C-c>` to cancel. The status area shows `Generating...` while the blocking LLM command runs.
12+
1113 Characterwise, linewise, and blockwise selections are supported. If generation fails, the buffer is left unchanged. A completed replacement is a normal Neovim edit and can be reverted with `u`.
1214
1315 ## LLM command
TODO.md+5−20
@@ -1,25 +1,10 @@
11 # TODO
22
3-These items describe product needs, not committed designs.
3+## Suggest next _few_ lines?
44
5-## Conversational edit sessions
5+Either on idle cursor, or as an explicit command from the user, generate a suggestion from the code surrounding the current cursor position. Show it as a suggestion (italic maybe) with a vim command to accept.
66
7-Support a conversation or session around each requested edit so the user can iterate with the model and refine its result while retaining relevant context. See [`docs/llama-vim-reference.md`](docs/llama-vim-reference.md) for a source-based review of a similar edit-scoped conversation model.
7+## Look into asynchronous generation
88
9-The interaction model is intentionally undecided. Do not commit to retry commands, refinement commands, or a particular session UI until the workflow has been explored further.
10-
11-## Authentication UX
12-
13-Authentication currently belongs to the configured LLM command. If a future integration requires plugin-managed authentication, provide a progressive in-Neovim flow similar to Windsurf's: let the user open the authorization page, copy its URL, display it inside Neovim, or provide an existing credential. Secret input must not expose credentials in the command line, messages, or logs.
14-
15-Keep the user in Neovim except when browser authorization is required, and provide a fallback when automatically opening the browser is unavailable.
16-
17-## Long-prompt input
18-
19-Investigate a dedicated prompt input UI because long `:Ai` command-line prompts can disrupt Neovim's layout. The input should handle long or multiline prompts while keeping the selected code visible. `vim.ui.input()`, a floating window, or a scratch buffer are possible approaches, not requirements.
20-
21-## Asynchronous generation
22-
23-Revisit non-blocking generation when conversational edit sessions make it useful. Blocking is intentional for the current one-shot workflow because it prevents the target selection from changing while generation is in progress.
24-
25-Any asynchronous design must define what happens when the user edits the target buffer or selection before a result arrives. It must not overwrite newer edits unconditionally, and automatic reconciliation should not be introduced without a clear interaction model.
9+Blocking the UI is a shortcut that ensures the buffer does not change and lets us easily insert the LLM output without collisions or conflicts by using Neovim's `gv` command.
10+It is time to stop using this shortcut and make the generation asynchronous.
lua/ai_complete/init.lua+162−1
@@ -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)
plugin/ai_complete.lua+1−1
@@ -5,7 +5,7 @@ end
55 vim.g.loaded_ai_complete = 1
66
77 vim.api.nvim_create_user_command('Ai', function(opts)
8- require('ai_complete').complete(opts.args, opts.range)
8+ require('ai_complete').dispatch(opts)
99 end, {
1010 nargs = '*',
1111 range = true,