forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeActions.ts
More file actions
418 lines (363 loc) · 10.2 KB
/
codeActions.ts
File metadata and controls
418 lines (363 loc) · 10.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { LSPPlugin } from "@codemirror/lsp-client";
import { EditorView } from "@codemirror/view";
import toast from "components/toast";
import select from "dialogs/select";
import type {
CodeAction,
CodeActionContext,
CodeActionKind,
Command,
Diagnostic,
Range as LspRange,
WorkspaceEdit,
} from "vscode-languageserver-types";
import type { Position, Range } from "./types";
import type AcodeWorkspace from "./workspace";
type CodeActionResponse = (CodeAction | Command)[] | null;
const CODE_ACTION_KINDS = {
QUICK_FIX: "quickfix",
REFACTOR: "refactor",
REFACTOR_EXTRACT: "refactor.extract",
REFACTOR_INLINE: "refactor.inline",
REFACTOR_REWRITE: "refactor.rewrite",
SOURCE: "source",
SOURCE_ORGANIZE_IMPORTS: "source.organizeImports",
SOURCE_FIX_ALL: "source.fixAll",
} as const;
const CODE_ACTION_ICONS: Record<string, string> = {
quickfix: "build",
refactor: "code",
"refactor.extract": "call_split",
"refactor.inline": "call_merge",
"refactor.rewrite": "edit",
source: "settings",
"source.organizeImports": "sort",
"source.fixAll": "done_all",
};
function getCodeActionIcon(kind?: CodeActionKind): string {
if (!kind) return "icon zap";
for (const [prefix, icon] of Object.entries(CODE_ACTION_ICONS)) {
if (kind.startsWith(prefix)) return icon;
}
return "icon zap";
}
function formatCodeActionKind(kind?: CodeActionKind): string {
if (!kind) return "";
return kind
.split(".")
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
.join(" › ");
}
function isCommand(item: CodeAction | Command): item is Command {
return (
"command" in item && typeof item.command === "string" && !("edit" in item)
);
}
function lspPositionToOffset(
doc: { line: (n: number) => { from: number } },
pos: Position,
): number {
return doc.line(pos.line + 1).from + pos.character;
}
async function requestCodeActions(
plugin: LSPPlugin,
range: LspRange,
diagnostics: Diagnostic[] = [],
): Promise<CodeActionResponse> {
const context: CodeActionContext = {
diagnostics,
triggerKind: 1, // CodeActionTriggerKind.Invoked
};
return plugin.client.request<
{
textDocument: { uri: string };
range: LspRange;
context: CodeActionContext;
},
CodeActionResponse
>("textDocument/codeAction", {
textDocument: { uri: plugin.uri },
range,
context,
});
}
async function resolveCodeAction(
plugin: LSPPlugin,
action: CodeAction,
): Promise<CodeAction> {
// If action already has an edit, no need to resolve
if (action.edit) return action;
const capabilities = plugin.client.serverCapabilities;
const provider = capabilities?.codeActionProvider;
const supportsResolve =
typeof provider === "object" &&
provider !== null &&
"resolveProvider" in provider &&
provider.resolveProvider === true;
if (!supportsResolve) return action;
// Resolve to get the edit property (lazy computation per LSP 3.16+)
try {
const resolved = await plugin.client.request<CodeAction, CodeAction>(
"codeAction/resolve",
action,
);
return resolved ?? action;
} catch (error) {
console.warn("[LSP:CodeAction] Failed to resolve:", error);
return action;
}
}
async function executeCommand(
plugin: LSPPlugin,
command: Command,
): Promise<boolean> {
try {
await plugin.client.request<
{ command: string; arguments?: unknown[] },
unknown
>("workspace/executeCommand", {
command: command.command,
arguments: command.arguments,
});
return true;
} catch (error) {
// -32601 = Method not implemented (expected for some LSP servers)
const lspError = error as { code?: number };
if (lspError?.code !== -32601) {
console.warn("[LSP:CodeAction] Command execution failed:", error);
}
return false;
}
}
interface LspChange {
range: Range;
newText: string;
}
async function applyChangesToFile(
workspace: AcodeWorkspace,
uri: string,
changes: LspChange[],
mapping: { mapPosition: (uri: string, pos: Position) => number },
): Promise<boolean> {
const file = workspace.getFile(uri);
if (file) {
const view = file.getView();
if (view) {
view.dispatch({
changes: changes.map((c) => ({
from: mapping.mapPosition(uri, c.range.start),
to: mapping.mapPosition(uri, c.range.end),
insert: c.newText,
})),
userEvent: "codeAction",
});
return true;
}
}
const displayedView = await workspace.displayFile(uri);
if (!displayedView?.state?.doc) {
console.warn(`[LSP:CodeAction] Could not open file: ${uri}`);
return false;
}
displayedView.dispatch({
changes: changes.map((c) => ({
from: lspPositionToOffset(displayedView.state.doc, c.range.start),
to: lspPositionToOffset(displayedView.state.doc, c.range.end),
insert: c.newText,
})),
userEvent: "codeAction",
});
return true;
}
async function applyWorkspaceEdit(
view: EditorView,
edit: WorkspaceEdit,
): Promise<boolean> {
const plugin = LSPPlugin.get(view);
if (!plugin) return false;
const workspace = plugin.client.workspace as AcodeWorkspace;
if (!workspace) return false;
let filesChanged = 0;
const result = await plugin.client.withMapping(async (mapping) => {
// Handle simple changes format
if (edit.changes) {
for (const uri in edit.changes) {
const changes = edit.changes[uri] as LspChange[];
if (
changes.length &&
(await applyChangesToFile(workspace, uri, changes, mapping))
) {
filesChanged++;
}
}
}
// Handle documentChanges format (supports versioned edits)
if (edit.documentChanges) {
for (const docChange of edit.documentChanges) {
if ("textDocument" in docChange && "edits" in docChange) {
const uri = docChange.textDocument.uri;
const edits = docChange.edits as LspChange[];
if (
edits.length &&
(await applyChangesToFile(workspace, uri, edits, mapping))
) {
filesChanged++;
}
}
}
}
return filesChanged;
});
return (result ?? 0) > 0;
}
/**
* Apply a code action following the LSP spec:
* "If both edit and command are supplied, first the edit is applied, then the command is executed"
*/
async function applyCodeAction(
view: EditorView,
action: CodeAction,
): Promise<boolean> {
const plugin = LSPPlugin.get(view);
if (!plugin) return false;
plugin.client.sync();
// Resolve to get the edit if not already present
const resolved = await resolveCodeAction(plugin, action);
let success = false;
// Step 1: Apply workspace edit if present
if (resolved.edit) {
success = await applyWorkspaceEdit(view, resolved.edit);
}
// Step 2: Execute command if present (after edit per LSP spec)
if (resolved.command) {
const commandSuccess = await executeCommand(plugin, resolved.command);
success = success || commandSuccess;
}
plugin.client.sync();
return success;
}
export interface CodeActionItem {
title: string;
kind?: CodeActionKind;
icon: string;
isPreferred?: boolean;
disabled?: boolean;
disabledReason?: string;
action: CodeAction | Command;
}
export async function fetchCodeActions(
view: EditorView,
): Promise<CodeActionItem[]> {
const plugin = LSPPlugin.get(view);
if (!plugin) return [];
const capabilities = plugin.client.serverCapabilities;
if (!capabilities?.codeActionProvider) return [];
const { from, to } = view.state.selection.main;
const range: LspRange = {
start: plugin.toPosition(from),
end: plugin.toPosition(to),
};
plugin.client.sync();
try {
const response = await requestCodeActions(plugin, range);
if (!response?.length) return [];
const items: CodeActionItem[] = response.map((item) => {
if (isCommand(item)) {
return { title: item.title, icon: "terminal", action: item };
}
return {
title: item.title,
kind: item.kind,
icon: getCodeActionIcon(item.kind),
isPreferred: item.isPreferred,
disabled: !!item.disabled,
disabledReason: item.disabled?.reason,
action: item,
};
});
// Sort: preferred first, then quickfixes, then alphabetically
items.sort((a, b) => {
if (a.isPreferred && !b.isPreferred) return -1;
if (!a.isPreferred && b.isPreferred) return 1;
if (a.kind?.startsWith("quickfix") && !b.kind?.startsWith("quickfix"))
return -1;
if (!a.kind?.startsWith("quickfix") && b.kind?.startsWith("quickfix"))
return 1;
return a.title.localeCompare(b.title);
});
return items;
} catch (error) {
console.error("[LSP:CodeAction] Failed to fetch:", error);
return [];
}
}
export async function executeCodeAction(
view: EditorView,
item: CodeActionItem,
): Promise<boolean> {
const plugin = LSPPlugin.get(view);
if (!plugin) return false;
try {
plugin.client.sync();
// Handle standalone Command (not CodeAction)
if (isCommand(item.action)) {
return executeCommand(plugin, item.action);
}
// Handle CodeAction
return applyCodeAction(view, item.action);
} catch (error) {
console.error("[LSP:CodeAction] Failed to execute:", error);
return false;
}
}
export function supportsCodeActions(view: EditorView): boolean {
const plugin = LSPPlugin.get(view);
return !!plugin?.client.serverCapabilities?.codeActionProvider;
}
export async function showCodeActionsMenu(view: EditorView): Promise<boolean> {
if (!supportsCodeActions(view)) return false;
const items = await fetchCodeActions(view);
if (!items.length) {
toast("No code actions available");
return false;
}
const selectItems = items.map((item, i) => ({
value: String(i),
text: item.title,
icon: item.icon,
disabled: item.disabled,
}));
try {
const result = await select(
strings["code actions"] || "Code Actions",
selectItems as unknown as string[],
{ hideOnSelect: true },
);
if (result !== null && result !== undefined) {
const index = Number.parseInt(String(result), 10);
if (!Number.isNaN(index) && index >= 0 && index < items.length) {
await executeCodeAction(view, items[index]);
view.focus();
return true;
}
}
} catch {
// User cancelled selection
}
view.focus();
return false;
}
export async function performQuickFix(view: EditorView): Promise<boolean> {
const items = await fetchCodeActions(view);
if (!items.length) return false;
// Find preferred action or first quickfix
const quickFix =
items.find((i) => i.isPreferred) ??
items.find((i) => i.kind?.startsWith("quickfix"));
if (quickFix) {
return executeCodeAction(view, quickFix);
}
// Fall back to showing menu
return showCodeActionsMenu(view);
}
export { CODE_ACTION_KINDS, formatCodeActionKind, getCodeActionIcon };