local helpers = require('utils.claude_helpers') local diagnostic_helpers = require('utils.diagnostic_helpers') local M = {} -- Display Claude response in new buffer local function show_claude_response(response) -- Create new buffer and switch to it vim.cmd('vnew') local buf = vim.api.nvim_get_current_buf() -- Set buffer content vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(response, '\n')) -- Set buffer options vim.bo[buf].modifiable = false vim.bo[buf].filetype = 'markdown' vim.bo[buf].buftype = '' -- Make it a normal buffer vim.bo[buf].bufhidden = 'hide' -- Hide instead of wipe -- Set buffer name vim.api.nvim_buf_set_name(buf, 'Claude Response') end -- Main Claude command handler local function claude_command(opts) if not helpers.claude_available() then vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR) return end local user_input = table.concat(opts.fargs, ' ') if user_input == '' then vim.notify("Please provide a request for Claude", vim.log.levels.WARN) return end local file_context = helpers.get_file_context() local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2) local prompt = helpers.build_prompt(user_input, file_context, selection, false, nil) vim.notify("Asking Claude...", vim.log.levels.INFO) helpers.execute_claude(prompt, function(response) vim.schedule(function() show_claude_response(response) end) end) end -- Claude edit command handler local function claude_edit_command(opts) if not helpers.claude_available() then vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR) return end local user_input = table.concat(opts.fargs, ' ') if user_input == '' then vim.notify("Please provide a request for Claude", vim.log.levels.WARN) return end local file_context = helpers.get_file_context() local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2) if not selection then vim.notify("ClaudeEdit requires a selection. Select text first or use a range.", vim.log.levels.WARN) return end local prompt = helpers.build_prompt(user_input, file_context, selection, true, nil) vim.notify("Claude is editing your selection...", vim.log.levels.INFO) helpers.execute_claude(prompt, function(response) vim.schedule(function() helpers.replace_selection(selection, response) vim.notify("Selection replaced by Claude", vim.log.levels.INFO) end) end) end -- Claude fix diagnostics command handler local function claude_fix_command(opts) if not helpers.claude_available() then vim.notify("Claude binary not found. Please install claude-code CLI.", vim.log.levels.ERROR) return end local file_context = helpers.get_file_context() local selection = helpers.get_selection_info(opts.range > 0, opts.line1, opts.line2) if not selection then vim.notify("ClaudeFix requires a selection. Select text first or use a range.", vim.log.levels.WARN) return end -- Get diagnostics for the selected range local diagnostics = diagnostic_helpers.get_range_diagnostics(0, selection.start_line, selection.end_line) if not diagnostic_helpers.has_diagnostics(0, selection.start_line, selection.end_line) then vim.notify("No diagnostic issues found in selected range.", vim.log.levels.INFO) return end local diagnostic_info = diagnostic_helpers.format_diagnostics_for_prompt(diagnostics) local user_input = table.concat(opts.fargs, ' ') if user_input == '' then user_input = "Fix the diagnostic issues listed above" end local prompt = helpers.build_prompt(user_input, file_context, selection, false, diagnostic_info) vim.notify("Claude is analyzing and fixing diagnostic issues...", vim.log.levels.INFO) helpers.execute_claude_with_tools(prompt, function(response) vim.schedule(function() vim.notify("Claude diagnostic fix completed. Check the file for changes.", vim.log.levels.INFO) end) end) end -- Setup function to register commands function M.setup() -- Register :Claude command vim.api.nvim_create_user_command('Claude', claude_command, { nargs = '*', range = true, desc = 'Ask Claude for help with code context' }) -- Register :ClaudeEdit command vim.api.nvim_create_user_command('ClaudeEdit', claude_edit_command, { nargs = '*', range = true, desc = 'Ask Claude to edit selected code' }) -- Register :ClaudeFix command vim.api.nvim_create_user_command('ClaudeFix', claude_fix_command, { nargs = '*', range = true, desc = 'Ask Claude to fix diagnostic issues in selected code' }) end return M