this repo has no description
1local M = {}
2
3-- Get current file context
4function M.get_file_context()
5 return {
6 filepath = vim.fn.expand('%:p'),
7 filename = vim.fn.expand('%:t'),
8 filetype = vim.bo.filetype,
9 line_count = vim.api.nvim_buf_line_count(0)
10 }
11end
12
13-- Detect and extract visual selection
14function M.get_selection_info(range_given, line1, line2)
15 local selection = nil
16 if range_given then
17 -- Command called with range
18 selection = {
19 start_line = line1,
20 end_line = line2,
21 text = table.concat(vim.api.nvim_buf_get_lines(0, line1 - 1, line2, false), '\n')
22 }
23 elseif vim.fn.mode() == 'v' or vim.fn.mode() == 'V' then
24 -- Visual mode selection
25 local start_pos = vim.fn.getpos("'<")
26 local end_pos = vim.fn.getpos("'>")
27 selection = {
28 start_line = start_pos[2],
29 end_line = end_pos[2],
30 text = table.concat(vim.api.nvim_buf_get_lines(0, start_pos[2] - 1, end_pos[2], false), '\n')
31 }
32 end
33 return selection
34end
35
36-- Build prompt with context
37function M.build_prompt(user_input, file_context, selection, is_edit_mode, diagnostic_info)
38 local prompt_parts = {}
39
40 -- Diagnostic fix mode instruction
41 if diagnostic_info then
42 table.insert(prompt_parts,
43 "DIAGNOSTIC FIX MODE: Use your Edit tool to fix the following diagnostic issues in the code. Make precise fixes to resolve the specific problems listed.")
44 table.insert(prompt_parts, diagnostic_info)
45 elseif is_edit_mode then
46 -- Edit mode instruction
47 table.insert(prompt_parts,
48 "EDIT MODE: Return ONLY the modified code with no explanations, markdown formatting, or additional text. Any explanations should be included as code comments within the code itself. Do not surround the code with backticks.")
49 end
50
51 -- File context
52 table.insert(prompt_parts, string.format("File: %s (%s)", file_context.filename, file_context.filetype))
53
54 -- Selection context if present
55 if selection then
56 table.insert(prompt_parts, string.format("Selected lines %d-%d:", selection.start_line, selection.end_line))
57 table.insert(prompt_parts, "```" .. file_context.filetype)
58 table.insert(prompt_parts, selection.text)
59 table.insert(prompt_parts, "```")
60 end
61
62 -- User request
63 table.insert(prompt_parts, "Request: " .. user_input)
64
65 return table.concat(prompt_parts, '\n\n')
66end
67
68-- Execute claude command
69function M.execute_claude(prompt, callback)
70 -- Alternative 1: Use vim.system (Neovim 0.10+) - cleaner, no shell escaping needed
71 vim.system({ 'claude', '-p', prompt }, {
72 text = true
73 }, function(result)
74 vim.schedule(function()
75 if result.code == 0 then
76 if callback then callback(result.stdout) end
77 else
78 vim.notify("Claude error: " .. (result.stderr or "Unknown error"), vim.log.levels.ERROR)
79 end
80 end)
81 end)
82end
83
84-- Execute claude command with tool permissions for diagnostic fixing
85function M.execute_claude_with_tools(prompt, callback)
86 vim.system({ 'claude', '--allowedTools=Edit,Read', '-p', prompt }, {
87 text = true
88 }, function(result)
89 vim.schedule(function()
90 if result.code == 0 then
91 if callback then callback(result.stdout) end
92 else
93 vim.notify("Claude error: " .. (result.stderr or "Unknown error"), vim.log.levels.ERROR)
94 end
95 end)
96 end)
97end
98
99-- Replace selection with new content
100function M.replace_selection(selection, new_content)
101 local lines = vim.split(new_content, '\n')
102 vim.api.nvim_buf_set_lines(0, selection.start_line - 1, selection.end_line, false, lines)
103end
104
105-- Check if claude binary exists
106function M.claude_available()
107 return vim.fn.executable('claude') == 1
108end
109
110return M