/
vue.ts
288 lines (256 loc) · 8.17 KB
/
vue.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
import * as shared from '@volar/shared';
import type * as ts2 from '@volar/typescript-language-service';
import { EmbeddedLanguageServicePlugin } from '@volar/vue-language-service-types';
import * as html from 'vscode-html-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { VueDocument } from '../vueDocuments';
import useHtmlPlugin from './html';
const dataProvider = html.newHTMLDataProvider('vue', {
version: 1.1,
tags: [
{
name: 'template',
attributes: [
{
name: 'lang',
values: [
{ name: 'html' },
{ name: 'pug' },
],
},
],
},
{
name: 'script',
attributes: [
{
name: 'lang',
values: [
{ name: 'js' },
{ name: 'ts' },
{ name: 'jsx' },
{ name: 'tsx' },
],
},
{ name: 'setup', valueSet: 'v' },
],
},
{
name: 'style',
attributes: [
{
name: 'lang',
values: [
{ name: 'css' },
{ name: 'scss' },
{ name: 'less' },
{ name: 'stylus' },
{ name: 'postcss' },
{ name: 'sass' },
],
},
{ name: 'scoped', valueSet: 'v' },
{ name: 'module', valueSet: 'v' },
],
},
],
globalAttributes: [
{
name: 'src',
},
{
name: 'lang',
// all other embedded languages
values: [
// template
{ name: 'html' },
{ name: 'pug' },
// script
{ name: 'js' },
{ name: 'ts' },
{ name: 'jsx' },
{ name: 'tsx' },
// style
{ name: 'css' },
{ name: 'scss' },
{ name: 'less' },
{ name: 'stylus' },
{ name: 'postcss' },
{ name: 'sass' },
// custom block
{ name: 'md' },
{ name: 'json' },
{ name: 'jsonc' },
{ name: 'yaml' },
{ name: 'toml' },
{ name: 'gql' },
{ name: 'graphql' },
],
}
]
});
export default function (options: {
getVueDocument(document: TextDocument): VueDocument | undefined,
tsLs: ts2.LanguageService | undefined,
}): EmbeddedLanguageServicePlugin {
const htmlPlugin = useHtmlPlugin({
validLang: 'vue',
disableCustomData: true,
});
htmlPlugin.htmlLs.setDataProviders(false, [dataProvider]);
return {
...htmlPlugin,
doValidation(document) {
return worker(document, (vueDocument) => {
const result: vscode.Diagnostic[] = [];
const sfc = vueDocument.file.getDescriptor();
const scriptSetupRanges = vueDocument.file.getScriptSetupRanges();
if (scriptSetupRanges && sfc.scriptSetup) {
for (const range of scriptSetupRanges.notOnTopTypeExports) {
result.push(vscode.Diagnostic.create(
{
start: document.positionAt(range.start + sfc.scriptSetup.startTagEnd),
end: document.positionAt(range.end + sfc.scriptSetup.startTagEnd),
},
'type and interface export statements must be on the top in <script setup>',
vscode.DiagnosticSeverity.Warning,
undefined,
'volar',
));
}
}
if (options.tsLs && !options.tsLs.__internal__.isValidFile(vueDocument.file.getScriptFileName())) {
for (const script of [sfc.script, sfc.scriptSetup]) {
if (!script || script.content === '')
continue;
const error = vscode.Diagnostic.create(
{
start: document.positionAt(script.start),
end: document.positionAt(script.startTagEnd),
},
'Virtual script not found, may missing <script lang="ts"> / "allowJs": true / jsconfig.json.',
vscode.DiagnosticSeverity.Information,
undefined,
'volar',
);
result.push(error);
}
}
if (options.tsLs && sfc.template && vueDocument.file.isJsxMissing()) {
const error = vscode.Diagnostic.create(
{
start: document.positionAt(sfc.template.start),
end: document.positionAt(sfc.template.startTagEnd),
},
'TypeScript intellisense is disabled on template. To enable, configure `"jsx": "preserve"` in the `"compilerOptions"` property of tsconfig or jsconfig. To disable this prompt instead, configure `"experimentalDisableTemplateSupport": true` in `"vueCompilerOptions"` property.',
vscode.DiagnosticSeverity.Information,
undefined,
'volar',
);
result.push(error);
}
return result;
});
},
findDocumentSymbols(document) {
return worker(document, (vueDocument) => {
const result: vscode.SymbolInformation[] = [];
const descriptor = vueDocument.file.getDescriptor();
if (descriptor.template) {
result.push({
name: 'template',
kind: vscode.SymbolKind.Module,
location: vscode.Location.create(document.uri, vscode.Range.create(
document.positionAt(descriptor.template.startTagEnd),
document.positionAt(descriptor.template.startTagEnd + descriptor.template.content.length),
)),
});
}
if (descriptor.script) {
result.push({
name: 'script',
kind: vscode.SymbolKind.Module,
location: vscode.Location.create(document.uri, vscode.Range.create(
document.positionAt(descriptor.script.startTagEnd),
document.positionAt(descriptor.script.startTagEnd + descriptor.script.content.length),
)),
});
}
if (descriptor.scriptSetup) {
result.push({
name: 'script setup',
kind: vscode.SymbolKind.Module,
location: vscode.Location.create(document.uri, vscode.Range.create(
document.positionAt(descriptor.scriptSetup.startTagEnd),
document.positionAt(descriptor.scriptSetup.startTagEnd + descriptor.scriptSetup.content.length),
)),
});
}
for (const style of descriptor.styles) {
result.push({
name: `${['style', style.scoped ? 'scoped' : undefined, style.module ? 'module' : undefined].filter(shared.notEmpty).join(' ')}`,
kind: vscode.SymbolKind.Module,
location: vscode.Location.create(document.uri, vscode.Range.create(
document.positionAt(style.startTagEnd),
document.positionAt(style.startTagEnd + style.content.length),
)),
});
}
for (const customBlock of descriptor.customBlocks) {
result.push({
name: `${customBlock.type}`,
kind: vscode.SymbolKind.Module,
location: vscode.Location.create(document.uri, vscode.Range.create(
document.positionAt(customBlock.startTagEnd),
document.positionAt(customBlock.startTagEnd + customBlock.content.length),
)),
});
}
return result;
});
},
getFoldingRanges(document) {
return worker(document, (vueDocument) => {
const sfcWithEmptyBlocks = getSfcCodeWithEmptyBlocks(vueDocument, document.getText());
const sfcWithEmptyBlocksDocument = TextDocument.create(document.uri, document.languageId, document.version, sfcWithEmptyBlocks);
return htmlPlugin.htmlLs.getFoldingRanges(sfcWithEmptyBlocksDocument);
});
},
getSelectionRanges(document, positions) {
return worker(document, (vueDocument) => {
const sfcWithEmptyBlocks = getSfcCodeWithEmptyBlocks(vueDocument, document.getText());
const sfcWithEmptyBlocksDocument = TextDocument.create(document.uri, document.languageId, document.version, sfcWithEmptyBlocks);
return htmlPlugin.htmlLs.getSelectionRanges(sfcWithEmptyBlocksDocument, positions);
});
},
format: undefined,
};
function worker<T>(document: TextDocument, callback: (vueDocument: VueDocument) => T) {
const vueDocument = options.getVueDocument(document);
if (!vueDocument)
return;
return callback(vueDocument);
}
}
function getSfcCodeWithEmptyBlocks(vueDocument: VueDocument, sfcCode: string) {
const descriptor = vueDocument.file.getDescriptor();
const blocks = [
descriptor.template, // relate to below
descriptor.script,
descriptor.scriptSetup,
...descriptor.styles,
...descriptor.customBlocks,
].filter(shared.notEmpty);
// TODO: keep this for now and check why has this logic later
// if (descriptor.template && descriptor.template.lang !== 'html') {
// blocks.push(descriptor.template);
// }
for (const block of blocks) {
const content = sfcCode.substring(block.startTagEnd, block.startTagEnd + block.content.length);
sfcCode = sfcCode.substring(0, block.startTagEnd)
+ content.split('\n').map(line => ' '.repeat(line.length)).join('\n')
+ sfcCode.substring(block.startTagEnd + block.content.length);
}
return sfcCode;
}