-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
codeActions.ts
125 lines (103 loc) · 3.32 KB
/
codeActions.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
import * as shared from '@volar/shared';
import { transformLocations } from '@volar/transforms';
import * as vscode from 'vscode-languageserver-protocol';
import type { LanguageServiceRuntimeContext } from '../types';
import * as dedupe from '../utils/dedupe';
import { languageFeatureWorker } from '../utils/featureWorkers';
import { embeddedEditToSourceEdit } from './rename';
export interface PluginCodeActionData {
uri: string,
originalItem: vscode.CodeAction,
pluginId: number,
sourceMap: {
embeddedDocumentUri: string;
} | undefined,
}
export function register(context: LanguageServiceRuntimeContext) {
return async (uri: string, range: vscode.Range, codeActionContext: vscode.CodeActionContext) => {
const document = context.getTextDocument(uri);
if (!document)
return;
const offsetRange = {
start: document.offsetAt(range.start),
end: document.offsetAt(range.end),
};
let codeActions = await languageFeatureWorker(
context,
uri,
{ range, codeActionContext },
(arg, sourceMap) => {
if (!sourceMap.embeddedFile.capabilities.codeActions)
return [];
const _codeActionContext: vscode.CodeActionContext = {
diagnostics: transformLocations(
codeActionContext.diagnostics,
range => sourceMap.getMappedRange(range.start, range.end)?.[0],
),
only: codeActionContext.only,
};
let minStart: number | undefined;
let maxEnd: number | undefined;
for (const mapping of sourceMap.mappings) {
const overlapRange = shared.getOverlapRange2(offsetRange, mapping.sourceRange);
if (overlapRange) {
const embeddedRange = sourceMap.getMappedRange(overlapRange.start, overlapRange.end)?.[0];
if (embeddedRange) {
minStart = minStart === undefined ? embeddedRange.start : Math.min(embeddedRange.start, minStart);
maxEnd = maxEnd === undefined ? embeddedRange.end : Math.max(embeddedRange.end, maxEnd);
}
}
}
if (minStart !== undefined && maxEnd !== undefined) {
return [{
range: vscode.Range.create(
sourceMap.mappedDocument.positionAt(minStart),
sourceMap.mappedDocument.positionAt(maxEnd),
),
codeActionContext: _codeActionContext,
}];
}
return [];
},
async (plugin, document, arg, sourceMap) => {
const codeActions = await plugin.codeAction?.on?.(document, arg.range, arg.codeActionContext);
return codeActions?.map(_codeAction => {
const data: PluginCodeActionData = {
uri,
originalItem: _codeAction,
pluginId: context.getPluginId(plugin),
sourceMap: sourceMap ? {
embeddedDocumentUri: sourceMap.mappedDocument.uri,
} : undefined,
};
return <vscode.CodeAction>{
..._codeAction,
data: data,
};
});
},
(_codeActions, sourceMap) => _codeActions.map(_codeAction => {
if (!sourceMap)
return _codeAction;
if (_codeAction.edit) {
const edit = embeddedEditToSourceEdit(
_codeAction.edit,
context.vueDocuments,
);
if (edit) {
_codeAction.edit = edit;
return _codeAction;
}
}
else {
return _codeAction;
}
}).filter(shared.notEmpty),
arr => arr.flat(),
);
if (codeActions) {
codeActions = codeActions.filter(codeAction => codeAction.title.indexOf('__VLS_') === -1);
return dedupe.withCodeAction(codeActions);
}
};
}