Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/client/activation/common/outputChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import { ILanguageServerOutputChannel } from '../types';
@injectable()
export class LanguageServerOutputChannel implements ILanguageServerOutputChannel {
public output: ILogOutputChannel | undefined;

private registered = false;

constructor(
@inject(IApplicationShell) private readonly appShell: IApplicationShell,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IDisposableRegistry) private readonly disposable: IDisposableRegistry,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
) {}

public get channel(): ILogOutputChannel {
if (!this.output) {
this.output = this.appShell.createOutputChannel(OutputChannelNames.languageServer);
this.disposable.push(this.output);
this.output = this.appShell.createOutputChannel(OutputChannelNames.LanguageServer);
this.disposables.push(this.output);
this.registerCommand().ignoreErrors();
}

return this.output;
}

Expand All @@ -39,10 +39,10 @@ export class LanguageServerOutputChannel implements ILanguageServerOutputChannel
// This controls the visibility of the command used to display the LS Output panel.
// We don't want to display it when Jedi is used instead of LS.
await this.commandManager.executeCommand('setContext', 'python.hasLanguageServerOutputChannel', true);
this.disposable.push(
this.disposables.push(
this.commandManager.registerCommand('python.viewLanguageServerOutput', () => this.output?.show(true)),
);
this.disposable.push({
this.disposables.push({
dispose: () => {
this.registered = false;
},
Expand Down
15 changes: 13 additions & 2 deletions src/client/activation/node/languageServerProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {

const api = extension?.exports;
if (api && api.client && api.client.isEnabled()) {
this.pylanceApi = api;
await api.client.start();
if (!api.client.supportsSelfManagedLifecycle) {
this.pylanceApi = api;
await api.client.start();
}
return;
}

Expand Down Expand Up @@ -151,6 +153,15 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
}
}

public async restartSelfManagedClient(): Promise<boolean> {
const api = (await this.getPylanceExtension())?.exports;
if (api?.client?.supportsSelfManagedLifecycle && api.client.restart) {
await api.client.restart();
return true;
}
return false;
}

// eslint-disable-next-line class-methods-use-this
public loadExtension(): void {
// No body.
Expand Down
3 changes: 3 additions & 0 deletions src/client/activation/node/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class NodeLanguageServerManager implements ILanguageServerManager {
@traceDecoratorError('Failed to restart language server')
@traceDecoratorVerbose('Restarting language server')
protected async restartLanguageServer(): Promise<void> {
if (await this.languageServerProxy.restartSelfManagedClient()) {
return;
}
await this.stopLanguageServer();
await this.startLanguageServer();
}
Expand Down
2 changes: 2 additions & 0 deletions src/client/activation/node/pylanceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {

export interface PylanceApi {
client?: {
readonly supportsSelfManagedLifecycle?: boolean;
isEnabled(): boolean;
start(): Promise<void>;
stop(): Promise<void>;
restart?(): Promise<void>;
};
notebook?: {
registerJupyterPythonPathFunction(func: (uri: Uri) => Promise<string | undefined>): void;
Expand Down
4 changes: 1 addition & 3 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ export function buildApi(
},
},
pylance: {
supportsSelfManagedPylance: true,
createClient: (...args: any[]): BaseLanguageClient => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove pylance apis except getTelemetryReporter

// Make sure we share output channel so that we can share one with
// Jedi as well.
const clientOptions = args[1] as LanguageClientOptions;
clientOptions.outputChannel = clientOptions.outputChannel ?? outputChannel.channel;

return new LanguageClient(PYTHON_LANGUAGE, PYLANCE_NAME, args[0], clientOptions);
},
start: (client: BaseLanguageClient): Promise<void> => client.start(),
Expand Down
1 change: 1 addition & 0 deletions src/client/browser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IBrowserExtensionApi {
export function buildApi(reporter: TelemetryReporter): IBrowserExtensionApi {
const api: IBrowserExtensionApi = {
pylance: {
supportsSelfManagedPylance: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient: (...args: any[]): BaseLanguageClient =>
new LanguageClient(PYTHON_LANGUAGE, 'Python Language Server', args[0], args[1]),
Expand Down
6 changes: 4 additions & 2 deletions src/client/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ async function runPylance(
pylanceExtension = await getActivatedExtension(pylanceExtension);
const api = pylanceExtension.exports;
if (api.client && api.client.isEnabled()) {
pylanceApi = api;
await api.client.start();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just removing this call to start. not sure why the diff is messed up. My other idea was to leave this logic in. but change pylance's api.client.isEnabled() to return false and then just return in that case.

if (!api.client.supportsSelfManagedLifecycle) {
pylanceApi = api;
await api.client.start();
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export namespace InterpreterQuickPickList {
}

export namespace OutputChannelNames {
export const languageServer = l10n.t('Python Language Server');
export const LanguageServer = l10n.t('Python Language Server');
export const python = l10n.t('Python');
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/languageServer/jediLSExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class JediLSExtensionManager implements IDisposable, ILanguageServerExten

constructor(
serviceContainer: IServiceContainer,
outputChannel: ILanguageServerOutputChannel,
_experimentService: IExperimentService,
workspaceService: IWorkspaceService,
configurationService: IConfigurationService,
Expand All @@ -41,6 +40,7 @@ export class JediLSExtensionManager implements IDisposable, ILanguageServerExten
environmentService: IEnvironmentVariablesProvider,
commandManager: ICommandManager,
) {
const outputChannel = serviceContainer.get<ILanguageServerOutputChannel>(ILanguageServerOutputChannel);
this.analysisOptions = new JediLanguageServerAnalysisOptions(
environmentService,
outputChannel,
Expand Down
11 changes: 5 additions & 6 deletions src/client/languageServer/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import * as path from 'path';
import { inject, injectable } from 'inversify';
import { ConfigurationChangeEvent, l10n, Uri, WorkspaceFoldersChangeEvent } from 'vscode';
import { LanguageServerChangeHandler } from '../activation/common/languageServerChangeHandler';
import { IExtensionActivationService, ILanguageServerOutputChannel, LanguageServerType } from '../activation/types';
import { IExtensionActivationService, LanguageServerType } from '../activation/types';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../common/application/types';
import { IFileSystem } from '../common/platform/types';

import {
IConfigurationService,
IDisposableRegistry,
Expand All @@ -30,6 +31,7 @@ import { ILanguageServerExtensionManager, ILanguageServerWatcher } from './types
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { StopWatch } from '../common/utils/stopWatch';
import { ILanguageServerOutputChannel } from '../activation/types';

@injectable()
/**
Expand All @@ -53,7 +55,6 @@ export class LanguageServerWatcher implements IExtensionActivationService, ILang

constructor(
@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer,
@inject(ILanguageServerOutputChannel) private readonly lsOutputChannel: ILanguageServerOutputChannel,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(IExperimentService) private readonly experimentService: IExperimentService,
@inject(IInterpreterHelper) private readonly interpreterHelper: IInterpreterHelper,
Expand All @@ -62,7 +63,6 @@ export class LanguageServerWatcher implements IExtensionActivationService, ILang
@inject(IEnvironmentVariablesProvider) private readonly environmentService: IEnvironmentVariablesProvider,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IFileSystem) private readonly fileSystem: IFileSystem,
@inject(IExtensions) private readonly extensions: IExtensions,
@inject(IApplicationShell) readonly applicationShell: IApplicationShell,
@inject(IDisposableRegistry) readonly disposables: IDisposableRegistry,
Expand Down Expand Up @@ -233,7 +233,6 @@ export class LanguageServerWatcher implements IExtensionActivationService, ILang
case LanguageServerType.Jedi:
lsManager = new JediLSExtensionManager(
this.serviceContainer,
this.lsOutputChannel,
this.experimentService,
this.workspaceService,
this.configurationService,
Expand All @@ -246,15 +245,15 @@ export class LanguageServerWatcher implements IExtensionActivationService, ILang
case LanguageServerType.Node:
lsManager = new PylanceLSExtensionManager(
this.serviceContainer,
this.lsOutputChannel,
this.serviceContainer.get<ILanguageServerOutputChannel>(ILanguageServerOutputChannel),
this.experimentService,
this.workspaceService,
this.configurationService,
this.interpreterPathService,
this.interpreterService,
this.environmentService,
this.commandManager,
this.fileSystem,
this.serviceContainer.get<IFileSystem>(IFileSystem),
this.extensions,
this.applicationShell,
);
Expand Down
1 change: 1 addition & 0 deletions src/client/pylanceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface TelemetryReporter {
}

export interface ApiForPylance {
readonly supportsSelfManagedPylance: true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient(...args: any[]): BaseLanguageClient;
start(client: BaseLanguageClient): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/test/activation/outputChannel.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ suite('Language Server Output Channel', () => {

test('Create output channel if one does not exist before and return it', async () => {
appShell
.setup((a) => a.createOutputChannel(OutputChannelNames.languageServer))
.setup((a) => a.createOutputChannel(OutputChannelNames.LanguageServer))
.returns(() => output.object)
.verifiable(TypeMoq.Times.once());
const { channel } = languageServerOutputChannel;
Expand Down
13 changes: 1 addition & 12 deletions src/test/activation/serviceRegistry.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import { instance, mock, verify } from 'ts-mockito';

import { ExtensionActivationManager } from '../../client/activation/activationManager';
import { ExtensionSurveyPrompt } from '../../client/activation/extensionSurvey';
import { LanguageServerOutputChannel } from '../../client/activation/common/outputChannel';
import { registerTypes } from '../../client/activation/serviceRegistry';
import {
IExtensionActivationManager,
IExtensionSingleActivationService,
ILanguageServerOutputChannel,
} from '../../client/activation/types';
import { IExtensionActivationManager, IExtensionSingleActivationService } from '../../client/activation/types';
import { ServiceManager } from '../../client/ioc/serviceManager';
import { IServiceManager } from '../../client/ioc/types';
import { LoadLanguageServerExtension } from '../../client/activation/common/loadLanguageServerExtension';
Expand All @@ -29,12 +24,6 @@ suite('Unit Tests - Language Server Activation Service Registry', () => {
verify(
serviceManager.add<IExtensionActivationManager>(IExtensionActivationManager, ExtensionActivationManager),
).once();
verify(
serviceManager.addSingleton<ILanguageServerOutputChannel>(
ILanguageServerOutputChannel,
LanguageServerOutputChannel,
),
).once();
verify(
serviceManager.addSingleton<IExtensionSingleActivationService>(
IExtensionSingleActivationService,
Expand Down
24 changes: 21 additions & 3 deletions src/test/languageServer/jediLSExtensionManager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// Licensed under the MIT License.

import * as assert from 'assert';

import { ILanguageServerOutputChannel } from '../../client/activation/types';
import { IWorkspaceService, ICommandManager } from '../../client/common/application/types';
import { IExperimentService, IConfigurationService, IInterpreterPathService } from '../../client/common/types';
import {
IExperimentService,
IConfigurationService,
IInterpreterPathService,
ILogOutputChannel,
} from '../../client/common/types';
import { IEnvironmentVariablesProvider } from '../../client/common/variables/types';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { IServiceContainer } from '../../client/ioc/types';
Expand All @@ -15,9 +21,21 @@ suite('Language Server - Jedi LS extension manager', () => {
let manager: JediLSExtensionManager;

setup(() => {
// Create a mock ILogOutputChannel
const mockOutputChannel = {} as ILogOutputChannel;

// Create a mock service container with the required get method
const mockServiceContainer = {
get: (serviceIdentifier: any) => {
if (serviceIdentifier === ILanguageServerOutputChannel) {
return { channel: mockOutputChannel };
}
return undefined;
},
} as IServiceContainer;

manager = new JediLSExtensionManager(
{} as IServiceContainer,
{} as ILanguageServerOutputChannel,
mockServiceContainer,
{} as IExperimentService,
{} as IWorkspaceService,
{} as IConfigurationService,
Expand Down
45 changes: 45 additions & 0 deletions src/test/languageServer/pylanceLSExtensionManager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Licensed under the MIT License.

import * as assert from 'assert';
import * as sinon from 'sinon';
import { LanguageClientOptions } from 'vscode-languageclient/node';
import { NodeLanguageServerProxy } from '../../client/activation/node/languageServerProxy';
import { ILanguageClientFactory } from '../../client/activation/types';
import { ILanguageServerOutputChannel } from '../../client/activation/types';
import { IWorkspaceService, ICommandManager, IApplicationShell } from '../../client/common/application/types';
import { IFileSystem } from '../../client/common/platform/types';
Expand Down Expand Up @@ -40,6 +44,47 @@ suite('Language Server - Pylance LS extension manager', () => {
);
});

suite('Language Server - Pylance lifecycle compatibility', () => {
for (const selfManaged of [false, true]) {
test(`Python ${selfManaged ? 'delegates' : 'manages'} the Pylance client lifecycle`, async () => {
const start = sinon.stub().resolves();
const stop = sinon.stub().resolves();
const restart = sinon.stub().resolves();
const createLanguageClient = sinon.stub();
const proxy = new NodeLanguageServerProxy(
({ createLanguageClient } as unknown) as ILanguageClientFactory,
{} as IExperimentService,
{} as IInterpreterPathService,
{} as IEnvironmentVariablesProvider,
{} as IWorkspaceService,
({
getExtension: () => ({
isActive: true,
packageJSON: { version: '1.0.0' },
exports: {
client: {
supportsSelfManagedLifecycle: selfManaged,
isEnabled: () => true,
start,
stop,
restart,
},
},
}),
} as unknown) as IExtensions,
);

await proxy.start(undefined, undefined, {} as LanguageClientOptions);
assert.strictEqual(start.callCount, selfManaged ? 0 : 1);
assert.strictEqual(createLanguageClient.callCount, 0);
assert.strictEqual(await proxy.restartSelfManagedClient(), selfManaged);
assert.strictEqual(restart.callCount, selfManaged ? 1 : 0);
await proxy.stop();
assert.strictEqual(stop.callCount, selfManaged ? 0 : 1);
});
}
});

test('Constructor should create a client proxy, a server manager and a server proxy', () => {
assert.notStrictEqual(manager.clientFactory, undefined);
assert.notStrictEqual(manager.serverManager, undefined);
Expand Down
Loading
Loading