-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathlanguageClient.ts
125 lines (102 loc) · 3.25 KB
/
languageClient.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
LanguageClientOptions,
RevealOutputChannelOn,
} from 'vscode-languageclient';
import {ServerOptions, LanguageClient} from 'vscode-languageclient/node';
import {window} from 'vscode';
import * as path from 'path';
import {RelayExtensionContext} from './context';
import {createErrorHandler} from './errorHandler';
import {LSPStatusBarFeature} from './lspStatusBarFeature';
import {getConfig} from './config';
export function createAndStartLanguageClient(context: RelayExtensionContext) {
const config = getConfig();
context.primaryOutputChannel.appendLine(
`Using relay binary: ${context.relayBinaryExecutionOptions.binaryPath}`,
);
const args = ['lsp', `--output=${config.lspOutputLevel}`];
if (config.pathToConfig) {
args.push(config.pathToConfig);
}
if (config.pathToLocateCommand) {
args.push(`--locateCommand=${config.pathToLocateCommand}`);
}
const serverOptions: ServerOptions = {
options: {
cwd: context.relayBinaryExecutionOptions.rootPath,
},
command: path.resolve(
context.relayBinaryExecutionOptions.rootPath,
context.relayBinaryExecutionOptions.binaryPath,
),
args,
};
// Options to control the language client
const clientOptions: LanguageClientOptions = {
markdown: {
isTrusted: true,
},
documentSelector: [
{scheme: 'file', language: 'javascript'},
{scheme: 'file', language: 'typescript'},
{scheme: 'file', language: 'typescriptreact'},
{scheme: 'file', language: 'javascriptreact'},
],
outputChannel: context.lspOutputChannel,
// Since we use stderr for debug logs, the "Something went wrong" popup
// in VSCode shows up a lot. This tells vscode not to show it in any case.
revealOutputChannelOn: RevealOutputChannelOn.Never,
initializationFailedHandler: error => {
context?.primaryOutputChannel.appendLine(
`initializationFailedHandler ${error}`,
);
return true;
},
errorHandler: createErrorHandler(context),
};
// Create the language client and start the client.
const client = new LanguageClient(
'RelayLanguageClient',
'Relay Language Client',
serverOptions,
clientOptions,
);
client.registerFeature(new LSPStatusBarFeature(context));
context.primaryOutputChannel.appendLine(
`Starting the Relay Language Server with these options: ${JSON.stringify(
serverOptions,
)}`,
);
// Start the client. This will also launch the server
client.start();
context.client = client;
}
type DidNotError = boolean;
export async function killLanguageClient(
context: RelayExtensionContext,
): Promise<DidNotError> {
if (!context.client) {
return true;
}
return context.client
.stop()
.then(() => {
context.primaryOutputChannel.appendLine(
'Successfully stopped existing relay lsp client',
);
context.client = null;
return true;
})
.catch(() => {
window.showErrorMessage(
'An error occurred while trying to stop the Relay LSP Client. Try restarting VSCode.',
);
return false;
});
}