Skip to content

Commit

Permalink
use pylance client if enabled and supported
Browse files Browse the repository at this point in the history
  • Loading branch information
heejaechang committed Sep 29, 2022
1 parent 5443b5e commit aba8cc7
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/client/activation/node/languageServerProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ namespace GetExperimentValue {
}
}

interface PylanceApi {
startClient?(): Promise<void>;
stopClient?(): Promise<void>;
}

export class NodeLanguageServerProxy implements ILanguageServerProxy {
public languageClient: LanguageClient | undefined;
Expand All @@ -63,19 +67,18 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {

private lsVersion: string | undefined;

private usePylanceClient = false;
private pylanceApi
private pylanceApi: PylanceApi | undefined;

constructor(
private readonly factory: ILanguageClientFactory,
private readonly experimentService: IExperimentService,
private readonly interpreterPathService: IInterpreterPathService,
private readonly environmentService: IEnvironmentVariablesProvider,
private readonly workspace: IWorkspaceService,
private readonly extensions: IExtensions,
configurationService: IConfigurationService,
private readonly configurationService: IConfigurationService,
) {
this.usePylanceClient = configurationService.getSettings().pylanceLspClientEnabled;
const extension = this.extensions.getExtension(PYLANCE_EXTENSION_ID);
// Empty
}

private static versionTelemetryProps(instance: NodeLanguageServerProxy) {
Expand All @@ -102,11 +105,13 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
interpreter: PythonEnvironment | undefined,
options: LanguageClientOptions,
): Promise<void> {

const extension = await this.getPylanceExtension();
this.lsVersion = extension?.packageJSON.version || '0';

if (this.usePylanceClient) {
await = extension.
const usePylanceClient = this.configurationService.getSettings().pylanceLspClientEnabled;
if (usePylanceClient && extension && (extension.exports as PylanceApi).startClient) {
this.pylanceApi = extension.exports as PylanceApi;
await this.pylanceApi.startClient!();
} else {
this.cancellationStrategy = new FileBasedCancellationStrategy();
options.connectionOptions = { cancellationStrategy: this.cancellationStrategy };
Expand All @@ -121,13 +126,19 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
);

await client.start();

this.languageClient = client;
}
}

@traceDecoratorVerbose('Disposing language server')
public async stop(): Promise<void> {
if (this.pylanceApi) {
this.pylanceApi.stopClient!();
this.pylanceApi = undefined;

return;
}

while (this.disposables.length > 0) {
const d = this.disposables.shift()!;
d.dispose();
Expand Down Expand Up @@ -220,4 +231,17 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
})),
);
}

private async getPylanceExtension() {
const extension = this.extensions.getExtension(PYLANCE_EXTENSION_ID);
if (!extension) {
return undefined;
}

if (!extension.isActive) {
await extension.activate();
}

return extension;
}
}

0 comments on commit aba8cc7

Please sign in to comment.