| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import * as vscode from "vscode"
- import { EditorUtils } from "./EditorUtils"
- export const ACTION_NAMES = {
- EXPLAIN: "Roo Code: Explain Code",
- FIX: "Roo Code: Fix Code",
- FIX_LOGIC: "Roo Code: Fix Logic",
- IMPROVE: "Roo Code: Improve Code",
- ADD_TO_CONTEXT: "Roo Code: Add to Context",
- NEW_TASK: "Roo Code: New Task",
- } as const
- export const COMMAND_IDS = {
- EXPLAIN: "roo-cline.explainCode",
- FIX: "roo-cline.fixCode",
- IMPROVE: "roo-cline.improveCode",
- ADD_TO_CONTEXT: "roo-cline.addToContext",
- NEW_TASK: "roo-cline.newTask",
- } as const
- export class CodeActionProvider implements vscode.CodeActionProvider {
- public static readonly providedCodeActionKinds = [
- vscode.CodeActionKind.QuickFix,
- vscode.CodeActionKind.RefactorRewrite,
- ]
- private createAction(title: string, kind: vscode.CodeActionKind, command: string, args: any[]): vscode.CodeAction {
- const action = new vscode.CodeAction(title, kind)
- action.command = { command, title, arguments: args }
- return action
- }
- private createActionPair(
- baseTitle: string,
- kind: vscode.CodeActionKind,
- baseCommand: string,
- args: any[],
- ): vscode.CodeAction[] {
- return [
- this.createAction(`${baseTitle} in New Task`, kind, baseCommand, args),
- this.createAction(`${baseTitle} in Current Task`, kind, `${baseCommand}InCurrentTask`, args),
- ]
- }
- public provideCodeActions(
- document: vscode.TextDocument,
- range: vscode.Range | vscode.Selection,
- context: vscode.CodeActionContext,
- ): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> {
- try {
- const effectiveRange = EditorUtils.getEffectiveRange(document, range)
- if (!effectiveRange) {
- return []
- }
- const filePath = EditorUtils.getFilePath(document)
- const actions: vscode.CodeAction[] = []
- actions.push(
- this.createAction(
- ACTION_NAMES.ADD_TO_CONTEXT,
- vscode.CodeActionKind.QuickFix,
- COMMAND_IDS.ADD_TO_CONTEXT,
- [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- ],
- ),
- )
- actions.push(
- ...this.createActionPair(ACTION_NAMES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- ]),
- )
- if (context.diagnostics.length > 0) {
- const relevantDiagnostics = context.diagnostics.filter((d) =>
- EditorUtils.hasIntersectingRange(effectiveRange.range, d.range),
- )
- if (relevantDiagnostics.length > 0) {
- const diagnosticMessages = relevantDiagnostics.map(EditorUtils.createDiagnosticData)
- actions.push(
- ...this.createActionPair(ACTION_NAMES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- diagnosticMessages,
- ]),
- )
- }
- } else {
- actions.push(
- ...this.createActionPair(ACTION_NAMES.FIX_LOGIC, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- ]),
- )
- }
- actions.push(
- ...this.createActionPair(
- ACTION_NAMES.IMPROVE,
- vscode.CodeActionKind.RefactorRewrite,
- COMMAND_IDS.IMPROVE,
- [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- ],
- ),
- )
- return actions
- } catch (error) {
- console.error("Error providing code actions:", error)
- return []
- }
- }
- }
|