-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
typescript-contribution.ts
90 lines (80 loc) · 4.07 KB
/
typescript-contribution.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
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, postConstruct, inject } from 'inversify';
import * as path from 'path';
import { ApplicationPackage, environment } from '@theia/application-package';
import { BaseLanguageServerContribution, IConnection, LanguageServerStartOptions } from '@theia/languages/lib/node';
import { TYPESCRIPT_LANGUAGE_ID, TYPESCRIPT_LANGUAGE_NAME, TypescriptStartParams } from '../common';
import { TypeScriptPlugin, TypeScriptInitializeParams, TypeScriptInitializationOptions } from 'typescript-language-server/lib/ts-protocol';
import { isRequestMessage, Message } from 'vscode-ws-jsonrpc';
import { InitializeRequest } from 'vscode-languageserver-protocol';
import { TypescriptVersionURI } from './typescript-version-service-impl';
export interface TypeScriptStartOptions extends LanguageServerStartOptions {
parameters?: TypescriptStartParams
}
@injectable()
export class TypeScriptContribution extends BaseLanguageServerContribution {
readonly id = TYPESCRIPT_LANGUAGE_ID;
readonly name = TYPESCRIPT_LANGUAGE_NAME;
protected readonly plugins: TypeScriptPlugin[] = [];
@inject(ApplicationPackage)
protected readonly applicationPackage: ApplicationPackage;
@postConstruct()
protected init(): void {
for (const extension of this.applicationPackage.extensionPackages) {
const { contributes, installed } = extension.raw;
if (installed && contributes && contributes.typescriptServerPlugins && Array.isArray(contributes.typescriptServerPlugins)) {
for (const plugin of contributes.typescriptServerPlugins) {
this.plugins.push({
name: plugin.name,
location: installed.packagePath
});
}
}
}
}
async start(clientConnection: IConnection, { parameters }: TypeScriptStartOptions): Promise<void> {
// Re-use the same tool used to launch Theia. e.g. for an Electron Theia packaging,
// this will be "electron" executable that is bundled with the application.
const command = process.execPath;
const args: string[] = [
path.join(__dirname, 'startserver.js'),
'--stdio'
];
const tsServerPath = TypescriptVersionURI.getTsServerPath(parameters && parameters.version);
if (tsServerPath) {
args.push(`--tsserver-path=${tsServerPath}`);
}
const serverConnection = await this.createProcessStreamConnectionAsync(command, args, { env: environment.electron.runAsNodeEnv() });
this.forward(clientConnection, serverConnection);
}
protected map(message: Message): Message {
if (isRequestMessage(message)) {
if (message.method === InitializeRequest.type.method) {
const initializeParams = message.params as TypeScriptInitializeParams;
if (this.plugins.length) {
const options: TypeScriptInitializationOptions = {
plugins: [],
...initializeParams.initializationOptions
};
options.plugins.push(...this.plugins);
initializeParams.initializationOptions = options;
}
}
}
return super.map(message);
}
}