xonecas.io
7415
Va

vim-ai-complete / commit 67df62f

feat: cleanup, formatting and code quality pass

xonecas2026-09-10 67df62f3663e57c168358837f1f65e90f6ff72e0 parent 6f2aa1b

3 files changed

README.md+14−0
@@ -71,3 +71,17 @@ git clone https://github.com/sacenox/vim-ai-complete.git
7171 ```
7272
7373 Then restart Neovim.
74+
75+## Formatting
76+
77+Use [StyLua](https://github.com/JohnnyMorganz/StyLua) with the repository's `stylua.toml`:
78+
79+```sh
80+stylua lua/ plugin/
81+```
82+
83+To check formatting without changing files:
84+
85+```sh
86+stylua --check lua/ plugin/
87+```
lua/ai_complete/init.lua+36−31
@@ -106,6 +106,11 @@ local function command_with_cwd(argv)
106106 return vim.list_extend(command, argv)
107107 end
108108
109+local function command_failure(code, output)
110+ local details = vim.trim(output or '')
111+ return 'LLM command failed (exit ' .. code .. ')' .. (details ~= '' and ': ' .. details or '')
112+end
113+
109114 local function run_llm_command(argv)
110115 local command = command_with_cwd(argv)
111116
@@ -119,7 +124,7 @@ local function run_llm_command(argv)
119124 end
120125
121126 if result.code ~= 0 then
122- return nil, 'LLM command failed'
127+ return nil, command_failure(result.code, result.stderr)
123128 end
124129
125130 return result.stdout or ''
@@ -133,7 +138,7 @@ local function run_llm_command(argv)
133138 end
134139
135140 if vim.v.shell_error ~= 0 then
136- return nil, 'LLM command failed'
141+ return nil, command_failure(vim.v.shell_error, output)
137142 end
138143
139144 return output
@@ -151,10 +156,7 @@ local function has_visual_range(opts)
151156 local start_mark = vim.fn.getpos("'<")
152157 local end_mark = vim.fn.getpos("'>")
153158
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]
159+ return start_mark[2] > 0 and end_mark[2] > 0 and opts.line1 == start_mark[2] and opts.line2 == end_mark[2]
158160 end
159161
160162 local function close_prompt(state, restore_focus)
@@ -306,7 +308,7 @@ local function prompt_for_llm(user_prompt, selected_text)
306308 'prompt: ' .. user_prompt,
307309 'selection:',
308310 selected_text,
309- 'Generate an exact replacement for the selected text using the user prompt and surrounding file context. Return only the replacement text.',
311+ '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.',
310312 }, '\n')
311313 end
312314
@@ -316,42 +318,45 @@ function M.complete(user_prompt, has_range)
316318 return
317319 end
318320
319- -- Use register z as scratch space, then restore it before returning.
320- local old_z = vim.fn.getreg('z')
321+ -- Restore scratch register z even if yanking, generation, or replacement fails.
322+ local old_z = vim.fn.getreg('z', 1, true)
321323 local old_z_type = vim.fn.getregtype('z')
322324
323- -- `gv` restores the last Visual selection before yanking it into register z.
324- vim.cmd([[silent normal! gv"zy]])
325+ local ok, err = pcall(function()
326+ -- `gv` restores the last Visual selection before yanking it into register z.
327+ vim.cmd([[silent normal! gv"zy]])
325328
326- local selected_text = vim.fn.getreg('z')
327- local selected_type = vim.fn.getregtype('z')
329+ local selected_text = vim.fn.getreg('z')
330+ local selected_type = vim.fn.getregtype('z')
328331
329- local llm_prompt = prompt_for_llm(user_prompt, selected_text)
330- local command, command_error = command_for_prompt(llm_prompt)
332+ local llm_prompt = prompt_for_llm(user_prompt, selected_text)
333+ local command, command_error = command_for_prompt(llm_prompt)
331334
332- if not command then
333- vim.fn.setreg('z', old_z, old_z_type)
334- vim.notify('ai-complete: ' .. command_error, vim.log.levels.ERROR)
335- return
336- end
335+ if not command then
336+ error(command_error, 0)
337+ end
337338
338- vim.notify('ai-complete: Generating...', vim.log.levels.INFO)
339- vim.cmd('redraw')
339+ vim.notify('ai-complete: Generating...', vim.log.levels.INFO)
340+ vim.cmd('redraw')
340341
341- local output, output_error = run_llm_command(command)
342+ local output, output_error = run_llm_command(command)
342343
343- if output == nil then
344- vim.fn.setreg('z', old_z, old_z_type)
345- vim.notify('ai-complete: ' .. output_error, vim.log.levels.ERROR)
346- return
347- end
344+ if output == nil then
345+ error(output_error, 0)
346+ end
348347
349- -- Preserve characterwise, linewise, or blockwise paste behavior.
350- vim.fn.setreg('z', output, selected_type)
351- vim.cmd([[silent normal! gv"zp]])
348+ -- Preserve characterwise, linewise, or blockwise paste behavior.
349+ vim.fn.setreg('z', output, selected_type)
350+ vim.cmd([[silent normal! gv"zp]])
351+ end)
352352
353353 vim.fn.setreg('z', old_z, old_z_type)
354354
355+ if not ok then
356+ notify_error(tostring(err))
357+ return
358+ end
359+
355360 vim.notify('ai-complete: done.', vim.log.levels.INFO)
356361 vim.cmd('redraw')
357362 end
stylua.toml+3−0
@@ -0,0 +1,3 @@
1+indent_type = "Spaces"
2+indent_width = 2
3+quote_style = "AutoPreferSingle"