-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathlanguageService.ts
More file actions
331 lines (296 loc) · 14.5 KB
/
Copy pathlanguageService.ts
File metadata and controls
331 lines (296 loc) · 14.5 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import * as os from 'os';
import { dirname } from 'path';
import * as net from 'net';
import { URL } from 'url';
import { LanguageClient, LanguageClientOptions, StreamInfo, DocumentFilter, ErrorAction, CloseAction, RevealOutputChannelOn } from 'vscode-languageclient/node';
import { Disposable, workspace, Uri, TextDocument, WorkspaceConfiguration, OutputChannel, window, WorkspaceFolder } from 'vscode';
import { DisposableProcess, getRLibPaths, getRpath, getInvokeCommand, promptToInstallRPackage, spawn, substituteVariables } from './util';
import { extensionContext } from './extension';
import { CommonOptions } from 'child_process';
export class LanguageService implements Disposable {
private client: LanguageClient | undefined;
private readonly clients: Map<string, LanguageClient> = new Map();
private readonly initSet: Set<string> = new Set();
private readonly config: WorkspaceConfiguration;
private readonly outputChannel: OutputChannel;
constructor() {
this.outputChannel = window.createOutputChannel('R Language Server');
this.client = undefined;
this.config = workspace.getConfiguration('r');
void this.startLanguageService(this);
}
dispose(): Thenable<void> {
return this.stopLanguageService();
}
private spawnServer(client: LanguageClient, command: string, options: CommonOptions & { cwd: string }): DisposableProcess {
const childProcess = spawn(command, undefined, options);
const pid = childProcess.pid || -1;
client.outputChannel.appendLine(`R Language Server (${pid}) started`);
childProcess.stderr.on('data', (chunk: Buffer) => {
client.outputChannel.appendLine(chunk.toString());
});
childProcess.on('exit', (code, signal) => {
client.outputChannel.appendLine(`R Language Server (${pid}) exited ` +
(signal ? `from signal ${signal}` : `with exit code ${code || 'null'}`));
if (code !== 0) {
if (code === 10) {
// languageserver is not installed.
void promptToInstallRPackage(
'languageserver', 'lsp.promptToInstall', options.cwd,
'R package {languageserver} is required to enable R language service features such as code completion, function signature, find references, etc. Do you want to install it?',
'You may need to reopen an R file to start the language service after the package is installed.'
);
} else {
client.outputChannel.show();
}
}
void client.stop();
});
return childProcess;
}
private async createClient(config: WorkspaceConfiguration, selector: DocumentFilter[],
cwd: string, workspaceFolder: WorkspaceFolder | undefined, outputChannel: OutputChannel): Promise<LanguageClient> {
let client: LanguageClient;
const debug = config.get<boolean>('lsp.debug');
const useRenvLibPath = config.get<boolean>('useRenvLibPath') ?? false;
const rPath = await getRpath() || ''; // TODO: Abort gracefully
if (debug) {
console.log(`R path: ${rPath}`);
}
const use_stdio = config.get<boolean>('lsp.use_stdio');
const env = Object.create(process.env) as NodeJS.ProcessEnv;
env.VSCR_LSP_DEBUG = debug ? 'TRUE' : 'FALSE';
env.VSCR_LIB_PATHS = getRLibPaths();
env.VSCR_USE_RENV_LIB_PATH = useRenvLibPath ? 'TRUE' : 'FALSE';
const lang = config.get<string>('lsp.lang');
if (lang !== '') {
env.LANG = lang;
} else if (env.LANG === undefined) {
env.LANG = 'en_US.UTF-8';
}
if (debug) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`LANG: ${env.LANG}`);
}
const rScriptPath = config.get<string>('lsp.bootstrapFile') || extensionContext.asAbsolutePath('R/languageServer.R');
const rLspArgs = (config.get<string[]>('lsp.args')?.map(substituteVariables) ?? []).map((str) => JSON.stringify(str)).join(' ');
const options = { cwd: cwd, env: env, shell: true };
const commandExp = getInvokeCommand() ?? ''; // TODO: Abort gracefully
const command = commandExp.replaceAll('${rpath}', rPath).replaceAll('${r.lsp.args}', rLspArgs).replaceAll('${r.lsp.bootstrapFile}', rScriptPath);
console.log('Language Server Command: ' + command);
const tcpServerOptions = () => new Promise<DisposableProcess | StreamInfo>((resolve, reject) => {
// Use a TCP socket because of problems with blocking STDIO
const server = net.createServer(socket => {
// 'connection' listener
console.log('R process connected');
socket.on('end', () => {
console.log('R process disconnected');
});
socket.on('error', (e: Error) => {
console.log(`R process error: ${e.message}`);
reject(e);
});
server.close();
resolve({ reader: socket, writer: socket });
});
// Listen on random port
server.listen(0, '127.0.0.1', () => {
const port = (server.address() as net.AddressInfo).port;
env.VSCR_LSP_PORT = String(port);
return this.spawnServer(client, command, options);
});
});
// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for selected R documents
documentSelector: selector,
uriConverters: {
// VS Code by default %-encodes even the colon after the drive letter
// NodeJS handles it much better
code2Protocol: uri => new URL(uri.toString(true)).toString(),
protocol2Code: str => Uri.parse(str)
},
workspaceFolder: workspaceFolder,
outputChannel: outputChannel,
synchronize: {
// Synchronize the setting section 'r' to the server
configurationSection: 'r.lsp',
fileEvents: workspace.createFileSystemWatcher('**/*.{R,r}'),
},
revealOutputChannelOn: RevealOutputChannelOn.Never,
errorHandler: {
error: () => {
return {
action: ErrorAction.Continue
};
},
closed: () => {
return {
action: CloseAction.DoNotRestart
};
},
},
};
// Create the language client and start the client.
if (use_stdio && process.platform !== 'win32') {
client = new LanguageClient('r', 'R Language Server', { command: command, options: options }, clientOptions);
} else {
client = new LanguageClient('r', 'R Language Server', tcpServerOptions, clientOptions);
}
extensionContext.subscriptions.push(client);
await client.start();
return client;
}
private checkClient(name: string): boolean {
if (this.initSet.has(name)) {
return true;
}
this.initSet.add(name);
const client = this.clients.get(name);
return (!!client) && client.needsStop();
}
private getKey(uri: Uri): string {
switch (uri.scheme) {
case 'untitled':
return uri.scheme;
case 'vscode-notebook-cell':
return `vscode-notebook:${uri.fsPath}`;
default:
return uri.toString(true);
}
}
private startMultiLanguageService(self: LanguageService): void {
async function didOpenTextDocument(document: TextDocument) {
if (document.uri.scheme !== 'file' && document.uri.scheme !== 'untitled' && document.uri.scheme !== 'vscode-notebook-cell') {
return;
}
if (document.languageId !== 'r' && document.languageId !== 'rmd') {
return;
}
const folder = workspace.getWorkspaceFolder(document.uri);
// Each notebook uses a server started from parent folder
if (document.uri.scheme === 'vscode-notebook-cell') {
const key = self.getKey(document.uri);
if (!self.checkClient(key)) {
console.log(`Start language server for ${document.uri.toString(true)}`);
const documentSelector: DocumentFilter[] = [
{ scheme: 'vscode-notebook-cell', language: 'r', pattern: `${document.uri.fsPath}` },
];
const client = await self.createClient(self.config, documentSelector,
dirname(document.uri.fsPath), folder, self.outputChannel);
self.clients.set(key, client);
self.initSet.delete(key);
}
return;
}
if (folder) {
// Each workspace uses a server started from the workspace folder
const key = self.getKey(folder.uri);
if (!self.checkClient(key)) {
console.log(`Start language server for ${document.uri.toString(true)}`);
const pattern = `${folder.uri.fsPath}/**/*`;
const documentSelector: DocumentFilter[] = [
{ scheme: 'file', language: 'r', pattern: pattern },
{ scheme: 'file', language: 'rmd', pattern: pattern },
];
const client = await self.createClient(self.config, documentSelector, folder.uri.fsPath, folder, self.outputChannel);
self.clients.set(key, client);
self.initSet.delete(key);
}
} else {
// All untitled documents share a server started from home folder
if (document.uri.scheme === 'untitled') {
const key = self.getKey(document.uri);
if (!self.checkClient(key)) {
console.log(`Start language server for ${document.uri.toString(true)}`);
const documentSelector: DocumentFilter[] = [
{ scheme: 'untitled', language: 'r' },
{ scheme: 'untitled', language: 'rmd' },
];
const client = await self.createClient(self.config, documentSelector, os.homedir(), undefined, self.outputChannel);
self.clients.set(key, client);
self.initSet.delete(key);
}
return;
}
// Each file outside workspace uses a server started from parent folder
if (document.uri.scheme === 'file') {
const key = self.getKey(document.uri);
if (!self.checkClient(key)) {
console.log(`Start language server for ${document.uri.toString(true)}`);
const documentSelector: DocumentFilter[] = [
{ scheme: 'file', pattern: document.uri.fsPath },
];
const client = await self.createClient(self.config, documentSelector,
dirname(document.uri.fsPath), undefined, self.outputChannel);
self.clients.set(key, client);
self.initSet.delete(key);
}
return;
}
}
}
function didCloseTextDocument(document: TextDocument): void {
if (document.uri.scheme === 'untitled') {
const result = workspace.textDocuments.find((doc) => doc.uri.scheme === 'untitled');
if (result) {
// Stop the language server when all untitled documents are closed.
return;
}
}
if (document.uri.scheme === 'vscode-notebook-cell') {
const result = workspace.textDocuments.find((doc) =>
doc.uri.scheme === document.uri.scheme && doc.uri.fsPath === document.uri.fsPath);
if (result) {
// Stop the language server when all cell documents are closed (notebook closed).
return;
}
}
// Stop the language server when single file outside workspace is closed, or the above cases.
const key = self.getKey(document.uri);
const client = self.clients.get(key);
if (client) {
self.clients.delete(key);
self.initSet.delete(key);
void client.stop();
}
}
workspace.onDidOpenTextDocument(didOpenTextDocument);
workspace.onDidCloseTextDocument(didCloseTextDocument);
workspace.textDocuments.forEach((doc) => void didOpenTextDocument(doc));
workspace.onDidChangeWorkspaceFolders((event) => {
for (const folder of event.removed) {
const key = self.getKey(folder.uri);
const client = self.clients.get(key);
if (client) {
self.clients.delete(key);
self.initSet.delete(key);
void client.stop();
}
}
});
}
private async startLanguageService(self: LanguageService): Promise<void> {
if (self.config.get<boolean>('r.lsp.multiServer')) {
return this.startMultiLanguageService(self);
} else {
const documentSelector: DocumentFilter[] = [
{ language: 'r' },
{ language: 'rmd' },
];
const workspaceFolder = workspace.workspaceFolders?.[0];
const cwd = workspaceFolder ? workspaceFolder.uri.fsPath : os.homedir();
self.client = await self.createClient(self.config, documentSelector, cwd, workspaceFolder, self.outputChannel);
}
}
private stopLanguageService(): Thenable<void> {
const promises: Thenable<void>[] = [];
if (this.client) {
promises.push(this.client.stop());
}
for (const client of this.clients.values()) {
promises.push(client.stop());
}
return Promise.all(promises).then(() => undefined);
}
}