/
rename.ts
323 lines (262 loc) · 9.59 KB
/
rename.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
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
import * as vscode from 'vscode-languageserver-protocol';
import type { LanguageServiceRuntimeContext } from '../types';
import { languageFeatureWorker } from '../utils/featureWorkers';
import * as dedupe from '../utils/dedupe';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { VueDocuments } from '../vueDocuments';
import { EmbeddedFileMappingData } from '@volar/vue-code-gen';
export function register(context: LanguageServiceRuntimeContext) {
return (uri: string, position: vscode.Position, newName: string) => {
return languageFeatureWorker(
context,
uri,
{ position, newName },
function* (arg, sourceMap) {
for (const [mapedRange, mapedData] of sourceMap.getMappedRanges(
arg.position,
arg.position,
data => typeof data.capabilities.rename === 'object' ? data.capabilities.rename.in : !!data.capabilities.rename,
)) {
let newName = arg.newName;
if (mapedData.normalizeNewName)
newName = mapedData.normalizeNewName(arg.newName);
yield { position: mapedRange.start, newName };
}
},
async (plugin, document, arg, sourceMap) => {
const recursiveChecker = dedupe.createLocationSet();
let result: vscode.WorkspaceEdit | undefined;
await withTeleports(document, arg.position, arg.newName);
return result;
async function withTeleports(document: TextDocument, position: vscode.Position, newName: string) {
if (!plugin.doRename)
return;
if (recursiveChecker.has({ uri: document.uri, range: { start: position, end: position } }))
return;
recursiveChecker.add({ uri: document.uri, range: { start: position, end: position } });
const workspaceEdit = await plugin.doRename(document, position, newName);
if (!workspaceEdit)
return;
if (!result)
result = {};
if (workspaceEdit.changes) {
for (const editUri in workspaceEdit.changes) {
const textEdits = workspaceEdit.changes[editUri];
for (const textEdit of textEdits) {
let foundTeleport = false;
if (sourceMap?.embeddedFile.lsType !== 'nonTs') {
recursiveChecker.add({ uri: editUri, range: { start: textEdit.range.start, end: textEdit.range.start } });
const teleport = context.vueDocuments.teleportfromEmbeddedDocumentUri(sourceMap?.embeddedFile.lsType ?? 'script', editUri);
if (teleport) {
for (const [teleRange, sideData] of teleport.findTeleports(
textEdit.range.start,
textEdit.range.end,
sideData => !!sideData.capabilities.references,
)) {
if (recursiveChecker.has({ uri: teleport.document.uri, range: { start: teleRange.start, end: teleRange.start } }))
continue;
foundTeleport = true;
const newName_2 = sideData.transformNewName
? sideData.transformNewName(newName)
: newName;
await withTeleports(teleport.document, teleRange.start, newName_2);
}
}
}
if (!foundTeleport) {
if (!result.changes)
result.changes = {};
if (!result.changes[editUri])
result.changes[editUri] = [];
result.changes[editUri].push(textEdit);
}
}
}
}
if (workspaceEdit.changeAnnotations) {
for (const uri in workspaceEdit.changeAnnotations) {
if (!result.changeAnnotations)
result.changeAnnotations = {};
result.changeAnnotations[uri] = workspaceEdit.changeAnnotations[uri];
}
}
if (workspaceEdit.documentChanges) {
if (!result.documentChanges)
result.documentChanges = [];
result.documentChanges = result.documentChanges.concat(workspaceEdit.documentChanges);
}
}
},
(data, sourceMap) => {
const vueDocument = context.vueDocuments.get(uri);
const renameFromScriptContent = !vueDocument || !vueDocument.getSourceMaps().some(sourceMap => sourceMap.embeddedFile.lsType === 'template' && sourceMap.getMappedRange(position));
return embeddedEditToSourceEdit(
sourceMap?.embeddedFile.lsType ?? 'script',
sourceMap?.embeddedFile.lsType === 'template' && renameFromScriptContent,
data,
context.vueDocuments,
);
},
(workspaceEdits) => {
const mainEdit = workspaceEdits[0];
const otherEdits = workspaceEdits.slice(1);
margeWorkspaceEdits(mainEdit, ...otherEdits);
if (mainEdit.changes) {
for (const uri in mainEdit.changes) {
mainEdit.changes[uri] = dedupe.withTextEdits(mainEdit.changes[uri]);
}
}
return workspaceEdits[0];
},
);
}
}
export function margeWorkspaceEdits(original: vscode.WorkspaceEdit, ...others: vscode.WorkspaceEdit[]) {
for (const other of others) {
for (const uri in other.changeAnnotations) {
if (!original.changeAnnotations) {
original.changeAnnotations = {};
}
original.changeAnnotations[uri] = other.changeAnnotations[uri];
}
for (const uri in other.changes) {
if (!original.changes) {
original.changes = {};
}
if (!original.changes[uri]) {
original.changes[uri] = [];
}
const edits = other.changes[uri];
original.changes[uri] = original.changes[uri].concat(edits);
}
if (other.documentChanges) {
if (!original.documentChanges) {
original.documentChanges = [];
}
for (const docChange of other.documentChanges) {
original.documentChanges.push(docChange);
}
}
}
}
/**
* TODO: rewrite this
*
* Start from Script LS
* -> Access all results
*
* Start from template LS
* -> Start from template content?
* -> Access all results
* -> Start from script content?
* -> Yes: Only access template results
* -> No: Access all results
*/
export function embeddedEditToSourceEdit(
lsType: 'script' | 'template' | 'nonTs',
ignoreScriptLsResult: boolean,
tsResult: vscode.WorkspaceEdit,
vueDocuments: VueDocuments,
) {
const vueResult: vscode.WorkspaceEdit = {};
let hasResult = false;
for (const tsUri in tsResult.changeAnnotations) {
if (!vueResult.changeAnnotations)
vueResult.changeAnnotations = {};
const tsAnno = tsResult.changeAnnotations[tsUri];
const uri = vueDocuments.sourceMapFromEmbeddedDocumentUri(lsType, tsUri)?.sourceDocument.uri ?? tsUri;
vueResult.changeAnnotations[uri] = tsAnno;
}
for (const tsUri in tsResult.changes) {
const tsEdits = tsResult.changes[tsUri];
for (const tsEdit of tsEdits) {
for (const vueLoc of vueDocuments.fromEmbeddedLocation(
lsType,
tsUri,
tsEdit.range.start,
tsEdit.range.end,
data => typeof data.capabilities.rename === 'object' ? data.capabilities.rename.out : !!data.capabilities.rename, // fix https://github.com/johnsoncodehk/volar/issues/1091
)) {
if (ignoreScriptLsResult) {
const isTemplateResult = vueLoc.sourceMap && (vueLoc.data.vueTag === 'template' || vueLoc.data.vueTag === 'style');
if (!isTemplateResult) {
continue;
}
}
let newText_2 = tsEdit.newText;
if (vueLoc.sourceMap && vueLoc.data.applyNewName) {
const vueDoc = vueLoc.sourceMap.sourceDocument;
newText_2 = vueLoc.data.applyNewName(vueDoc.getText(vueLoc.range), tsEdit.newText);
}
if (!vueResult.changes) {
vueResult.changes = {};
}
if (!vueResult.changes[vueLoc.uri]) {
vueResult.changes[vueLoc.uri] = [];
}
vueResult.changes[vueLoc.uri].push({
newText: newText_2,
range: vueLoc.range,
});
hasResult = true;
}
}
}
if (tsResult.documentChanges) {
for (const tsDocEdit of tsResult.documentChanges) {
if (!vueResult.documentChanges) {
vueResult.documentChanges = [];
}
let vueDocEdit: typeof tsDocEdit | undefined;
if (vscode.TextDocumentEdit.is(tsDocEdit)) {
const sourceMap = vueDocuments.sourceMapFromEmbeddedDocumentUri(lsType, tsDocEdit.textDocument.uri);
if (sourceMap) {
vueDocEdit = vscode.TextDocumentEdit.create(
{ uri: sourceMap.sourceDocument.uri, version: sourceMap.sourceDocument.version },
[],
);
for (const tsEdit of tsDocEdit.edits) {
for (const [vueRange, data] of sourceMap.getSourceRanges(
tsEdit.range.start,
tsEdit.range.end,
data => typeof data.capabilities.rename === 'object' ? data.capabilities.rename.out : !!data.capabilities.rename, // fix https://github.com/johnsoncodehk/volar/issues/1091
)) {
if (ignoreScriptLsResult && !(data.vueTag === 'template' || data.vueTag === 'style')) {
continue;
}
vueDocEdit.edits.push({
annotationId: vscode.AnnotatedTextEdit.is(tsEdit.range) ? tsEdit.range.annotationId : undefined,
newText: tsEdit.newText,
range: vueRange,
});
}
}
if (!vueDocEdit.edits.length) {
vueDocEdit = undefined;
}
}
else if (!ignoreScriptLsResult) {
vueDocEdit = tsDocEdit;
}
}
else if (vscode.CreateFile.is(tsDocEdit) && !ignoreScriptLsResult) {
vueDocEdit = tsDocEdit; // TODO: remove .ts?
}
else if (vscode.RenameFile.is(tsDocEdit) && !ignoreScriptLsResult) {
const oldUri = vueDocuments.sourceMapFromEmbeddedDocumentUri(lsType, tsDocEdit.oldUri)?.sourceDocument.uri ?? tsDocEdit.oldUri;
vueDocEdit = vscode.RenameFile.create(oldUri, tsDocEdit.newUri /* TODO: remove .ts? */, tsDocEdit.options, tsDocEdit.annotationId);
}
else if (vscode.DeleteFile.is(tsDocEdit) && !ignoreScriptLsResult) {
const uri = vueDocuments.sourceMapFromEmbeddedDocumentUri(lsType, tsDocEdit.uri)?.sourceDocument.uri ?? tsDocEdit.uri;
vueDocEdit = vscode.DeleteFile.create(uri, tsDocEdit.options, tsDocEdit.annotationId);
}
if (vueDocEdit) {
vueResult.documentChanges.push(vueDocEdit);
hasResult = true;
}
}
}
if (hasResult) {
return vueResult;
}
}