forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlanguageClientFactory.ts
More file actions
56 lines (48 loc) · 2.55 KB
/
languageClientFactory.ts
File metadata and controls
56 lines (48 loc) · 2.55 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
import { PYLANCE_EXTENSION_ID, PYTHON_LANGUAGE } from '../../common/constants';
import { IFileSystem } from '../../common/platform/types';
import { IExtensions, Resource } from '../../common/types';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { FileBasedCancellationStrategy } from '../common/cancellationUtils';
import { ILanguageClientFactory } from '../types';
export const PYLANCE_NAME = 'Pylance';
export class NodeLanguageClientFactory implements ILanguageClientFactory {
constructor(private readonly fs: IFileSystem, private readonly extensions: IExtensions) {}
public async createLanguageClient(
_resource: Resource,
_interpreter: PythonEnvironment | undefined,
clientOptions: LanguageClientOptions,
): Promise<LanguageClient> {
// this must exist for node language client
const commandArgs = (
clientOptions.connectionOptions?.cancellationStrategy as FileBasedCancellationStrategy
).getCommandLineArguments();
const extension = this.extensions.getExtension(PYLANCE_EXTENSION_ID);
const languageServerFolder = extension ? extension.extensionPath : '';
const bundlePath = path.join(languageServerFolder, 'dist', 'server.bundle.js');
const nonBundlePath = path.join(languageServerFolder, 'dist', '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,
},
};
return new LanguageClient(PYTHON_LANGUAGE, PYLANCE_NAME, serverOptions, clientOptions);
}
}