|
| 1 | +import * as vscode from "vscode" |
| 2 | +import * as path from "path" |
| 3 | + |
| 4 | +export const ACTION_NAMES = { |
| 5 | + EXPLAIN: "Roo Code: Explain Code", |
| 6 | + FIX: "Roo Code: Fix Code", |
| 7 | + IMPROVE: "Roo Code: Improve Code", |
| 8 | +} as const |
| 9 | + |
| 10 | +const COMMAND_IDS = { |
| 11 | + EXPLAIN: "roo-cline.explainCode", |
| 12 | + FIX: "roo-cline.fixCode", |
| 13 | + IMPROVE: "roo-cline.improveCode", |
| 14 | +} as const |
| 15 | + |
| 16 | +interface DiagnosticData { |
| 17 | + message: string |
| 18 | + severity: vscode.DiagnosticSeverity |
| 19 | + code?: string | number | { value: string | number; target: vscode.Uri } |
| 20 | + source?: string |
| 21 | + range: vscode.Range |
| 22 | +} |
| 23 | + |
| 24 | +interface EffectiveRange { |
| 25 | + range: vscode.Range |
| 26 | + text: string |
| 27 | +} |
| 28 | + |
| 29 | +export class CodeActionProvider implements vscode.CodeActionProvider { |
| 30 | + public static readonly providedCodeActionKinds = [ |
| 31 | + vscode.CodeActionKind.QuickFix, |
| 32 | + vscode.CodeActionKind.RefactorRewrite, |
| 33 | + ] |
| 34 | + |
| 35 | + // Cache file paths for performance |
| 36 | + private readonly filePathCache = new WeakMap<vscode.TextDocument, string>() |
| 37 | + |
| 38 | + private getEffectiveRange( |
| 39 | + document: vscode.TextDocument, |
| 40 | + range: vscode.Range | vscode.Selection, |
| 41 | + ): EffectiveRange | null { |
| 42 | + try { |
| 43 | + const selectedText = document.getText(range) |
| 44 | + if (selectedText) { |
| 45 | + return { range, text: selectedText } |
| 46 | + } |
| 47 | + |
| 48 | + const currentLine = document.lineAt(range.start.line) |
| 49 | + if (!currentLine.text.trim()) { |
| 50 | + return null |
| 51 | + } |
| 52 | + |
| 53 | + // Optimize range creation by checking bounds first |
| 54 | + const startLine = Math.max(0, currentLine.lineNumber - 1) |
| 55 | + const endLine = Math.min(document.lineCount - 1, currentLine.lineNumber + 1) |
| 56 | + |
| 57 | + // Only create new positions if needed |
| 58 | + const effectiveRange = new vscode.Range( |
| 59 | + startLine === currentLine.lineNumber ? range.start : new vscode.Position(startLine, 0), |
| 60 | + endLine === currentLine.lineNumber |
| 61 | + ? range.end |
| 62 | + : new vscode.Position(endLine, document.lineAt(endLine).text.length), |
| 63 | + ) |
| 64 | + |
| 65 | + return { |
| 66 | + range: effectiveRange, |
| 67 | + text: document.getText(effectiveRange), |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + console.error("Error getting effective range:", error) |
| 71 | + return null |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private getFilePath(document: vscode.TextDocument): string { |
| 76 | + // Check cache first |
| 77 | + let filePath = this.filePathCache.get(document) |
| 78 | + if (filePath) { |
| 79 | + return filePath |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri) |
| 84 | + if (!workspaceFolder) { |
| 85 | + filePath = document.uri.fsPath |
| 86 | + } else { |
| 87 | + const relativePath = path.relative(workspaceFolder.uri.fsPath, document.uri.fsPath) |
| 88 | + filePath = !relativePath || relativePath.startsWith("..") ? document.uri.fsPath : relativePath |
| 89 | + } |
| 90 | + |
| 91 | + // Cache the result |
| 92 | + this.filePathCache.set(document, filePath) |
| 93 | + return filePath |
| 94 | + } catch (error) { |
| 95 | + console.error("Error getting file path:", error) |
| 96 | + return document.uri.fsPath |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private createDiagnosticData(diagnostic: vscode.Diagnostic): DiagnosticData { |
| 101 | + return { |
| 102 | + message: diagnostic.message, |
| 103 | + severity: diagnostic.severity, |
| 104 | + code: diagnostic.code, |
| 105 | + source: diagnostic.source, |
| 106 | + range: diagnostic.range, // Reuse the range object |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private createAction(title: string, kind: vscode.CodeActionKind, command: string, args: any[]): vscode.CodeAction { |
| 111 | + const action = new vscode.CodeAction(title, kind) |
| 112 | + action.command = { command, title, arguments: args } |
| 113 | + return action |
| 114 | + } |
| 115 | + |
| 116 | + private hasIntersectingRange(range1: vscode.Range, range2: vscode.Range): boolean { |
| 117 | + // Optimize range intersection check |
| 118 | + return !( |
| 119 | + range2.end.line < range1.start.line || |
| 120 | + range2.start.line > range1.end.line || |
| 121 | + (range2.end.line === range1.start.line && range2.end.character < range1.start.character) || |
| 122 | + (range2.start.line === range1.end.line && range2.start.character > range1.end.character) |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + public provideCodeActions( |
| 127 | + document: vscode.TextDocument, |
| 128 | + range: vscode.Range | vscode.Selection, |
| 129 | + context: vscode.CodeActionContext, |
| 130 | + ): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> { |
| 131 | + try { |
| 132 | + const effectiveRange = this.getEffectiveRange(document, range) |
| 133 | + if (!effectiveRange) { |
| 134 | + return [] |
| 135 | + } |
| 136 | + |
| 137 | + const filePath = this.getFilePath(document) |
| 138 | + const actions: vscode.CodeAction[] = [] |
| 139 | + |
| 140 | + // Create actions using helper method |
| 141 | + actions.push( |
| 142 | + this.createAction(ACTION_NAMES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [ |
| 143 | + filePath, |
| 144 | + effectiveRange.text, |
| 145 | + ]), |
| 146 | + ) |
| 147 | + |
| 148 | + // Only process diagnostics if they exist |
| 149 | + if (context.diagnostics.length > 0) { |
| 150 | + const relevantDiagnostics = context.diagnostics.filter((d) => |
| 151 | + this.hasIntersectingRange(effectiveRange.range, d.range), |
| 152 | + ) |
| 153 | + |
| 154 | + if (relevantDiagnostics.length > 0) { |
| 155 | + const diagnosticMessages = relevantDiagnostics.map(this.createDiagnosticData) |
| 156 | + actions.push( |
| 157 | + this.createAction(ACTION_NAMES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [ |
| 158 | + filePath, |
| 159 | + effectiveRange.text, |
| 160 | + diagnosticMessages, |
| 161 | + ]), |
| 162 | + ) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + actions.push( |
| 167 | + this.createAction(ACTION_NAMES.IMPROVE, vscode.CodeActionKind.RefactorRewrite, COMMAND_IDS.IMPROVE, [ |
| 168 | + filePath, |
| 169 | + effectiveRange.text, |
| 170 | + ]), |
| 171 | + ) |
| 172 | + |
| 173 | + return actions |
| 174 | + } catch (error) { |
| 175 | + console.error("Error providing code actions:", error) |
| 176 | + return [] |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments