forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeActionProvider.ts
More file actions
104 lines (90 loc) · 2.95 KB
/
CodeActionProvider.ts
File metadata and controls
104 lines (90 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as vscode from "vscode"
import { CodeActionName, CodeActionId } from "@roo-code/types"
import { Package } from "../shared/package"
import { getCodeActionCommand } from "../utils/commands"
import { EditorUtils } from "../integrations/editor/EditorUtils"
export const TITLES: Record<CodeActionName, string> = {
EXPLAIN: "Explain with Datacoves Copilot",
FIX: "Fix with Datacoves Copilot",
IMPROVE: "Improve with Datacoves Copilot",
ADD_TO_CONTEXT: "Add to Datacoves Copilot",
NEW_TASK: "New Datacoves Copilot Task",
} 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: getCodeActionCommand(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 {
if (!vscode.workspace.getConfiguration(Package.name).get<boolean>("enableCodeActions", true)) {
return []
}
const effectiveRange = EditorUtils.getEffectiveRange(document, range)
if (!effectiveRange) {
return []
}
const filePath = EditorUtils.getFilePath(document)
const actions: vscode.CodeAction[] = []
actions.push(
this.createAction(TITLES.ADD_TO_CONTEXT, vscode.CodeActionKind.QuickFix, "addToContext", [
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(TITLES.FIX, vscode.CodeActionKind.QuickFix, "fixCode", [
filePath,
effectiveRange.text,
effectiveRange.range.start.line + 1,
effectiveRange.range.end.line + 1,
relevantDiagnostics.map(EditorUtils.createDiagnosticData),
]),
)
}
} else {
actions.push(
this.createAction(TITLES.EXPLAIN, vscode.CodeActionKind.QuickFix, "explainCode", [
filePath,
effectiveRange.text,
effectiveRange.range.start.line + 1,
effectiveRange.range.end.line + 1,
]),
)
actions.push(
this.createAction(TITLES.IMPROVE, vscode.CodeActionKind.QuickFix, "improveCode", [
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 []
}
}
}