-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathvue-template.ts
734 lines (624 loc) · 23.1 KB
/
vue-template.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import type { Disposable, ServiceEnvironment, ServicePluginInstance } from '@volar/language-service';
import { VueGeneratedCode, hyphenateAttr, hyphenateTag, parseScriptSetupRanges, tsCodegen } from '@vue/language-core';
import { camelize, capitalize } from '@vue/shared';
import * as namedPipeClient from '@vue/typescript-plugin/lib/client';
import { create as createHtmlService } from 'volar-service-html';
import { create as createPugService } from 'volar-service-pug';
import * as html from 'vscode-html-languageservice';
import type * as vscode from 'vscode-languageserver-protocol';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { getNameCasing } from '../ideFeatures/nameCasing';
import { AttrNameCasing, ServicePlugin, TagNameCasing, VueCompilerOptions } from '../types';
import { loadModelModifiersData, loadTemplateData } from './data';
let builtInData: html.HTMLDataV1;
let modelData: html.HTMLDataV1;
export function create(
mode: 'html' | 'pug',
ts: typeof import('typescript'),
getVueOptions: (env: ServiceEnvironment) => VueCompilerOptions,
): ServicePlugin {
let customData: html.IHTMLDataProvider[] = [];
const onDidChangeCustomDataListeners = new Set<() => void>();
const onDidChangeCustomData = (listener: () => void): Disposable => {
onDidChangeCustomDataListeners.add(listener);
return {
dispose() {
onDidChangeCustomDataListeners.delete(listener);
},
};
};
const baseServicePlugin = mode === 'pug' ? createPugService : createHtmlService;
const baseService = baseServicePlugin({
getCustomData() {
return customData;
},
onDidChangeCustomData,
});
return {
name: `vue-template (${mode})`,
triggerCharacters: [
...baseService.triggerCharacters ?? [],
'@', // vue event shorthand
],
create(context): ServicePluginInstance {
const baseServiceInstance = baseService.create(context);
const vueCompilerOptions = getVueOptions(context.env);
builtInData ??= loadTemplateData(context.env.locale ?? 'en');
modelData ??= loadModelModifiersData(context.env.locale ?? 'en');
// https://vuejs.org/api/built-in-directives.html#v-on
// https://vuejs.org/api/built-in-directives.html#v-bind
const eventModifiers: Record<string, string> = {};
const propModifiers: Record<string, string> = {};
const vOn = builtInData.globalAttributes?.find(x => x.name === 'v-on');
const vBind = builtInData.globalAttributes?.find(x => x.name === 'v-bind');
if (vOn) {
const markdown = (typeof vOn.description === 'string' ? vOn.description : vOn.description?.value) ?? '';
const modifiers = markdown
.split('\n- ')[4]
.split('\n').slice(2, -1);
for (let text of modifiers) {
text = text.substring(' - `.'.length);
const [name, disc] = text.split('` - ');
eventModifiers[name] = disc;
}
}
if (vBind) {
const markdown = (typeof vBind.description === 'string' ? vBind.description : vBind.description?.value) ?? '';
const modifiers = markdown
.split('\n- ')[4]
.split('\n').slice(2, -1);
for (let text of modifiers) {
text = text.substring(' - `.'.length);
const [name, disc] = text.split('` - ');
propModifiers[name] = disc;
}
}
return {
...baseServiceInstance,
async provideCompletionItems(document, position, completionContext, token) {
if (!isSupportedDocument(document))
return;
let sync: (() => Promise<number>) | undefined;
let currentVersion: number | undefined;
const [_, sourceFile] = context.documents.getVirtualCodeByUri(document.uri);
if (sourceFile?.generated?.code instanceof VueGeneratedCode) {
sync = (await provideHtmlData(sourceFile.id, sourceFile.generated.code)).sync;
currentVersion = await sync();
}
let htmlComplete = await baseServiceInstance.provideCompletionItems?.(document, position, completionContext, token);
while (currentVersion !== (currentVersion = await sync?.())) {
htmlComplete = await baseServiceInstance.provideCompletionItems?.(document, position, completionContext, token);
}
if (!htmlComplete)
return;
if (sourceFile?.generated?.code instanceof VueGeneratedCode) {
await afterHtmlCompletion(
htmlComplete,
context.documents.get(sourceFile.id, sourceFile.languageId, sourceFile.snapshot),
sourceFile.generated.code,
);
}
return htmlComplete;
},
async provideInlayHints(document) {
if (!isSupportedDocument(document))
return;
const enabled = await context.env.getConfiguration?.<boolean>('vue.inlayHints.missingProps') ?? false;
if (!enabled)
return;
const result: vscode.InlayHint[] = [];
const [virtualCode] = context.documents.getVirtualCodeByUri(document.uri);
if (!virtualCode)
return;
for (const map of context.documents.getMaps(virtualCode)) {
const code = context.language.files.get(map.sourceDocument.uri)?.generated?.code;
const scanner = getScanner(baseServiceInstance, document);
if (code instanceof VueGeneratedCode && scanner) {
// visualize missing required props
const casing = await getNameCasing(context, map.sourceDocument.uri);
const components = await namedPipeClient.getComponentNames(code.fileName) ?? [];
const componentProps: Record<string, string[]> = {};
let token: html.TokenType;
let current: {
unburnedRequiredProps: string[];
labelOffset: number;
insertOffset: number;
} | undefined;
while ((token = scanner.scan()) !== html.TokenType.EOS) {
if (token === html.TokenType.StartTag) {
const tagName = scanner.getTokenText();
const component =
tagName.indexOf('.') >= 0
? components.find(component => component === tagName.split('.')[0])
: components.find(component => component === tagName || hyphenateTag(component) === tagName);
const checkTag = tagName.indexOf('.') >= 0 ? tagName : component;
if (checkTag) {
componentProps[checkTag] ??= await namedPipeClient.getComponentProps(code.fileName, checkTag, true) ?? [];
current = {
unburnedRequiredProps: [...componentProps[checkTag]],
labelOffset: scanner.getTokenOffset() + scanner.getTokenLength(),
insertOffset: scanner.getTokenOffset() + scanner.getTokenLength(),
};
}
}
else if (token === html.TokenType.AttributeName) {
if (current) {
let attrText = scanner.getTokenText();
if (attrText === 'v-bind') {
current.unburnedRequiredProps = [];
}
else {
// remove modifiers
if (attrText.indexOf('.') >= 0) {
attrText = attrText.split('.')[0];
}
// normalize
if (attrText.startsWith('v-bind:')) {
attrText = attrText.substring('v-bind:'.length);
}
else if (attrText.startsWith(':')) {
attrText = attrText.substring(':'.length);
}
else if (attrText.startsWith('v-model:')) {
attrText = attrText.substring('v-model:'.length);
}
else if (attrText === 'v-model') {
attrText = vueCompilerOptions.target >= 3 ? 'modelValue' : 'value'; // TODO: support for experimentalModelPropName?
}
else if (attrText.startsWith('@')) {
attrText = 'on-' + hyphenateAttr(attrText.substring('@'.length));
}
current.unburnedRequiredProps = current.unburnedRequiredProps.filter(propName => {
return attrText !== propName
&& attrText !== hyphenateAttr(propName);
});
}
}
}
else if (token === html.TokenType.StartTagSelfClose || token === html.TokenType.StartTagClose) {
if (current) {
for (const requiredProp of current.unburnedRequiredProps) {
result.push({
label: `${requiredProp}!`,
paddingLeft: true,
position: document.positionAt(current.labelOffset),
kind: 2 satisfies typeof vscode.InlayHintKind.Parameter,
textEdits: [{
range: {
start: document.positionAt(current.insertOffset),
end: document.positionAt(current.insertOffset),
},
newText: ` :${casing.attr === AttrNameCasing.Kebab ? hyphenateAttr(requiredProp) : requiredProp}=`,
}],
});
}
current = undefined;
}
}
if (token === html.TokenType.AttributeName || token === html.TokenType.AttributeValue) {
if (current) {
current.insertOffset = scanner.getTokenOffset() + scanner.getTokenLength();
}
}
}
}
}
return result;
},
provideHover(document, position, token) {
if (!isSupportedDocument(document))
return;
if (context.documents.getVirtualCodeByUri(document.uri)[0])
updateCustomData([]);
return baseServiceInstance.provideHover?.(document, position, token);
},
async provideDiagnostics(document, token) {
if (!isSupportedDocument(document))
return;
const originalResult = await baseServiceInstance.provideDiagnostics?.(document, token);
const [virtualCode] = context.documents.getVirtualCodeByUri(document.uri);
if (!virtualCode)
return;
for (const map of context.documents.getMaps(virtualCode)) {
const code = context.language.files.get(map.sourceDocument.uri)?.generated?.code;
if (!(code instanceof VueGeneratedCode))
continue;
const templateErrors: vscode.Diagnostic[] = [];
const { template } = code.sfc;
if (template) {
for (const error of template.errors) {
onCompilerError(error, 1 satisfies typeof vscode.DiagnosticSeverity.Error);
}
for (const warning of template.warnings) {
onCompilerError(warning, 2 satisfies typeof vscode.DiagnosticSeverity.Warning);
}
function onCompilerError(error: NonNullable<typeof template>['errors'][number], severity: vscode.DiagnosticSeverity) {
const templateHtmlRange = {
start: error.loc?.start.offset ?? 0,
end: error.loc?.end.offset ?? 0,
};
let errorMessage = error.message;
templateErrors.push({
range: {
start: document.positionAt(templateHtmlRange.start),
end: document.positionAt(templateHtmlRange.end),
},
severity,
code: error.code,
source: 'vue',
message: errorMessage,
});
}
}
return [
...originalResult ?? [],
...templateErrors,
];
}
},
};
async function provideHtmlData(sourceDocumentUri: string, vueCode: VueGeneratedCode) {
const casing = await getNameCasing(context, sourceDocumentUri);
if (builtInData.tags) {
for (const tag of builtInData.tags) {
if (tag.name === 'slot')
continue;
if (tag.name === 'component')
continue;
if (tag.name === 'template')
continue;
if (casing.tag === TagNameCasing.Kebab) {
tag.name = hyphenateTag(tag.name);
}
else {
tag.name = camelize(capitalize(tag.name));
}
}
}
const promises: Promise<void>[] = [];
const tagInfos = new Map<string, {
attrs: string[];
props: string[];
events: string[];
}>();
let version = 0;
let components: string[] | undefined;
let templateContextProps: string[] | undefined;
updateCustomData([
html.newHTMLDataProvider('vue-template-built-in', builtInData),
{
getId: () => 'vue-template',
isApplicable: () => true,
provideTags: () => {
if (!components) {
promises.push((async () => {
components = (await namedPipeClient.getComponentNames(vueCode.fileName) ?? [])
.filter(name =>
name !== 'Transition'
&& name !== 'TransitionGroup'
&& name !== 'KeepAlive'
&& name !== 'Suspense'
&& name !== 'Teleport'
);
version++;
})());
return [];
}
const scriptSetupRanges = vueCode.sfc.scriptSetup ? parseScriptSetupRanges(ts, vueCode.sfc.scriptSetup.ast, vueCompilerOptions) : undefined;
const names = new Set<string>();
const tags: html.ITagData[] = [];
for (const tag of components) {
if (casing.tag === TagNameCasing.Kebab) {
names.add(hyphenateTag(tag));
}
else if (casing.tag === TagNameCasing.Pascal) {
names.add(tag);
}
}
for (const binding of scriptSetupRanges?.bindings ?? []) {
const name = vueCode.sfc.scriptSetup!.content.substring(binding.start, binding.end);
if (casing.tag === TagNameCasing.Kebab) {
names.add(hyphenateTag(name));
}
else if (casing.tag === TagNameCasing.Pascal) {
names.add(name);
}
}
for (const name of names) {
tags.push({
name: name,
attributes: [],
});
}
return tags;
},
provideAttributes: (tag) => {
namedPipeClient.getTemplateContextProps;
let failed = false;
let tagInfo = tagInfos.get(tag);
if (!tagInfo) {
promises.push((async () => {
const attrs = await namedPipeClient.getElementAttrs(vueCode.fileName, tag) ?? [];
const props = await namedPipeClient.getComponentProps(vueCode.fileName, tag) ?? [];
const events = await namedPipeClient.getComponentEvents(vueCode.fileName, tag) ?? [];
tagInfos.set(tag, {
attrs,
props,
events,
});
version++;
})());
return [];
}
if (failed) {
return [];
}
const { attrs, props, events } = tagInfo;
const attributes: html.IAttributeData[] = [];
const _tsCodegen = tsCodegen.get(vueCode.sfc);
if (_tsCodegen) {
if (!templateContextProps) {
promises.push((async () => {
templateContextProps = await namedPipeClient.getTemplateContextProps(vueCode.fileName) ?? [];
version++;
})());
return [];
}
let ctxVars = [
..._tsCodegen.scriptRanges()?.bindings.map(binding => vueCode.sfc.script!.content.substring(binding.start, binding.end)) ?? [],
..._tsCodegen.scriptSetupRanges()?.bindings.map(binding => vueCode.sfc.scriptSetup!.content.substring(binding.start, binding.end)) ?? [],
...templateContextProps,
];
ctxVars = [...new Set(ctxVars)];
const dirs = ctxVars.map(hyphenateAttr).filter(v => v.startsWith('v-'));
for (const dir of dirs) {
attributes.push(
{
name: dir,
},
);
}
}
const propsSet = new Set(props);
for (const prop of [...props, ...attrs]) {
const isGlobal = !propsSet.has(prop);
const name = casing.attr === AttrNameCasing.Camel ? prop : hyphenateAttr(prop);
if (hyphenateAttr(name).startsWith('on-')) {
const propNameBase = name.startsWith('on-')
? name.slice('on-'.length)
: (name['on'.length].toLowerCase() + name.slice('onX'.length));
const propKey = createInternalItemId('componentEvent', [isGlobal ? '*' : tag, propNameBase]);
attributes.push(
{
name: 'v-on:' + propNameBase,
description: propKey,
},
{
name: '@' + propNameBase,
description: propKey,
},
);
}
{
const propName = name;
const propKey = createInternalItemId('componentProp', [isGlobal ? '*' : tag, propName]);
attributes.push(
{
name: propName,
description: propKey,
},
{
name: ':' + propName,
description: propKey,
},
{
name: 'v-bind:' + propName,
description: propKey,
},
);
}
}
for (const event of events) {
const name = casing.attr === AttrNameCasing.Camel ? event : hyphenateAttr(event);
const propKey = createInternalItemId('componentEvent', [tag, name]);
attributes.push({
name: 'v-on:' + name,
description: propKey,
});
attributes.push({
name: '@' + name,
description: propKey,
});
}
const models: [boolean, string][] = [];
for (const prop of [...props, ...attrs]) {
if (prop.startsWith('onUpdate:')) {
const isGlobal = !propsSet.has(prop);
models.push([isGlobal, prop.substring('onUpdate:'.length)]);
}
}
for (const event of events) {
if (event.startsWith('update:')) {
models.push([false, event.substring('update:'.length)]);
}
}
for (const [isGlobal, model] of models) {
const name = casing.attr === AttrNameCasing.Camel ? model : hyphenateAttr(model);
const propKey = createInternalItemId('componentProp', [isGlobal ? '*' : tag, name]);
attributes.push({
name: 'v-model:' + name,
description: propKey,
});
if (model === 'modelValue') {
attributes.push({
name: 'v-model',
description: propKey,
});
}
}
return attributes;
},
provideValues: () => [],
},
]);
return {
async sync() {
await Promise.all(promises);
return version;
}
};
}
async function afterHtmlCompletion(completionList: vscode.CompletionList, sourceDocument: TextDocument, code: VueGeneratedCode) {
const replacement = getReplacement(completionList, sourceDocument);
const componentNames = new Set(
(await namedPipeClient.getComponentNames(code.fileName) ?? [])
.map(hyphenateTag)
);
if (replacement) {
const isEvent = replacement.text.startsWith('v-on:') || replacement.text.startsWith('@');
const isProp = replacement.text.startsWith('v-bind:') || replacement.text.startsWith(':');
const isModel = replacement.text.startsWith('v-model:') || replacement.text.split('.')[0] === 'v-model';
const hasModifier = replacement.text.includes('.');
const validModifiers =
isEvent ? eventModifiers
: isProp ? propModifiers
: undefined;
const modifiers = replacement.text.split('.').slice(1);
const textWithoutModifier = replacement.text.split('.')[0];
if (validModifiers && hasModifier) {
for (const modifier in validModifiers) {
if (modifiers.includes(modifier))
continue;
const modifierDes = validModifiers[modifier];
const insertText = textWithoutModifier + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier;
const newItem: html.CompletionItem = {
label: modifier,
filterText: insertText,
documentation: {
kind: 'markdown',
value: modifierDes,
},
textEdit: {
range: replacement.textEdit.range,
newText: insertText,
},
kind: 20 satisfies typeof vscode.CompletionItemKind.EnumMember,
};
completionList.items.push(newItem);
}
}
else if (hasModifier && isModel) {
for (const modifier of modelData.globalAttributes ?? []) {
if (modifiers.includes(modifier.name))
continue;
const insertText = textWithoutModifier + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier.name;
const newItem: html.CompletionItem = {
label: modifier.name,
filterText: insertText,
documentation: {
kind: 'markdown',
value: (typeof modifier.description === 'object' ? modifier.description.value : modifier.description)
+ '\n\n' + modifier.references?.map(ref => `[${ref.name}](${ref.url})`).join(' | '),
},
textEdit: {
range: replacement.textEdit.range,
newText: insertText,
},
kind: 20 satisfies typeof vscode.CompletionItemKind.EnumMember,
};
completionList.items.push(newItem);
}
}
}
for (const item of completionList.items) {
const itemIdKey = typeof item.documentation === 'string' ? item.documentation : item.documentation?.value;
const itemId = itemIdKey ? readInternalItemId(itemIdKey) : undefined;
if (itemId) {
item.documentation = undefined;
}
if (item.kind === 10 satisfies typeof vscode.CompletionItemKind.Property && componentNames.has(hyphenateTag(item.label))) {
item.kind = 6 satisfies typeof vscode.CompletionItemKind.Variable;
item.sortText = '\u0000' + (item.sortText ?? item.label);
}
else if (itemId && (itemId.type === 'componentProp' || itemId.type === 'componentEvent')) {
const [componentName] = itemId.args;
if (componentName !== '*') {
item.sortText = '\u0000' + (item.sortText ?? item.label);
}
if (itemId.type === 'componentProp') {
if (componentName !== '*') {
item.kind = 5 satisfies typeof vscode.CompletionItemKind.Field;
}
}
else {
item.kind = componentName !== '*' ? 3 satisfies typeof vscode.CompletionItemKind.Function : 23 satisfies typeof vscode.CompletionItemKind.Event;
}
}
else if (
item.label === 'v-if'
|| item.label === 'v-else-if'
|| item.label === 'v-else'
|| item.label === 'v-for'
) {
item.kind = 14 satisfies typeof vscode.CompletionItemKind.Keyword;
item.sortText = '\u0003' + (item.sortText ?? item.label);
}
else if (item.label.startsWith('v-')) {
item.kind = 3 satisfies typeof vscode.CompletionItemKind.Function;
item.sortText = '\u0002' + (item.sortText ?? item.label);
}
else {
item.sortText = '\u0001' + (item.sortText ?? item.label);
}
}
updateCustomData([]);
}
},
};
function getScanner(service: ServicePluginInstance, document: TextDocument) {
if (mode === 'html') {
return service.provide['html/languageService']().createScanner(document.getText());
}
else {
const pugDocument = service.provide['pug/pugDocument'](document);
if (pugDocument) {
return service.provide['pug/languageService']().createScanner(pugDocument);
}
}
}
function updateCustomData(extraData: html.IHTMLDataProvider[]) {
customData = extraData;
onDidChangeCustomDataListeners.forEach(l => l());
}
function isSupportedDocument(document: TextDocument) {
if (mode === 'pug') {
return document.languageId === 'jade';
}
else {
return document.languageId === 'html';
}
}
};
function createInternalItemId(type: 'componentEvent' | 'componentProp', args: string[]) {
return '__VLS_::' + type + '::' + args.join(',');
}
function readInternalItemId(key: string) {
if (key.startsWith('__VLS_::')) {
const strs = key.split('::');
return {
type: strs[1] as 'componentEvent' | 'componentProp',
args: strs[2].split(','),
};
}
}
function getReplacement(list: html.CompletionList, doc: TextDocument) {
for (const item of list.items) {
if (item.textEdit && 'range' in item.textEdit) {
return {
item: item,
textEdit: item.textEdit,
text: doc.getText(item.textEdit.range)
};
}
}
}