Skip to content

Commit 9545c8b

Browse files
committed
update to python api for documentation
1 parent 4e30462 commit 9545c8b

File tree

1 file changed

+91
-10
lines changed

1 file changed

+91
-10
lines changed

src/extension/common/python.ts

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,45 @@ import {
66
ActiveEnvironmentPathChangeEvent,
77
Environment,
88
EnvironmentPath,
9+
EnvironmentVariables,
910
PythonExtension,
11+
ResolvedEnvironment,
1012
Resource,
1113
} from '@vscode/python-extension';
12-
import { commands, EventEmitter, extensions, Uri, Event, Disposable } from 'vscode';
14+
import { commands, EventEmitter, extensions, Uri, Event, Disposable, Extension } from 'vscode';
1315
import { createDeferred } from './utils/async';
1416
import { traceError, traceLog } from './log/logging';
1517

18+
/**
19+
* Interface for the Python extension API.
20+
*/
1621
interface IExtensionApi {
1722
ready: Promise<void>;
1823
settings: {
1924
getExecutionDetails(resource?: Resource): { execCommand: string[] | undefined };
2025
};
2126
}
2227

28+
/**
29+
* Details about a Python interpreter.
30+
*/
2331
export interface IInterpreterDetails {
32+
/** Array of path components to the Python executable */
2433
path?: string[];
34+
/** The workspace resource associated with this interpreter */
2535
resource?: Uri;
2636
}
2737

38+
/** Event emitter for Python interpreter changes */
2839
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
40+
41+
/** Event that fires when the active Python interpreter changes */
2942
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
30-
async function activateExtension() {
43+
/**
44+
* Activates the Python extension and ensures it's ready for use.
45+
* @returns The activated Python extension instance
46+
*/
47+
async function activateExtension(): Promise<Extension<any> | undefined> {
3148
console.log('Activating Python extension...');
3249
activateEnvsExtension();
3350
const extension = extensions.getExtension('ms-python.python');
@@ -39,7 +56,11 @@ async function activateExtension() {
3956
console.log('Python extension activated.');
4057
return extension;
4158
}
42-
async function activateEnvsExtension() {
59+
/**
60+
* Activates the Python environments extension.
61+
* @returns The activated Python environments extension instance
62+
*/
63+
async function activateEnvsExtension(): Promise<Extension<any> | undefined> {
4364
const extension = extensions.getExtension('ms-python.vscode-python-envs');
4465
if (extension) {
4566
if (!extension.isActive) {
@@ -49,23 +70,36 @@ async function activateEnvsExtension() {
4970
return extension;
5071
}
5172

73+
/**
74+
* Gets the Python extension's API interface.
75+
* @returns The Python extension API or undefined if not available
76+
*/
5277
async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
5378
const extension = await activateExtension();
5479
return extension?.exports as IExtensionApi;
5580
}
5681

82+
/**
83+
* Gets the Python extension's environment API.
84+
* @returns The Python extension environment API
85+
*/
5786
async function getPythonExtensionEnviromentAPI(): Promise<PythonExtension> {
5887
// Load the Python extension API
5988
await activateExtension();
6089
return await PythonExtension.api();
6190
}
6291

92+
/**
93+
* Initializes Python integration by setting up event listeners and getting initial interpreter details.
94+
* @param disposables Array to store disposable resources for cleanup
95+
*/
6396
export async function initializePython(disposables: Disposable[]): Promise<void> {
6497
try {
6598
const api = await getPythonExtensionEnviromentAPI();
6699

67100
if (api) {
68101
disposables.push(
102+
// This event is triggered when the active environment setting changes.
69103
api.environments.onDidChangeActiveEnvironmentPath((e: ActiveEnvironmentPathChangeEvent) => {
70104
let resourceUri: Uri | undefined;
71105
if (e.resource instanceof Uri) {
@@ -87,31 +121,68 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
87121
}
88122
}
89123

90-
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
124+
/**
125+
* Executes a command from the Python extension.
126+
* @param command The command identifier to execute
127+
* @param rest Additional arguments to pass to the command
128+
* @returns The result of the command execution
129+
*/
130+
export async function runPythonExtensionCommand(command: string, ...rest: any[]): Promise<any> {
91131
await activateExtension();
92132
return await commands.executeCommand(command, ...rest);
93133
}
94134

135+
/**
136+
* Returns all the details needed to execute code within the selected environment,
137+
* corresponding to the specified resource taking into account any workspace-specific settings
138+
* for the workspace to which this resource belongs.
139+
* @param resource Optional workspace resource to get settings for
140+
* @returns Array of command components or undefined if not available
141+
*/
95142
export async function getSettingsPythonPath(resource?: Uri): Promise<string[] | undefined> {
96143
const api = await getPythonExtensionAPI();
97144
return api?.settings.getExecutionDetails(resource).execCommand;
98145
}
99146

100-
export async function getEnvironmentVariables(resource?: Resource) {
147+
/**
148+
* Returns the environment variables used by the extension for a resource, which includes the custom
149+
* variables configured by user in `.env` files.
150+
* @param resource Optional workspace resource to get environment variables for
151+
* @returns Environment variables object
152+
*/
153+
export async function getEnvironmentVariables(resource?: Resource): Promise<EnvironmentVariables> {
101154
const api = await getPythonExtensionEnviromentAPI();
102-
return api.environments.getEnvironmentVariables(resource);
155+
return Promise.resolve(api.environments.getEnvironmentVariables(resource));
103156
}
104157

105-
export async function resolveEnvironment(env: Environment | EnvironmentPath | string) {
158+
/**
159+
* Returns details for the given environment, or `undefined` if the env is invalid.
160+
* @param env Environment to resolve (can be Environment object, path, or string)
161+
* @returns Resolved environment details
162+
*/
163+
export async function resolveEnvironment(
164+
env: Environment | EnvironmentPath | string,
165+
): Promise<ResolvedEnvironment | undefined> {
106166
const api = await getPythonExtensionEnviromentAPI();
107167
return api.environments.resolveEnvironment(env);
108168
}
109169

110-
export async function getActiveEnvironmentPath(resource?: Resource) {
170+
/**
171+
* Returns the environment configured by user in settings. Note that this can be an invalid environment, use
172+
* resolve the environment to get full details.
173+
* @param resource Optional workspace resource to get active environment for
174+
* @returns Path to the active environment
175+
*/
176+
export async function getActiveEnvironmentPath(resource?: Resource): Promise<EnvironmentPath> {
111177
const api = await getPythonExtensionEnviromentAPI();
112178
return api.environments.getActiveEnvironmentPath(resource);
113179
}
114180

181+
/**
182+
* Gets detailed information about the active Python interpreter.
183+
* @param resource Optional workspace resource to get interpreter details for
184+
* @returns Interpreter details including path and resource information
185+
*/
115186
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
116187
const api = await getPythonExtensionEnviromentAPI();
117188
const environment = await api.environments.resolveEnvironment(api.environments.getActiveEnvironmentPath(resource));
@@ -126,7 +197,11 @@ export async function getInterpreterDetails(resource?: Uri): Promise<IInterprete
126197
return { path: undefined, resource };
127198
}
128199

129-
export async function hasInterpreters() {
200+
/**
201+
* Checks if any Python interpreters are available in the system.
202+
* @returns True if interpreters are found, false otherwise
203+
*/
204+
export async function hasInterpreters(): Promise<boolean> {
130205
const api = await getPythonExtensionEnviromentAPI();
131206
const onAddedToCollection = createDeferred();
132207
api.environments.onDidChangeEnvironments(async () => {
@@ -138,12 +213,18 @@ export async function hasInterpreters() {
138213
if (initialEnvs.length > 0) {
139214
return true;
140215
}
216+
// Initiates a refresh of Python environments within the specified scope.
141217
await Promise.race([onAddedToCollection.promise, api?.environments.refreshEnvironments()]);
142218

143219
return api.environments.known.length > 0;
144220
}
145221

146-
export async function getInterpreters() {
222+
/**
223+
* Gets environments known to the extension at the time of fetching the property. Note this may not
224+
* contain all environments in the system as a refresh might be going on.
225+
* @returns Array of known Python environments
226+
*/
227+
export async function getInterpreters(): Promise<readonly Environment[]> {
147228
const api = await getPythonExtensionEnviromentAPI();
148229
return api.environments.known || [];
149230
}

0 commit comments

Comments
 (0)