mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
- Move "Add to Context" to the top of submenu and code actions for improved accessibility - Add line number tracking (startLine/endLine) to EditorContext and code actions - Update templates in support-prompt.ts to include line numbers in file references - Ensure backward compatibility with existing code This change improves the UX by making the frequently used "Add to Context" action more accessible and enhances context awareness by tracking and displaying line numbers for selected code.
130 lines
3.5 KiB
TypeScript
130 lines
3.5 KiB
TypeScript
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 []
|
|
}
|
|
}
|
|
}
|