/
validation.ts
205 lines (173 loc) · 5.42 KB
/
validation.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
import * as vscode from 'vscode-languageserver-protocol';
import { isTsDocument } from '../plugins/typescript';
import type { LanguageServiceRuntimeContext } from '../types';
import * as dedupe from '../utils/dedupe';
import { languageFeatureWorker } from '../utils/featureWorkers';
import { EmbeddedDocumentSourceMap } from '../vueDocuments';
export function register(context: LanguageServiceRuntimeContext) {
const responseCache = new Map<
string,
{
nonTs: vscode.Diagnostic[],
tsSemantic: vscode.Diagnostic[],
tsDeclaration: vscode.Diagnostic[],
tsSyntactic: vscode.Diagnostic[],
tsSuggestion: vscode.Diagnostic[],
}
>();
const nonTsCache = new Map<
number,
Map<
string,
{
documentVersion: number,
tsProjectVersion: string | undefined,
errors: vscode.Diagnostic[] | undefined | null,
}
>
>();
const scriptTsCache_semantic: typeof nonTsCache = new Map();
const scriptTsCache_declaration: typeof nonTsCache = new Map();
const scriptTsCache_syntactic: typeof nonTsCache = new Map();
const scriptTsCache_suggestion: typeof nonTsCache = new Map();
return async (uri: string, response?: (result: vscode.Diagnostic[]) => void, isCancel?: () => Promise<boolean>) => {
const cache = responseCache.get(uri) ?? responseCache.set(uri, {
nonTs: [],
tsSemantic: [],
tsDeclaration: [],
tsSuggestion: [],
tsSyntactic: [],
}).get(uri)!;
let errorsDirty = false; // avoid cache error range jitter
await worker(false, {
declaration: true,
semantic: true,
suggestion: true,
syntactic: true,
}, nonTsCache, errors => cache.nonTs = errors ?? []);
doResponse();
await worker(true, { syntactic: true }, scriptTsCache_syntactic, errors => cache.tsSyntactic = errors ?? []);
await worker(true, { suggestion: true }, scriptTsCache_suggestion, errors => cache.tsSuggestion = errors ?? []);
doResponse();
await worker(true, { semantic: true }, scriptTsCache_semantic, errors => cache.tsSemantic = errors ?? []);
doResponse();
await worker(true, { declaration: true }, scriptTsCache_declaration, errors => cache.tsDeclaration = errors ?? []);
return getErrors();
function doResponse() {
if (errorsDirty) {
response?.(getErrors());
errorsDirty = false;
}
}
function getErrors() {
return [
...cache.nonTs,
...cache.tsSyntactic,
...cache.tsSuggestion,
...cache.tsSemantic,
...cache.tsDeclaration,
];
}
async function worker(
isTs: boolean,
options: {
declaration?: boolean,
semantic?: boolean,
suggestion?: boolean,
syntactic?: boolean,
},
cacheMap: typeof nonTsCache,
response: (result: vscode.Diagnostic[] | undefined) => void,
) {
const result = await languageFeatureWorker(
context,
uri,
true,
function* (arg, sourceMap) {
if (sourceMap.embeddedFile.capabilities.diagnostics && sourceMap.embeddedFile.isTsHostFile === isTs) {
yield arg;
}
},
async (plugin, document, arg, sourceMap) => {
// avoid duplicate errors from vue plugin & typescript plugin
if (isTsDocument(document) !== isTs)
return;
if (await isCancel?.())
return;
const pluginId = context.getPluginId(plugin);
const pluginCache = cacheMap.get(pluginId) ?? cacheMap.set(pluginId, new Map()).get(pluginId)!;
const cache = pluginCache.get(document.uri);
const tsProjectVersion = isTs ? context.getTsLs().__internal__.host.getProjectVersion?.() : undefined;
if (!isTs) {
if (cache && cache.documentVersion === document.version) {
return cache.errors;
}
}
else {
if (options.declaration || options.semantic) {
if (cache && cache.documentVersion === document.version && cache.tsProjectVersion === tsProjectVersion) {
return cache.errors;
}
}
else {
if (cache && cache.documentVersion === document.version) {
return cache.errors;
}
}
}
const errors = await plugin.doValidation?.(document, options);
errorsDirty = true;
pluginCache.set(document.uri, {
documentVersion: document.version,
errors,
tsProjectVersion,
});
return errors;
},
(errors, sourceMap) => transformErrorRange(sourceMap, errors),
arr => dedupe.withDiagnostics(arr.flat()),
);
if (!await isCancel?.())
response(result);
}
};
function transformErrorRange(sourceMap: EmbeddedDocumentSourceMap | undefined, errors: vscode.Diagnostic[]) {
const result: vscode.Diagnostic[] = [];
for (const error of errors) {
const _error: vscode.Diagnostic = { ...error };
if (sourceMap) {
const sourceRange = sourceMap.getSourceRange(
error.range.start,
error.range.end,
data => !!data.capabilities.diagnostic,
)?.[0];
if (!sourceRange)
continue;
_error.range = sourceRange;
}
if (_error.relatedInformation) {
const relatedInfos: vscode.DiagnosticRelatedInformation[] = [];
for (const info of _error.relatedInformation) {
for (const sourceLoc of context.vueDocuments.fromEmbeddedLocation(
info.location.uri,
info.location.range.start,
info.location.range.end,
data => !!data.capabilities.diagnostic,
)) {
relatedInfos.push({
location: {
uri: sourceLoc.uri,
range: sourceLoc.range,
},
message: info.message,
});
break;
}
}
_error.relatedInformation = relatedInfos;
}
result.push(_error);
}
return result;
}
}