Skip to content

Commit d673004

Browse files
author
Kartik Raj
authored
Convert JS-style typings to native TS in @vscode/python-extension (#21692)
Closes #21690
1 parent 8b9bca1 commit d673004

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

pythonExtensionApi/src/main.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { CancellationToken, Event, Uri, WorkspaceFolder, QuickPickItem, extensio
1010
export interface PythonExtension {
1111
/**
1212
* Promise indicating whether all parts of the extension have completed loading or not.
13-
* @type {Promise<void>}
1413
*/
1514
ready: Promise<void>;
1615
jupyter: {
@@ -21,10 +20,9 @@ export interface PythonExtension {
2120
* Generate an array of strings for commands to pass to the Python executable to launch the debugger for remote debugging.
2221
* Users can append another array of strings of what they want to execute along with relevant arguments to Python.
2322
* E.g `['/Users/..../pythonVSCode/pythonFiles/lib/python/debugpy', '--listen', 'localhost:57039', '--wait-for-client']`
24-
* @param {string} host
25-
* @param {number} port
26-
* @param {boolean} [waitUntilDebuggerAttaches=true]
27-
* @returns {Promise<string[]>}
23+
* @param host
24+
* @param port
25+
* @param waitUntilDebuggerAttaches Defaults to `true`.
2826
*/
2927
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
3028

@@ -38,8 +36,8 @@ export interface PythonExtension {
3836
datascience: {
3937
/**
4038
* Launches Data Viewer component.
41-
* @param {IDataViewerDataProvider} dataProvider Instance that will be used by the Data Viewer component to fetch data.
42-
* @param {string} title Data Viewer title
39+
* @param dataProvider Instance that will be used by the Data Viewer component to fetch data.
40+
* @param title Data Viewer title
4341
*/
4442
showDataViewer(dataProvider: IDataViewerDataProvider, title: string): Promise<void>;
4543
/**
@@ -316,7 +314,6 @@ export type EnvironmentPath = {
316314
* was contributed.
317315
*/
318316
export type EnvironmentTools = KnownEnvironmentTools | string;
319-
320317
/**
321318
* Tools or plugins the Python extension currently has built-in support for. Note this list is expected to shrink
322319
* once tools have their own separate extensions.
@@ -335,7 +332,6 @@ export type KnownEnvironmentTools =
335332
* Type of the environment. It can be {@link KnownEnvironmentTypes} or custom string which was contributed.
336333
*/
337334
export type EnvironmentType = KnownEnvironmentTypes | string;
338-
339335
/**
340336
* Environment types the Python extension is aware of. Note this list is expected to shrink once tools have their
341337
* own separate extensions, in which case they're expected to provide the type themselves.

src/client/api/types.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { CancellationToken, Event, Uri, WorkspaceFolder, QuickPickItem } from 'vscode';
4+
import { CancellationToken, Event, Uri, WorkspaceFolder, QuickPickItem, extensions } from 'vscode';
55

66
/*
77
* Do not introduce any breaking changes to this API.
@@ -10,7 +10,6 @@ import { CancellationToken, Event, Uri, WorkspaceFolder, QuickPickItem } from 'v
1010
export interface PythonExtension {
1111
/**
1212
* Promise indicating whether all parts of the extension have completed loading or not.
13-
* @type {Promise<void>}
1413
*/
1514
ready: Promise<void>;
1615
jupyter: {
@@ -21,10 +20,9 @@ export interface PythonExtension {
2120
* Generate an array of strings for commands to pass to the Python executable to launch the debugger for remote debugging.
2221
* Users can append another array of strings of what they want to execute along with relevant arguments to Python.
2322
* E.g `['/Users/..../pythonVSCode/pythonFiles/lib/python/debugpy', '--listen', 'localhost:57039', '--wait-for-client']`
24-
* @param {string} host
25-
* @param {number} port
26-
* @param {boolean} [waitUntilDebuggerAttaches=true]
27-
* @returns {Promise<string[]>}
23+
* @param host
24+
* @param port
25+
* @param waitUntilDebuggerAttaches Defaults to `true`.
2826
*/
2927
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
3028

@@ -38,8 +36,8 @@ export interface PythonExtension {
3836
datascience: {
3937
/**
4038
* Launches Data Viewer component.
41-
* @param {IDataViewerDataProvider} dataProvider Instance that will be used by the Data Viewer component to fetch data.
42-
* @param {string} title Data Viewer title
39+
* @param dataProvider Instance that will be used by the Data Viewer component to fetch data.
40+
* @param title Data Viewer title
4341
*/
4442
showDataViewer(dataProvider: IDataViewerDataProvider, title: string): Promise<void>;
4543
/**
@@ -387,3 +385,23 @@ export type EnvironmentVariablesChangeEvent = {
387385
*/
388386
readonly env: EnvironmentVariables;
389387
};
388+
389+
export const PVSC_EXTENSION_ID = 'ms-python.python';
390+
391+
// eslint-disable-next-line @typescript-eslint/no-namespace
392+
export namespace PythonExtension {
393+
/**
394+
* Returns the API exposed by the Python extension in VS Code.
395+
*/
396+
export async function api(): Promise<PythonExtension> {
397+
const extension = extensions.getExtension(PVSC_EXTENSION_ID);
398+
if (extension === undefined) {
399+
throw new Error(`Python extension is not installed or is disabled`);
400+
}
401+
if (!extension.isActive) {
402+
await extension.activate();
403+
}
404+
const pythonApi: PythonExtension = extension.exports;
405+
return pythonApi;
406+
}
407+
}

0 commit comments

Comments
 (0)