@@ -6,28 +6,45 @@ import {
6
6
ActiveEnvironmentPathChangeEvent ,
7
7
Environment ,
8
8
EnvironmentPath ,
9
+ EnvironmentVariables ,
9
10
PythonExtension ,
11
+ ResolvedEnvironment ,
10
12
Resource ,
11
13
} 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' ;
13
15
import { createDeferred } from './utils/async' ;
14
16
import { traceError , traceLog } from './log/logging' ;
15
17
18
+ /**
19
+ * Interface for the Python extension API.
20
+ */
16
21
interface IExtensionApi {
17
22
ready : Promise < void > ;
18
23
settings : {
19
24
getExecutionDetails ( resource ?: Resource ) : { execCommand : string [ ] | undefined } ;
20
25
} ;
21
26
}
22
27
28
+ /**
29
+ * Details about a Python interpreter.
30
+ */
23
31
export interface IInterpreterDetails {
32
+ /** Array of path components to the Python executable */
24
33
path ?: string [ ] ;
34
+ /** The workspace resource associated with this interpreter */
25
35
resource ?: Uri ;
26
36
}
27
37
38
+ /** Event emitter for Python interpreter changes */
28
39
const onDidChangePythonInterpreterEvent = new EventEmitter < IInterpreterDetails > ( ) ;
40
+
41
+ /** Event that fires when the active Python interpreter changes */
29
42
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 > {
31
48
console . log ( 'Activating Python extension...' ) ;
32
49
activateEnvsExtension ( ) ;
33
50
const extension = extensions . getExtension ( 'ms-python.python' ) ;
@@ -39,7 +56,11 @@ async function activateExtension() {
39
56
console . log ( 'Python extension activated.' ) ;
40
57
return extension ;
41
58
}
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 > {
43
64
const extension = extensions . getExtension ( 'ms-python.vscode-python-envs' ) ;
44
65
if ( extension ) {
45
66
if ( ! extension . isActive ) {
@@ -49,23 +70,36 @@ async function activateEnvsExtension() {
49
70
return extension ;
50
71
}
51
72
73
+ /**
74
+ * Gets the Python extension's API interface.
75
+ * @returns The Python extension API or undefined if not available
76
+ */
52
77
async function getPythonExtensionAPI ( ) : Promise < IExtensionApi | undefined > {
53
78
const extension = await activateExtension ( ) ;
54
79
return extension ?. exports as IExtensionApi ;
55
80
}
56
81
82
+ /**
83
+ * Gets the Python extension's environment API.
84
+ * @returns The Python extension environment API
85
+ */
57
86
async function getPythonExtensionEnviromentAPI ( ) : Promise < PythonExtension > {
58
87
// Load the Python extension API
59
88
await activateExtension ( ) ;
60
89
return await PythonExtension . api ( ) ;
61
90
}
62
91
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
+ */
63
96
export async function initializePython ( disposables : Disposable [ ] ) : Promise < void > {
64
97
try {
65
98
const api = await getPythonExtensionEnviromentAPI ( ) ;
66
99
67
100
if ( api ) {
68
101
disposables . push (
102
+ // This event is triggered when the active environment setting changes.
69
103
api . environments . onDidChangeActiveEnvironmentPath ( ( e : ActiveEnvironmentPathChangeEvent ) => {
70
104
let resourceUri : Uri | undefined ;
71
105
if ( e . resource instanceof Uri ) {
@@ -87,31 +121,68 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
87
121
}
88
122
}
89
123
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 > {
91
131
await activateExtension ( ) ;
92
132
return await commands . executeCommand ( command , ...rest ) ;
93
133
}
94
134
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
+ */
95
142
export async function getSettingsPythonPath ( resource ?: Uri ) : Promise < string [ ] | undefined > {
96
143
const api = await getPythonExtensionAPI ( ) ;
97
144
return api ?. settings . getExecutionDetails ( resource ) . execCommand ;
98
145
}
99
146
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 > {
101
154
const api = await getPythonExtensionEnviromentAPI ( ) ;
102
- return api . environments . getEnvironmentVariables ( resource ) ;
155
+ return Promise . resolve ( api . environments . getEnvironmentVariables ( resource ) ) ;
103
156
}
104
157
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 > {
106
166
const api = await getPythonExtensionEnviromentAPI ( ) ;
107
167
return api . environments . resolveEnvironment ( env ) ;
108
168
}
109
169
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 > {
111
177
const api = await getPythonExtensionEnviromentAPI ( ) ;
112
178
return api . environments . getActiveEnvironmentPath ( resource ) ;
113
179
}
114
180
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
+ */
115
186
export async function getInterpreterDetails ( resource ?: Uri ) : Promise < IInterpreterDetails > {
116
187
const api = await getPythonExtensionEnviromentAPI ( ) ;
117
188
const environment = await api . environments . resolveEnvironment ( api . environments . getActiveEnvironmentPath ( resource ) ) ;
@@ -126,7 +197,11 @@ export async function getInterpreterDetails(resource?: Uri): Promise<IInterprete
126
197
return { path : undefined , resource } ;
127
198
}
128
199
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 > {
130
205
const api = await getPythonExtensionEnviromentAPI ( ) ;
131
206
const onAddedToCollection = createDeferred ( ) ;
132
207
api . environments . onDidChangeEnvironments ( async ( ) => {
@@ -138,12 +213,18 @@ export async function hasInterpreters() {
138
213
if ( initialEnvs . length > 0 ) {
139
214
return true ;
140
215
}
216
+ // Initiates a refresh of Python environments within the specified scope.
141
217
await Promise . race ( [ onAddedToCollection . promise , api ?. environments . refreshEnvironments ( ) ] ) ;
142
218
143
219
return api . environments . known . length > 0 ;
144
220
}
145
221
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 [ ] > {
147
228
const api = await getPythonExtensionEnviromentAPI ( ) ;
148
229
return api . environments . known || [ ] ;
149
230
}
0 commit comments