@@ -0,0 +1,88 @@
1+-- Create a table that will hold everything this module exposes.
2+-- In Lua plugins, returning a table like this lets other files call M.complete().
3+local M = {}
4+
5+-- Build the prompt that will be sent to the `pi` command-line tool.
6+-- We include both editor context and the text the user selected so the tool knows
7+-- exactly what file it is working on and what text it should transform.
8+local function prompt_for_pi(user_prompt, selected_text)
9+ -- table.concat joins each line below with a newline, producing one readable
10+ -- prompt instead of manually adding "\n" to every string.
11+ return table.concat({
12+ -- Current file name, without directories. Example: "init.lua".
13+ 'filename: ' .. vim.fn.expand('%:t'),
14+ -- Full path to the current file, so the tool has precise context.
15+ 'path: ' .. vim.fn.expand('%:p'),
16+ -- The user's instruction, such as "add comments" or "refactor this".
17+ 'prompt: ' .. user_prompt,
18+ -- A label that separates metadata above from the selected text below.
19+ 'selection:',
20+ -- The actual text selected in Visual mode.
21+ selected_text,
22+ -- The "system" prompt
23+ 'Use the information above to generate the exact replacement of the given text above. Return only the exact replacement text. Do not edit the file directly'
24+ }, '\n')
25+end
26+
27+-- Complete or rewrite the current visual selection using the external `pi` tool.
28+-- `user_prompt` is the instruction typed by the user.
29+-- `has_range` tells us whether Vim passed a visual range to this command.
30+function M.complete(user_prompt, has_range)
31+ -- This command only makes sense when text is selected. If there is no range,
32+ -- show an error and stop before changing anything.
33+ if has_range == 0 then
34+ vim.notify('pi-complete: select text visually first', vim.log.levels.ERROR)
35+ return
36+ end
37+
38+ -- We use register "z" as a temporary clipboard for the selected text and for
39+ -- the replacement. Before touching it, save its contents and type so we can
40+ -- restore the user's register exactly as it was.
41+ local old_z = vim.fn.getreg('z')
42+ local old_z_type = vim.fn.getregtype('z')
43+
44+ -- Re-select the previous Visual selection (`gv`), then yank it into register z
45+ -- (`"zy`). `silent normal!` runs the Normal-mode keys without extra messages or
46+ -- user mappings, making the command predictable.
47+ vim.cmd([[silent normal! gv"zy]])
48+
49+ -- Read the selected text and selection type from register z. The type matters:
50+ -- it tells Vim whether the selection was characterwise, linewise, or blockwise.
51+ local selected_text = vim.fn.getreg('z')
52+ local selected_type = vim.fn.getregtype('z')
53+
54+ -- Call the external `pi` program with a prompt built from the user instruction
55+ -- and the selected text from Vim's current working directory. The command's
56+ -- stdout becomes `output`.
57+ local output = vim.fn.system({
58+ 'sh',
59+ '-c',
60+ 'cd "$1" && shift && exec "$@"',
61+ 'sh',
62+ vim.fn.getcwd(),
63+ 'pi',
64+ '-tools',
65+ 'read',
66+ '-p',
67+ prompt_for_pi(user_prompt, selected_text),
68+ })
69+
70+ -- If the shell command failed, restore register z, report the error, and leave
71+ -- the buffer unchanged.
72+ if vim.v.shell_error ~= 0 then
73+ vim.fn.setreg('z', old_z, old_z_type)
74+ vim.notify('pi-complete: pi failed', vim.log.levels.ERROR)
75+ return
76+ end
77+
78+ -- Put the generated output into register z using the original selection type,
79+ -- then re-select the same text and paste over it from register z.
80+ vim.fn.setreg('z', output, selected_type)
81+ vim.cmd([[silent normal! gv"zp]])
82+
83+ -- Finally, restore register z so this plugin does not clobber the user's data.
84+ vim.fn.setreg('z', old_z, old_z_type)
85+end
86+
87+-- Return the module table so Neovim can require() this file and call M.complete().
88+return M