···11-local helpers = require('utils.claude_helpers')
22-local diagnostic_helpers = require('utils.diagnostic_helpers')
33-44-local M = {}
55-66--- Display Claude response in new buffer
77-local function show_claude_response(response)
88- -- Create new buffer and switch to it
99- vim.cmd('vnew')
1010- local buf = vim.api.nvim_get_current_buf()
1111-1212- -- Set buffer content
1313- vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(response, '\n'))
1414-1515- -- Set buffer options
1616- vim.bo[buf].modifiable = false
1717- vim.bo[buf].filetype = 'markdown'
1818- vim.bo[buf].buftype = '' -- Make it a normal buffer
1919- vim.bo[buf].bufhidden = 'hide' -- Hide instead of wipe
2020-2121- -- Set buffer name
2222- vim.api.nvim_buf_set_name(buf, 'Claude Response')
2323-end
2424-2525--- Main Claude command handler
2626-local function claude_command(opts)
2727- if not helpers.claude_available() then
2828- vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
2929- return
3030- end
3131-3232- local user_input = table.concat(opts.fargs, ' ')
3333- if user_input == '' then
3434- vim.notify("Please provide a request for Claude", vim.log.levels.WARN)
3535- return
3636- end
3737-3838- local file_context = helpers.get_file_context()
3939- local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
4040- local prompt = helpers.build_prompt(user_input, file_context, selection, false, nil)
4141-4242- vim.notify("Asking Claude...", vim.log.levels.INFO)
4343-4444- helpers.execute_claude(prompt, function(response)
4545- vim.schedule(function()
4646- show_claude_response(response)
4747- end)
4848- end)
4949-end
5050-5151--- Claude edit command handler
5252-local function claude_edit_command(opts)
5353- if not helpers.claude_available() then
5454- vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
5555- return
5656- end
5757-5858- local user_input = table.concat(opts.fargs, ' ')
5959- if user_input == '' then
6060- vim.notify("Please provide a request for Claude", vim.log.levels.WARN)
6161- return
6262- end
6363-6464- local file_context = helpers.get_file_context()
6565- local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
6666-6767- if not selection then
6868- vim.notify("ClaudeEdit requires a selection. Select text first or use a range.", vim.log.levels.WARN)
6969- return
7070- end
7171-7272- local prompt = helpers.build_prompt(user_input, file_context, selection, true, nil)
7373-7474- vim.notify("Claude is editing your selection...", vim.log.levels.INFO)
7575-7676- helpers.execute_claude(prompt, function(response)
7777- vim.schedule(function()
7878- helpers.replace_selection(selection, response)
7979- vim.notify("Selection replaced by Claude", vim.log.levels.INFO)
8080- end)
8181- end)
8282-end
8383-8484--- Claude fix diagnostics command handler
8585-local function claude_fix_command(opts)
8686- if not helpers.claude_available() then
8787- vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR)
8888- return
8989- end
9090-9191- local file_context = helpers.get_file_context()
9292- local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2)
9393-9494- if not selection then
9595- vim.notify("ClaudeFix requires a selection. Select text first or use a range.", vim.log.levels.WARN)
9696- return
9797- end
9898-9999- -- Get diagnostics for the selected range
100100- local diagnostics = diagnostic_helpers.get_range_diagnostics(0, selection.start_line, selection.end_line)
101101-102102- if not diagnostic_helpers.has_diagnostics(0, selection.start_line, selection.end_line) then
103103- vim.notify("No diagnostic issues found in selected range.", vim.log.levels.INFO)
104104- return
105105- end
106106-107107- local diagnostic_info = diagnostic_helpers.format_diagnostics_for_prompt(diagnostics)
108108- local user_input = table.concat(opts.fargs, ' ')
109109- if user_input == '' then
110110- user_input = "Fix the diagnostic issues listed above"
111111- end
112112-113113- local prompt = helpers.build_prompt(user_input, file_context, selection, false, diagnostic_info)
114114-115115- vim.notify("Claude is analyzing and fixing diagnostic issues...", vim.log.levels.INFO)
116116-117117- helpers.execute_claude_with_tools(prompt, function(response)
118118- vim.schedule(function()
119119- vim.notify("Claude diagnostic fix completed. Check the file for changes.", vim.log.levels.INFO)
120120- end)
121121- end)
122122-end
123123-124124--- Setup function to register commands
125125-function M.setup()
126126- -- Register :Claude command
127127- vim.api.nvim_create_user_command('Claude', claude_command, {
128128- nargs = '*',
129129- range = true,
130130- desc = 'Ask Claude for help with code context'
131131- })
132132-133133- -- Register :ClaudeEdit command
134134- vim.api.nvim_create_user_command('ClaudeEdit', claude_edit_command, {
135135- nargs = '*',
136136- range = true,
137137- desc = 'Ask Claude to edit selected code'
138138- })
139139-140140- -- Register :ClaudeFix command
141141- vim.api.nvim_create_user_command('ClaudeFix', claude_fix_command, {
142142- nargs = '*',
143143- range = true,
144144- desc = 'Ask Claude to fix diagnostic issues in selected code'
145145- })
146146-end
147147-148148-return M