/
codeAction.ts
243 lines (222 loc) · 6.77 KB
/
codeAction.ts
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
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver-protocol';
import * as shared from '@volar/shared';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { fileTextChangesToWorkspaceEdit } from './rename';
import * as fixNames from '../utils/fixNames';
import type { GetConfiguration } from '../createLanguageService';
import { URI } from 'vscode-uri';
import { getFormatCodeSettings } from '../configs/getFormatCodeSettings';
import { getUserPreferences } from '../configs/getUserPreferences';
export interface FixAllData {
type: 'fixAll',
uri: string,
fileName: string,
fixIds: {}[],
}
export interface RefactorData {
type: 'refactor',
uri: string,
fileName: string,
refactorName: string,
actionName: string,
range: { pos: number, end: number; },
}
export interface OrganizeImportsData {
type: 'organizeImports',
uri: string,
fileName: string,
}
export type Data = FixAllData | RefactorData | OrganizeImportsData;
export function register(
rootUri: URI,
languageService: ts.LanguageService,
getTextDocument: (uri: string) => TextDocument | undefined,
getConfiguration: GetConfiguration,
) {
return async (uri: string, range: vscode.Range, context: vscode.CodeActionContext) => {
const document = getTextDocument(uri);
if (!document) return;
const [formatOptions, preferences] = await Promise.all([
getFormatCodeSettings(getConfiguration, document.uri),
getUserPreferences(getConfiguration, document.uri, rootUri),
]);
const fileName = shared.getPathOfUri(document.uri);
const start = document.offsetAt(range.start);
const end = document.offsetAt(range.end);
let result: vscode.CodeAction[] = [];
const onlyQuickFix = matchOnlyKind(`${vscode.CodeActionKind.QuickFix}.ts`);
if (!context.only || onlyQuickFix) {
for (const error of context.diagnostics) {
try {
const codeFixes = languageService.getCodeFixesAtPosition(
fileName,
document.offsetAt(error.range.start),
document.offsetAt(error.range.end),
[Number(error.code)],
formatOptions,
preferences,
);
for (const codeFix of codeFixes) {
result = result.concat(transformCodeFix(codeFix, [error], onlyQuickFix ?? vscode.CodeActionKind.Empty));
}
} catch { }
}
}
if (context.only) {
for (const only of context.only) {
if (only.split('.')[0] === vscode.CodeActionKind.Refactor) {
try {
const refactors = languageService.getApplicableRefactors(fileName, { pos: start, end: end }, preferences, undefined, only);
for (const refactor of refactors) {
result = result.concat(transformRefactor(refactor));
}
} catch { }
}
}
}
else {
try {
const refactors = languageService.getApplicableRefactors(fileName, { pos: start, end: end }, preferences, undefined, undefined);
for (const refactor of refactors) {
result = result.concat(transformRefactor(refactor));
}
} catch { }
}
const onlySourceOrganizeImports = matchOnlyKind(`${vscode.CodeActionKind.SourceOrganizeImports}.ts`);
if (onlySourceOrganizeImports) {
const action = vscode.CodeAction.create('Organize Imports', onlySourceOrganizeImports);
action.data = {
type: 'organizeImports',
uri,
fileName,
} satisfies OrganizeImportsData;
result.push(action);
}
const onlySourceFixAll = matchOnlyKind(`${vscode.CodeActionKind.SourceFixAll}.ts`);
if (onlySourceFixAll) {
const action = vscode.CodeAction.create('Fix All', onlySourceFixAll);
action.data = {
uri,
type: 'fixAll',
fileName,
fixIds: [
fixNames.classIncorrectlyImplementsInterface,
fixNames.awaitInSyncFunction,
fixNames.unreachableCode,
],
} satisfies FixAllData;
result.push(action);
}
const onlyRemoveUnused = matchOnlyKind(`${vscode.CodeActionKind.Source}.removeUnused.ts`);
if (onlyRemoveUnused) {
const action = vscode.CodeAction.create('Remove all unused code', onlyRemoveUnused);
action.data = {
uri,
type: 'fixAll',
fileName,
fixIds: [
// not working and throw
fixNames.unusedIdentifier,
// TODO: remove patching
'unusedIdentifier_prefix',
'unusedIdentifier_deleteImports',
'unusedIdentifier_delete',
'unusedIdentifier_infer',
],
} satisfies FixAllData;
result.push(action);
}
const onlyAddMissingImports = matchOnlyKind(`${vscode.CodeActionKind.Source}.addMissingImports.ts`);
if (onlyAddMissingImports) {
const action = vscode.CodeAction.create('Add all missing imports', onlyAddMissingImports);
action.data = {
uri,
type: 'fixAll',
fileName,
fixIds: [
// not working and throw
fixNames.fixImport,
// TODO: remove patching
'fixMissingImport',
],
} satisfies FixAllData;
result.push(action);
}
for (const codeAction of result) {
if (codeAction.diagnostics === undefined) {
codeAction.diagnostics = context.diagnostics;
}
}
return result;
function matchOnlyKind(kind: string) {
if (context.only) {
for (const only of context.only) {
const a = only.split('.');
const b = kind.split('.');
if (a.length <= b.length) {
let matchNums = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] == b[i]) {
matchNums++;
}
}
if (matchNums === a.length)
return only;
}
}
}
}
function transformCodeFix(codeFix: ts.CodeFixAction, diagnostics: vscode.Diagnostic[], kind: vscode.CodeActionKind) {
const edit = fileTextChangesToWorkspaceEdit(codeFix.changes, getTextDocument);
const codeActions: vscode.CodeAction[] = [];
const fix = vscode.CodeAction.create(
codeFix.description,
edit,
kind,
);
fix.diagnostics = diagnostics;
codeActions.push(fix);
if (codeFix.fixAllDescription && codeFix.fixId) {
const fixAll = vscode.CodeAction.create(
codeFix.fixAllDescription,
kind,
);
fixAll.data = {
uri,
type: 'fixAll',
fileName,
fixIds: [codeFix.fixId],
} satisfies FixAllData;
fixAll.diagnostics = diagnostics;
codeActions.push(fixAll);
}
return codeActions;
}
function transformRefactor(refactor: ts.ApplicableRefactorInfo) {
const codeActions: vscode.CodeAction[] = [];
for (const action of refactor.actions) {
const codeAction = vscode.CodeAction.create(
action.description,
action.kind,
);
codeAction.data = {
uri,
type: 'refactor',
fileName,
range: { pos: start, end: end },
refactorName: refactor.name,
actionName: action.name,
} satisfies RefactorData;
if (action.notApplicableReason) {
codeAction.disabled = { reason: action.notApplicableReason };
}
if (refactor.inlineable) {
codeAction.isPreferred = true;
}
codeActions.push(codeAction);
}
return codeActions;
}
};
}