Repository navigation
Expand file tree
/
Copy pathlanguageClientFactory.ts
More file actions
66 lines (58 loc) · 3 KB
/
Copy pathlanguageClientFactory.ts
File metadata and controls
66 lines (58 loc) · 3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient';
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../common/constants';
import { IFileSystem } from '../../common/platform/types';
import { Resource } from '../../common/types';
import { PythonInterpreter } from '../../pythonEnvironments/discovery/types';
import { ILanguageClientFactory, ILanguageServerFolderService } from '../types';
import { FileBasedCancellationStrategy } from './cancellationUtils';
// tslint:disable:no-require-imports no-require-imports no-var-requires max-classes-per-file
const languageClientName = 'Python Tools';
@injectable()
export class NodeLanguageClientFactory implements ILanguageClientFactory {
constructor(
@inject(IFileSystem) private readonly fs: IFileSystem,
@inject(ILanguageServerFolderService) private readonly languageServerFolderService: ILanguageServerFolderService
) {}
public async createLanguageClient(
resource: Resource,
_interpreter: PythonInterpreter | undefined,
clientOptions: LanguageClientOptions
): Promise<LanguageClient> {
// this must exist for node language client
const commandArgs = (clientOptions.connectionOptions
?.cancellationStrategy as FileBasedCancellationStrategy).getCommandLineArguments();
const languageServerFolder = await this.languageServerFolderService.getLanguageServerFolderName(resource);
const bundlePath = path.join(EXTENSION_ROOT_DIR, languageServerFolder, 'server.bundle.js');
const nonBundlePath = path.join(EXTENSION_ROOT_DIR, languageServerFolder, 'server.js');
const modulePath = (await this.fs.fileExists(nonBundlePath)) ? nonBundlePath : bundlePath;
const debugOptions = { execArgv: ['--nolazy', '--inspect=6600'] };
// If the extension is launched in debug mode, then the debug server options are used.
const serverOptions: ServerOptions = {
run: {
module: bundlePath,
transport: TransportKind.ipc,
args: commandArgs
},
// In debug mode, use the non-bundled code if it's present. The production
// build includes only the bundled package, so we don't want to crash if
// someone starts the production extension in debug mode.
debug: {
module: modulePath,
transport: TransportKind.ipc,
options: debugOptions,
args: commandArgs
}
};
const vscodeLanguageClient = require('vscode-languageclient') as typeof import('vscode-languageclient');
return new vscodeLanguageClient.LanguageClient(
PYTHON_LANGUAGE,
languageClientName,
serverOptions,
clientOptions
);
}
}