Skip to content

Commit

Permalink
Made browser to expose pylance api. (#20847)
Browse files Browse the repository at this point in the history
This will let pylance to create LS Client on browser
  • Loading branch information
heejaechang authored Mar 14, 2023
1 parent 0735876 commit 1668d06
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/client/browser/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { BaseLanguageClient } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/browser';
import { PYTHON_LANGUAGE } from '../common/constants';

export interface IBrowserExtensionApi {
/**
* @deprecated Temporarily exposed for Pylance until we expose this API generally. Will be removed in an
* iteration or two.
*/
pylance: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient(...args: any[]): BaseLanguageClient;
start(client: BaseLanguageClient): Promise<void>;
stop(client: BaseLanguageClient): Promise<void>;
};
}

export function buildApi(): IBrowserExtensionApi {
const api: IBrowserExtensionApi = {
pylance: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient: (...args: any[]): BaseLanguageClient =>
new LanguageClient(PYTHON_LANGUAGE, 'Python Language Server', args[0], args[1]),
start: (client: BaseLanguageClient): Promise<void> => client.start(),
stop: (client: BaseLanguageClient): Promise<void> => client.stop(),
},
};

return api;
}
7 changes: 5 additions & 2 deletions src/client/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppinsightsKey, PYLANCE_EXTENSION_ID } from '../common/constants';
import { EventName } from '../telemetry/constants';
import { createStatusItem } from './intellisenseStatus';
import { PylanceApi } from '../activation/node/pylanceApi';
import { buildApi, IBrowserExtensionApi } from './api';

interface BrowserConfig {
distUrl: string; // URL to Pylance's dist folder.
Expand All @@ -19,11 +20,11 @@ interface BrowserConfig {
let languageClient: LanguageClient | undefined;
let pylanceApi: PylanceApi | undefined;

export async function activate(context: vscode.ExtensionContext): Promise<void> {
export async function activate(context: vscode.ExtensionContext): Promise<IBrowserExtensionApi> {
const pylanceExtension = vscode.extensions.getExtension<PylanceApi>(PYLANCE_EXTENSION_ID);
if (pylanceExtension) {
await runPylance(context, pylanceExtension);
return;
return buildApi();
}

const changeDisposable = vscode.extensions.onDidChange(async () => {
Expand All @@ -33,6 +34,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
await runPylance(context, newPylanceExtension);
}
});

return buildApi();
}

export async function deactivate(): Promise<void> {
Expand Down

0 comments on commit 1668d06

Please sign in to comment.