-
-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathinterpolationMode.ts
368 lines (318 loc) · 12.1 KB
/
interpolationMode.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
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
import { LanguageMode } from '../../embeddedSupport/languageModes';
import {
Diagnostic,
TextDocument,
DiagnosticSeverity,
Position,
MarkedString,
Range,
Location,
Definition,
CompletionList,
TextEdit,
CompletionItem
} from 'vscode-languageserver-types';
import { IServiceHost } from '../../services/typescriptService/serviceHost';
import { languageServiceIncludesFile } from '../script/javascript';
import { getFileFsPath } from '../../utils/paths';
import { mapBackRange, mapFromPositionToOffset } from '../../services/typescriptService/sourceMap';
import { URI } from 'vscode-uri';
import * as ts from 'typescript';
import { T_TypeScript } from '../../services/dependencyService';
import * as _ from 'lodash';
import { createTemplateDiagnosticFilter } from '../../services/typescriptService/templateDiagnosticFilter';
import { NULL_COMPLETION } from '../nullMode';
import { toCompletionItemKind } from '../../services/typescriptService/util';
import { LanguageModelCache } from '../../embeddedSupport/languageModelCache';
import { HTMLDocument } from './parser/htmlParser';
import { isInsideInterpolation } from './services/isInsideInterpolation';
export class VueInterpolationMode implements LanguageMode {
private config: any = {};
constructor(
private tsModule: T_TypeScript,
private serviceHost: IServiceHost,
private vueDocuments: LanguageModelCache<HTMLDocument>
) {}
getId() {
return 'vue-html-interpolation';
}
configure(c: any) {
this.config = c;
}
queryVirtualFileInfo(fileName: string, currFileText: string) {
return this.serviceHost.queryVirtualFileInfo(fileName, currFileText);
}
doValidation(document: TextDocument): Diagnostic[] {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return [];
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return [];
}
const templateFileFsPath = getFileFsPath(templateDoc.uri);
// We don't need syntactic diagnostics because
// compiled template is always valid JavaScript syntax.
const rawTemplateDiagnostics = templateService.getSemanticDiagnostics(templateFileFsPath);
const templateDiagnosticFilter = createTemplateDiagnosticFilter(this.tsModule);
return rawTemplateDiagnostics.filter(templateDiagnosticFilter).map(diag => {
// syntactic/semantic diagnostic always has start and length
// so we can safely cast diag to TextSpan
return {
range: mapBackRange(templateDoc, diag as ts.TextSpan, templateSourceMap),
severity: DiagnosticSeverity.Error,
message: ts.flattenDiagnosticMessageText(diag.messageText, '\n'),
code: diag.code,
source: 'Vetur'
};
});
}
doComplete(document: TextDocument, position: Position): CompletionList {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return NULL_COMPLETION;
}
const offset = document.offsetAt(position);
const node = this.vueDocuments.refreshAndGet(document).findNodeBefore(offset);
const nodeRange = Range.create(document.positionAt(node.start), document.positionAt(node.end));
const nodeText = document.getText(nodeRange);
if (!isInsideInterpolation(node, nodeText, offset - node.start)) {
return NULL_COMPLETION;
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return NULL_COMPLETION;
}
/**
* In the cases of empty content inside node
* For example, completion in {{ | }}
* Source map would only map this position {{| }}
* And position mapped back wouldn't fall into any source map ranges
*/
let completionPos = position;
// Case {{ }}
if (node.isInterpolation) {
if (nodeText.match(/{{\s*}}/)) {
completionPos = document.positionAt(node.start + '{{'.length);
}
}
// Todo: Case v-, : or @ directives
const mappedOffset = mapFromPositionToOffset(templateDoc, completionPos, templateSourceMap);
const templateFileFsPath = getFileFsPath(templateDoc.uri);
const completions = templateService.getCompletionsAtPosition(templateFileFsPath, mappedOffset, {
includeCompletionsWithInsertText: true,
includeCompletionsForModuleExports: false
});
if (!completions) {
return NULL_COMPLETION;
}
const tsItems = completions.entries.map((entry, index) => {
return {
uri: templateDoc.uri,
position,
label: entry.name,
sortText: entry.name.startsWith('$') ? '1' + entry.sortText : '0' + entry.sortText,
kind: toCompletionItemKind(entry.kind),
textEdit:
entry.replacementSpan &&
TextEdit.replace(mapBackRange(templateDoc, entry.replacementSpan, templateSourceMap), entry.name),
data: {
// data used for resolving item details (see 'doResolve')
languageId: 'vue-html',
uri: templateDoc.uri,
offset: position,
source: entry.source
}
};
});
return {
isIncomplete: false,
items: tsItems
};
}
doResolve(document: TextDocument, item: CompletionItem): CompletionItem {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return item;
}
/**
* resolve is called for both HTMl and interpolation completions
* HTML completions send back no data
*/
if (!item.data) {
return item;
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return item;
}
const templateFileFsPath = getFileFsPath(templateDoc.uri);
const mappedOffset = mapFromPositionToOffset(templateDoc, item.data.offset, templateSourceMap);
const details = templateService.getCompletionEntryDetails(
templateFileFsPath,
mappedOffset,
item.label,
undefined,
undefined,
undefined
);
if (details) {
item.detail = ts.displayPartsToString(details.displayParts);
item.documentation = ts.displayPartsToString(details.documentation);
delete item.data;
}
return item;
}
doHover(
document: TextDocument,
position: Position
): {
contents: MarkedString[];
range?: Range;
} {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return { contents: [] };
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return {
contents: []
};
}
const templateFileFsPath = getFileFsPath(templateDoc.uri);
const mappedPosition = mapFromPositionToOffset(templateDoc, position, templateSourceMap);
const info = templateService.getQuickInfoAtPosition(templateFileFsPath, mappedPosition);
if (info) {
const display = this.tsModule.displayPartsToString(info.displayParts);
const doc = this.tsModule.displayPartsToString(info.documentation);
const markedContents: MarkedString[] = [{ language: 'ts', value: display }];
if (doc) {
markedContents.unshift(doc, '\n');
}
return {
range: mapBackRange(templateDoc, info.textSpan, templateSourceMap),
contents: markedContents
};
}
return { contents: [] };
}
findDefinition(document: TextDocument, position: Position): Location[] {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return [];
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return [];
}
const templateFileFsPath = getFileFsPath(templateDoc.uri);
const mappedPosition = mapFromPositionToOffset(templateDoc, position, templateSourceMap);
const definitions = templateService.getDefinitionAtPosition(templateFileFsPath, mappedPosition);
if (!definitions) {
return [];
}
const definitionResults: Definition = [];
const program = templateService.getProgram();
if (!program) {
return [];
}
definitions.forEach(r => {
const definitionTargetDoc = r.fileName === templateFileFsPath ? document : getSourceDoc(r.fileName, program);
if (definitionTargetDoc) {
const range =
r.fileName === templateFileFsPath
? mapBackRange(templateDoc, r.textSpan, templateSourceMap)
: convertRange(definitionTargetDoc, r.textSpan);
definitionResults.push({
uri: URI.file(definitionTargetDoc.uri).toString(),
range
});
}
});
return definitionResults;
}
findReferences(document: TextDocument, position: Position): Location[] {
if (!_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true)) {
return [];
}
// Add suffix to process this doc as vue template.
const templateDoc = TextDocument.create(
document.uri + '.template',
document.languageId,
document.version,
document.getText()
);
const { templateService, templateSourceMap } = this.serviceHost.updateCurrentVirtualVueTextDocument(templateDoc);
if (!languageServiceIncludesFile(templateService, templateDoc.uri)) {
return [];
}
const templateFileFsPath = getFileFsPath(templateDoc.uri);
const mappedPosition = mapFromPositionToOffset(templateDoc, position, templateSourceMap);
const references = templateService.getReferencesAtPosition(templateFileFsPath, mappedPosition);
if (!references) {
return [];
}
const referenceResults: Location[] = [];
const program = templateService.getProgram();
if (!program) {
return [];
}
references.forEach(r => {
const referenceTargetDoc = r.fileName === templateFileFsPath ? document : getSourceDoc(r.fileName, program);
if (referenceTargetDoc) {
const range =
r.fileName === templateFileFsPath
? mapBackRange(templateDoc, r.textSpan, templateSourceMap)
: convertRange(referenceTargetDoc, r.textSpan);
referenceResults.push({
uri: URI.file(referenceTargetDoc.uri).toString(),
range
});
}
});
return referenceResults;
}
onDocumentRemoved() {}
dispose() {}
}
function getSourceDoc(fileName: string, program: ts.Program): TextDocument {
const sourceFile = program.getSourceFile(fileName)!;
return TextDocument.create(fileName, 'vue', 0, sourceFile.getFullText());
}
function convertRange(document: TextDocument, span: ts.TextSpan): Range {
const startPosition = document.positionAt(span.start);
const endPosition = document.positionAt(span.start + span.length);
return Range.create(startPosition, endPosition);
}