this repo has no description
1local helpers = require('utils.claude_helpers')
2local diagnostic_helpers = require('utils.diagnostic_helpers')
3
4local M = {}
5
6-- Display Claude response in new buffer
7local function show_claude_response(response)
8 -- Create new buffer and switch to it
9 vim.cmd('vnew')
10 local buf = vim.api.nvim_get_current_buf()
11
12 -- Set buffer content
13 vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(response, '\n'))
14
15 -- Set buffer options
16 vim.bo[buf].modifiable = false
17 vim.bo[buf].filetype = 'markdown'
18 vim.bo[buf].buftype = '' -- Make it a normal buffer
19 vim.bo[buf].bufhidden = 'hide' -- Hide instead of wipe
20
21 -- Set buffer name
22 vim.api.nvim_buf_set_name(buf, 'Claude Response')
23end
24
25-- Main Claude command handler
26local function claude_command(opts)
27 if not helpers.claude_available() then
28 vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
29 return
30 end
31
32 local user_input = table.concat(opts.fargs, ' ')
33 if user_input == '' then
34 vim.notify("Please provide a request for Claude", vim.log.levels.WARN)
35 return
36 end
37
38 local file_context = helpers.get_file_context()
39 local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
40 local prompt = helpers.build_prompt(user_input, file_context, selection, false, nil)
41
42 vim.notify("Asking Claude...", vim.log.levels.INFO)
43
44 helpers.execute_claude(prompt, function(response)
45 vim.schedule(function()
46 show_claude_response(response)
47 end)
48 end)
49end
50
51-- Claude edit command handler
52local function claude_edit_command(opts)
53 if not helpers.claude_available() then
54 vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
55 return
56 end
57
58 local user_input = table.concat(opts.fargs, ' ')
59 if user_input == '' then
60 vim.notify("Please provide a request for Claude", vim.log.levels.WARN)
61 return
62 end
63
64 local file_context = helpers.get_file_context()
65 local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
66
67 if not selection then
68 vim.notify("ClaudeEdit requires a selection. Select text first or use a range.", vim.log.levels.WARN)
69 return
70 end
71
72 local prompt = helpers.build_prompt(user_input, file_context, selection, true, nil)
73
74 vim.notify("Claude is editing your selection...", vim.log.levels.INFO)
75
76 helpers.execute_claude(prompt, function(response)
77 vim.schedule(function()
78 helpers.replace_selection(selection, response)
79 vim.notify("Selection replaced by Claude", vim.log.levels.INFO)
80 end)
81 end)
82end
83
84-- Claude fix diagnostics command handler
85local function claude_fix_command(opts)
86 if not helpers.claude_available() then
87 vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
88 return
89 end
90
91 local file_context = helpers.get_file_context()
92 local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
93
94 if not selection then
95 vim.notify("ClaudeFix requires a selection. Select text first or use a range.", vim.log.levels.WARN)
96 return
97 end
98
99 -- Get diagnostics for the selected range
100 local diagnostics = diagnostic_helpers.get_range_diagnostics(0, selection.start_line, selection.end_line)
101
102 if not diagnostic_helpers.has_diagnostics(0, selection.start_line, selection.end_line) then
103 vim.notify("No diagnostic issues found in selected range.", vim.log.levels.INFO)
104 return
105 end
106
107 local diagnostic_info = diagnostic_helpers.format_diagnostics_for_prompt(diagnostics)
108 local user_input = table.concat(opts.fargs, ' ')
109 if user_input == '' then
110 user_input = "Fix the diagnostic issues listed above"
111 end
112
113 local prompt = helpers.build_prompt(user_input, file_context, selection, false, diagnostic_info)
114
115 vim.notify("Claude is analyzing and fixing diagnostic issues...", vim.log.levels.INFO)
116
117 helpers.execute_claude_with_tools(prompt, function(response)
118 vim.schedule(function()
119 vim.notify("Claude diagnostic fix completed. Check the file for changes.", vim.log.levels.INFO)
120 end)
121 end)
122end
123
124-- Setup function to register commands
125function M.setup()
126 -- Register :Claude command
127 vim.api.nvim_create_user_command('Claude', claude_command, {
128 nargs = '*',
129 range = true,
130 desc = 'Ask Claude for help with code context'
131 })
132
133 -- Register :ClaudeEdit command
134 vim.api.nvim_create_user_command('ClaudeEdit', claude_edit_command, {
135 nargs = '*',
136 range = true,
137 desc = 'Ask Claude to edit selected code'
138 })
139
140 -- Register :ClaudeFix command
141 vim.api.nvim_create_user_command('ClaudeFix', claude_fix_command, {
142 nargs = '*',
143 range = true,
144 desc = 'Ask Claude to fix diagnostic issues in selected code'
145 })
146end
147
148return M