-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
typeScriptServiceClientHost.ts
278 lines (236 loc) · 10.2 KB
/
typeScriptServiceClientHost.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------
* Includes code from typescript-sublime-plugin project, obtained from
* https://github.com/Microsoft/TypeScript-Sublime-Plugin/blob/master/TypeScript%20Indent.tmPreferences
* ------------------------------------------------------------------------------------------ */
import * as vscode from 'vscode';
import { DiagnosticKind } from './features/diagnostics';
import FileConfigurationManager from './features/fileConfigurationManager';
import LanguageProvider from './languageProvider';
import * as Proto from './protocol';
import * as PConst from './protocol.const';
import TypeScriptServiceClient from './typescriptServiceClient';
import { coalesce, flatten } from './utils/arrays';
import { CommandManager } from './utils/commandManager';
import { Disposable } from './utils/dispose';
import { DiagnosticLanguage, LanguageDescription } from './utils/languageDescription';
import LogDirectoryProvider from './utils/logDirectoryProvider';
import { PluginManager } from './utils/plugins';
import * as typeConverters from './utils/typeConverters';
import TypingsStatus, { AtaProgressReporter } from './utils/typingsStatus';
import VersionStatus from './utils/versionStatus';
// Style check diagnostics that can be reported as warnings
const styleCheckDiagnostics = [
6133, // variable is declared but never used
6138, // property is declared but its value is never read
6192, // All imports are unused
7027, // unreachable code detected
7028, // unused label
7029, // fall through case in switch
7030 // not all code paths return a value
];
export default class TypeScriptServiceClientHost extends Disposable {
private readonly client: TypeScriptServiceClient;
private readonly languages: LanguageProvider[] = [];
private readonly languagePerId = new Map<string, LanguageProvider>();
private readonly typingsStatus: TypingsStatus;
private readonly versionStatus: VersionStatus;
private readonly fileConfigurationManager: FileConfigurationManager;
private reportStyleCheckAsWarnings: boolean = true;
constructor(
descriptions: LanguageDescription[],
workspaceState: vscode.Memento,
pluginManager: PluginManager,
private readonly commandManager: CommandManager,
logDirectoryProvider: LogDirectoryProvider,
onCompletionAccepted: (item: vscode.CompletionItem) => void,
) {
super();
const allModeIds = this.getAllModeIds(descriptions, pluginManager);
this.client = this._register(new TypeScriptServiceClient(
workspaceState,
version => this.versionStatus.onDidChangeTypeScriptVersion(version),
pluginManager,
logDirectoryProvider,
allModeIds));
this.client.onDiagnosticsReceived(({ kind, resource, diagnostics }) => {
this.diagnosticsReceived(kind, resource, diagnostics);
}, null, this._disposables);
this.client.onConfigDiagnosticsReceived(diag => this.configFileDiagnosticsReceived(diag), null, this._disposables);
this.client.onResendModelsRequested(() => this.populateService(), null, this._disposables);
this.versionStatus = this._register(new VersionStatus(this.client, commandManager));
this._register(new AtaProgressReporter(this.client));
this.typingsStatus = this._register(new TypingsStatus(this.client));
this.fileConfigurationManager = this._register(new FileConfigurationManager(this.client));
for (const description of descriptions) {
const manager = new LanguageProvider(this.client, description, this.commandManager, this.client.telemetryReporter, this.typingsStatus, this.fileConfigurationManager, onCompletionAccepted);
this.languages.push(manager);
this._register(manager);
this.languagePerId.set(description.id, manager);
}
import('./features/updatePathsOnRename').then(module =>
this._register(module.register(this.client, this.fileConfigurationManager, uri => this.handles(uri))));
import('./features/workspaceSymbols').then(module =>
this._register(module.register(this.client, allModeIds)));
this.client.ensureServiceStarted();
this.client.onReady(() => {
const languages = new Set<string>();
for (const plugin of pluginManager.plugins) {
for (const language of plugin.languages) {
languages.add(language);
}
}
if (languages.size) {
const description: LanguageDescription = {
id: 'typescript-plugins',
modeIds: Array.from(languages.values()),
diagnosticSource: 'ts-plugin',
diagnosticLanguage: DiagnosticLanguage.TypeScript,
diagnosticOwner: 'typescript',
isExternal: true
};
const manager = new LanguageProvider(this.client, description, this.commandManager, this.client.telemetryReporter, this.typingsStatus, this.fileConfigurationManager, onCompletionAccepted);
this.languages.push(manager);
this._register(manager);
this.languagePerId.set(description.id, manager);
}
});
this.client.onTsServerStarted(() => {
this.triggerAllDiagnostics();
});
vscode.workspace.onDidChangeConfiguration(this.configurationChanged, this, this._disposables);
this.configurationChanged();
}
private getAllModeIds(descriptions: LanguageDescription[], pluginManager: PluginManager) {
const allModeIds = flatten([
...descriptions.map(x => x.modeIds),
...pluginManager.plugins.map(x => x.languages)
]);
return allModeIds;
}
public get serviceClient(): TypeScriptServiceClient {
return this.client;
}
public reloadProjects(): void {
this.client.executeWithoutWaitingForResponse('reloadProjects', null);
this.triggerAllDiagnostics();
}
public async handles(resource: vscode.Uri): Promise<boolean> {
const provider = await this.findLanguage(resource);
if (provider) {
return true;
}
return this.client.bufferSyncSupport.handles(resource);
}
private configurationChanged(): void {
const typescriptConfig = vscode.workspace.getConfiguration('typescript');
this.reportStyleCheckAsWarnings = typescriptConfig.get('reportStyleChecksAsWarnings', true);
}
private async findLanguage(resource: vscode.Uri): Promise<LanguageProvider | undefined> {
try {
const doc = await vscode.workspace.openTextDocument(resource);
return this.languages.find(language => language.handles(resource, doc));
} catch {
return undefined;
}
}
private triggerAllDiagnostics() {
for (const language of this.languagePerId.values()) {
language.triggerAllDiagnostics();
}
}
private populateService(): void {
this.fileConfigurationManager.reset();
// See https://github.com/Microsoft/TypeScript/issues/5530
vscode.workspace.saveAll(false).then(() => {
for (const language of this.languagePerId.values()) {
language.reInitialize();
}
});
}
private async diagnosticsReceived(
kind: DiagnosticKind,
resource: vscode.Uri,
diagnostics: Proto.Diagnostic[]
): Promise<void> {
const language = await this.findLanguage(resource);
if (language) {
language.diagnosticsReceived(
kind,
resource,
this.createMarkerDatas(diagnostics, language.diagnosticSource));
}
}
private configFileDiagnosticsReceived(event: Proto.ConfigFileDiagnosticEvent): void {
// See https://github.com/Microsoft/TypeScript/issues/10384
const body = event.body;
if (!body || !body.diagnostics || !body.configFile) {
return;
}
this.findLanguage(this.client.toResource(body.configFile)).then(language => {
if (!language) {
return;
}
language.configFileDiagnosticsReceived(this.client.toResource(body.configFile), body.diagnostics.map(tsDiag => {
const range = tsDiag.start && tsDiag.end ? typeConverters.Range.fromTextSpan(tsDiag) : new vscode.Range(0, 0, 0, 1);
const diagnostic = new vscode.Diagnostic(range, body.diagnostics[0].text, this.getDiagnosticSeverity(tsDiag));
diagnostic.source = language.diagnosticSource;
return diagnostic;
}));
});
}
private createMarkerDatas(
diagnostics: Proto.Diagnostic[],
source: string
): (vscode.Diagnostic & { reportUnnecessary: any })[] {
return diagnostics.map(tsDiag => this.tsDiagnosticToVsDiagnostic(tsDiag, source));
}
private tsDiagnosticToVsDiagnostic(diagnostic: Proto.Diagnostic, source: string): vscode.Diagnostic & { reportUnnecessary: any } {
const { start, end, text } = diagnostic;
const range = new vscode.Range(typeConverters.Position.fromLocation(start), typeConverters.Position.fromLocation(end));
const converted = new vscode.Diagnostic(range, text, this.getDiagnosticSeverity(diagnostic));
converted.source = diagnostic.source || source;
if (diagnostic.code) {
converted.code = diagnostic.code;
}
const relatedInformation = diagnostic.relatedInformation;
if (relatedInformation) {
converted.relatedInformation = coalesce(relatedInformation.map((info: any) => {
const span = info.span;
if (!span) {
return undefined;
}
return new vscode.DiagnosticRelatedInformation(typeConverters.Location.fromTextSpan(this.client.toResource(span.file), span), info.message);
}));
}
if (diagnostic.reportsUnnecessary) {
converted.tags = [vscode.DiagnosticTag.Unnecessary];
}
(converted as vscode.Diagnostic & { reportUnnecessary: any }).reportUnnecessary = diagnostic.reportsUnnecessary;
return converted as vscode.Diagnostic & { reportUnnecessary: any };
}
private getDiagnosticSeverity(diagnostic: Proto.Diagnostic): vscode.DiagnosticSeverity {
if (this.reportStyleCheckAsWarnings
&& this.isStyleCheckDiagnostic(diagnostic.code)
&& diagnostic.category === PConst.DiagnosticCategory.error
) {
return vscode.DiagnosticSeverity.Warning;
}
switch (diagnostic.category) {
case PConst.DiagnosticCategory.error:
return vscode.DiagnosticSeverity.Error;
case PConst.DiagnosticCategory.warning:
return vscode.DiagnosticSeverity.Warning;
case PConst.DiagnosticCategory.suggestion:
return vscode.DiagnosticSeverity.Hint;
default:
return vscode.DiagnosticSeverity.Error;
}
}
private isStyleCheckDiagnostic(code: number | undefined): boolean {
return code ? styleCheckDiagnostics.indexOf(code) !== -1 : false;
}
}