xonecas.io
7415
Va

vim-ai-complete / commit 46bf1b8

chore: small refactoring for legibility, no behaviour change

xonecas2026-09-10 46bf1b8f63b88bd2f80b1ebc26178e3ef64037ac parent 07e2c5a

3 files changed

AGENTS.md+0−1
@@ -21,7 +21,6 @@ Users can configure another assistant with `require("ai_complete").setup()`. The
2121 - Neovim runtime entrypoint.
2222 - Guards against double loading with `vim.g.loaded_ai_complete`.
2323 - Defines the public `:Ai` command.
24- - Adds a lowercase `:ai` command-line abbreviation.
2524
2625 - `lua/ai_complete/init.lua`
2726 - Main implementation module.
README.md+0−12
@@ -27,18 +27,6 @@ return {
2727 }
2828 ```
2929
30-Note: when lazy-loading with `cmd = { "Ai", "AiComplete" }`, the lowercase `:ai` abbreviation is only available after the plugin has loaded. Use `:Ai` to trigger loading, or define the abbreviation in `init`:
31-
32-```lua
33-return {
34- "sacenox/vim-ai-complete",
35- cmd = { "Ai", "AiComplete" },
36- init = function()
37- vim.cmd([[cabbrev ai Ai]])
38- end,
39-}
40-```
41-
4230 ### Native packages
4331
4432 Without a plugin manager:
lua/ai_complete/init.lua+84−53
@@ -1,6 +1,23 @@
11 local M = {}
22
3+-- Constants and module state
4+
35 local PROMPT_PLACEHOLDER = '{prompt}'
6+local REPLACEMENT_INSTRUCTION =
7+ 'Generate an exact replacement for the selected text using the user prompt and surrounding file context. Return only the replacement text exactly as it should appear in the file. Do not add commentary, formatting wrappers, or surrounding code fences.'
8+local COMPLETION_INSTRUCTION =
9+ 'Complete the text at the cursor based on the surrounding context. Decide what and how much to insert, and read additional context as needed. Return only the text to insert, without repeating existing text, commentary, formatting wrappers, or surrounding code fences.'
10+
11+---@class AiCompletePromptState
12+---@field changedtick integer
13+---@field closed boolean
14+---@field opts { range: integer, line1: integer, line2: integer }
15+---@field prompt_buf integer
16+---@field prompt_win integer
17+---@field source_buf integer
18+---@field source_win integer
19+
20+---@type AiCompletePromptState?
421 local active_prompt
522
623 local config = {
@@ -25,6 +42,21 @@ function M.setup(opts)
2542 end
2643 end
2744
45+-- Shared feedback, file context, and LLM execution
46+
47+local function notify_error(message)
48+ vim.notify('ai-complete: ' .. message, vim.log.levels.ERROR)
49+end
50+
51+local function notify_done()
52+ vim.notify('ai-complete: done.', vim.log.levels.INFO)
53+ vim.cmd('redraw')
54+end
55+
56+local function file_context()
57+ return 'filename: ' .. vim.fn.expand('%:t') .. '\npath: ' .. vim.fn.expand('%:p')
58+end
59+
2860 local function validate_command(command)
2961 if type(command) ~= 'table' or #command == 0 then
3062 return nil, 'LLM command must be a non-empty argv list'
@@ -163,8 +195,50 @@ local function generate(prompt)
163195 return output
164196 end
165197
166-local function notify_error(message)
167- vim.notify('ai-complete: ' .. message, vim.log.levels.ERROR)
198+-- Feature: replace a visual selection
199+
200+local function prompt_for_selection(user_prompt, selected_text)
201+ return table.concat({
202+ file_context(),
203+ 'prompt: ' .. user_prompt,
204+ 'selection:',
205+ selected_text,
206+ REPLACEMENT_INSTRUCTION,
207+ }, '\n')
208+end
209+
210+function M.complete(user_prompt, has_range)
211+ if has_range == 0 then
212+ notify_error('select text visually first')
213+ return
214+ end
215+
216+ -- Restore scratch register z even if yanking, generation, or replacement fails.
217+ local old_z = vim.fn.getreg('z', 1, true)
218+ local old_z_type = vim.fn.getregtype('z')
219+
220+ local ok, err = pcall(function()
221+ -- `gv` restores the last Visual selection before yanking it into register z.
222+ vim.cmd([[silent normal! gv"zy]])
223+
224+ local selected_text = vim.fn.getreg('z')
225+ local selected_type = vim.fn.getregtype('z')
226+
227+ local output = generate(prompt_for_selection(user_prompt, selected_text))
228+
229+ -- Preserve characterwise, linewise, or blockwise paste behavior.
230+ vim.fn.setreg('z', output, selected_type)
231+ vim.cmd([[silent normal! gv"zp]])
232+ end)
233+
234+ vim.fn.setreg('z', old_z, old_z_type)
235+
236+ if not ok then
237+ notify_error(tostring(err))
238+ return
239+ end
240+
241+ notify_done()
168242 end
169243
170244 local function has_visual_range(opts)
@@ -190,7 +264,9 @@ local function close_prompt(state, restore_focus)
190264 end
191265
192266 if vim.api.nvim_get_current_win() == state.prompt_win then
193- pcall(vim.cmd, 'stopinsert')
267+ pcall(function()
268+ vim.cmd('stopinsert')
269+ end)
194270 end
195271
196272 if vim.api.nvim_win_is_valid(state.prompt_win) then
@@ -251,6 +327,7 @@ function M.prompt(opts)
251327 style = 'minimal',
252328 })
253329
330+ ---@type AiCompletePromptState
254331 local state = {
255332 changedtick = vim.api.nvim_buf_get_changedtick(source_buf),
256333 closed = false,
@@ -320,51 +397,7 @@ function M.dispatch(opts)
320397 M.complete(opts.args, opts.range)
321398 end
322399
323-local function prompt_for_llm(user_prompt, selected_text)
324- return table.concat({
325- 'filename: ' .. vim.fn.expand('%:t'),
326- 'path: ' .. vim.fn.expand('%:p'),
327- 'prompt: ' .. user_prompt,
328- 'selection:',
329- selected_text,
330- 'Generate an exact replacement for the selected text using the user prompt and surrounding file context. Return only the replacement text exactly as it should appear in the file. Do not add commentary, formatting wrappers, or surrounding code fences.',
331- }, '\n')
332-end
333-
334-function M.complete(user_prompt, has_range)
335- if has_range == 0 then
336- vim.notify('ai-complete: select text visually first', vim.log.levels.ERROR)
337- return
338- end
339-
340- -- Restore scratch register z even if yanking, generation, or replacement fails.
341- local old_z = vim.fn.getreg('z', 1, true)
342- local old_z_type = vim.fn.getregtype('z')
343-
344- local ok, err = pcall(function()
345- -- `gv` restores the last Visual selection before yanking it into register z.
346- vim.cmd([[silent normal! gv"zy]])
347-
348- local selected_text = vim.fn.getreg('z')
349- local selected_type = vim.fn.getregtype('z')
350-
351- local output = generate(prompt_for_llm(user_prompt, selected_text))
352-
353- -- Preserve characterwise, linewise, or blockwise paste behavior.
354- vim.fn.setreg('z', output, selected_type)
355- vim.cmd([[silent normal! gv"zp]])
356- end)
357-
358- vim.fn.setreg('z', old_z, old_z_type)
359-
360- if not ok then
361- notify_error(tostring(err))
362- return
363- end
364-
365- vim.notify('ai-complete: done.', vim.log.levels.INFO)
366- vim.cmd('redraw')
367-end
400+-- Feature: complete at the cursor
368401
369402 function M.complete_at_cursor()
370403 local buf = vim.api.nvim_get_current_buf()
@@ -373,9 +406,8 @@ function M.complete_at_cursor()
373406
374407 local ok, err = pcall(function()
375408 local prompt = table.concat({
376- 'filename: ' .. vim.fn.expand('%:t'),
377- 'path: ' .. vim.fn.expand('%:p'),
378- 'Complete the text at the cursor based on the surrounding context. Decide what and how much to insert, and read additional context as needed. Return only the text to insert, without repeating existing text, commentary, formatting wrappers, or surrounding code fences.',
409+ file_context(),
410+ COMPLETION_INSTRUCTION,
379411 'Insertion is immediately before line ' .. cursor[1] .. ', byte column ' .. (col + 1) .. ' (1-based).',
380412 'Current buffer (including unsaved changes):',
381413 table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n'),
@@ -392,8 +424,7 @@ function M.complete_at_cursor()
392424 return
393425 end
394426
395- vim.notify('ai-complete: done.', vim.log.levels.INFO)
396- vim.cmd('redraw')
427+ notify_done()
397428 end
398429
399430 return M