This repository was archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathlanguageClientFactory.ts
More file actions
93 lines (84 loc) · 4.63 KB
/
languageClientFactory.ts
File metadata and controls
93 lines (84 loc) · 4.63 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict'
import { Executable, LanguageClient, LanguageClientOptions } from 'coc.nvim'
import { inject, injectable, named } from 'inversify'
import * as path from 'path'
import { IConfigurationService, IExtensionContext, Resource } from '../../common/types'
import { IEnvironmentVariablesProvider } from '../../common/variables/types'
import { IEnvironmentActivationService } from '../../interpreter/activation/types'
import { ILanguageClientFactory, ILanguageServerFolderService, IPlatformData, LanguageClientFactory } from '../types'
// tslint:disable:no-require-imports no-require-imports no-var-requires max-classes-per-file
const dotNetCommand = 'dotnet'
const languageClientName = 'Python Tools'
@injectable()
export class BaseLanguageClientFactory implements ILanguageClientFactory {
constructor(@inject(ILanguageClientFactory) @named(LanguageClientFactory.downloaded) private readonly downloadedFactory: ILanguageClientFactory,
@inject(ILanguageClientFactory) @named(LanguageClientFactory.simple) private readonly simpleFactory: ILanguageClientFactory,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(IEnvironmentVariablesProvider) private readonly envVarsProvider: IEnvironmentVariablesProvider,
@inject(IEnvironmentActivationService) private readonly environmentActivationService: IEnvironmentActivationService) { }
public async createLanguageClient(resource: Resource, clientOptions: LanguageClientOptions): Promise<LanguageClient> {
const settings = this.configurationService.getSettings(resource)
const factory = settings.downloadLanguageServer ? this.downloadedFactory : this.simpleFactory
const env = await this.getEnvVars(resource)
return factory.createLanguageClient(resource, clientOptions, env)
}
private async getEnvVars(resource: Resource): Promise<NodeJS.ProcessEnv> {
const envVars = await this.environmentActivationService.getActivatedEnvironmentVariables(resource)
if (envVars && Object.keys(envVars).length > 0) {
return envVars
}
return this.envVarsProvider.getEnvironmentVariables(resource)
}
}
/**
* Creates a langauge client for use by users of the extension.
*
* @export
* @class DownloadedLanguageClientFactory
* @implements {ILanguageClientFactory}
*/
@injectable()
export class DownloadedLanguageClientFactory implements ILanguageClientFactory {
constructor(@inject(IPlatformData) private readonly platformData: IPlatformData,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(ILanguageServerFolderService) private readonly languageServerFolderService: ILanguageServerFolderService,
@inject(IExtensionContext) private readonly context: IExtensionContext) { }
public async createLanguageClient(_resource: Resource, clientOptions: LanguageClientOptions, env?: NodeJS.ProcessEnv): Promise<LanguageClient> {
let serverModule = this.configurationService.getSettings().languageServerPath;
const languageServerFolder = await this.languageServerFolderService.getLanguageServerFolderName()
if(serverModule === "") {
serverModule = path.join(this.context.storagePath, languageServerFolder, this.platformData.engineExecutableName)
}
const serverOptions: Executable = {
command: serverModule,
args: [],
options: { stdio: 'pipe', env }
}
return new LanguageClient('python', serverOptions, clientOptions, false)
}
}
/**
* Creates a language client factory primarily used for LS development purposes.
*
* @export
* @class SimpleLanguageClientFactory
* @implements {ILanguageClientFactory}
*/
@injectable()
export class SimpleLanguageClientFactory implements ILanguageClientFactory {
constructor(@inject(IPlatformData) private readonly platformData: IPlatformData,
@inject(ILanguageServerFolderService) private readonly languageServerFolderService: ILanguageServerFolderService,
@inject(IExtensionContext) private readonly context: IExtensionContext) { }
public async createLanguageClient(_resource: Resource, clientOptions: LanguageClientOptions, env?: NodeJS.ProcessEnv): Promise<LanguageClient> {
const languageServerFolder = await this.languageServerFolderService.getLanguageServerFolderName()
const serverModule = path.join(this.context.storagePath, languageServerFolder, this.platformData.engineDllName)
const serverOptions: Executable = {
command: dotNetCommand,
args: [serverModule],
options: { stdio: 'pipe', env }
}
return new LanguageClient('python', languageClientName, serverOptions, clientOptions)
}
}