| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import * as vscode from "vscode"
- import { EditorUtils } from "./EditorUtils"
- export type CodeActionName = "EXPLAIN" | "FIX" | "IMPROVE" | "ADD_TO_CONTEXT" | "NEW_TASK"
- export type CodeActionId =
- | "roo-cline.explainCode"
- | "roo-cline.fixCode"
- | "roo-cline.improveCode"
- | "roo-cline.addToContext"
- | "roo-cline.newTask"
- export const ACTION_TITLES: Record<CodeActionName, string> = {
- EXPLAIN: "Explain with Roo Code",
- FIX: "Fix with Roo Code",
- IMPROVE: "Improve with Roo Code",
- ADD_TO_CONTEXT: "Add to Roo Code",
- NEW_TASK: "New Roo Code Task",
- } as const
- export const COMMAND_IDS: Record<CodeActionName, CodeActionId> = {
- 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: CodeActionId,
- args: any[],
- ): vscode.CodeAction {
- const action = new vscode.CodeAction(title, kind)
- action.command = { command, title, arguments: args }
- return action
- }
- 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_TITLES.ADD_TO_CONTEXT,
- vscode.CodeActionKind.QuickFix,
- COMMAND_IDS.ADD_TO_CONTEXT,
- [
- 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) {
- actions.push(
- this.createAction(ACTION_TITLES.FIX, vscode.CodeActionKind.QuickFix, COMMAND_IDS.FIX, [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- relevantDiagnostics.map(EditorUtils.createDiagnosticData),
- ]),
- )
- }
- } else {
- actions.push(
- this.createAction(ACTION_TITLES.EXPLAIN, vscode.CodeActionKind.QuickFix, COMMAND_IDS.EXPLAIN, [
- filePath,
- effectiveRange.text,
- effectiveRange.range.start.line + 1,
- effectiveRange.range.end.line + 1,
- ]),
- )
- actions.push(
- this.createAction(ACTION_TITLES.IMPROVE, vscode.CodeActionKind.QuickFix, 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 []
- }
- }
- }
|