2
2
// Licensed under the MIT License.
3
3
4
4
/* eslint-disable @typescript-eslint/naming-convention */
5
- import { commands , Disposable , Event , EventEmitter , extensions , Uri } from 'vscode' ;
6
- import { traceError , traceLog } from './log/logging' ;
7
- import { EnvironmentVariables } from './variables/types' ;
8
- import {
9
- ActiveEnvironmentPathChangeEvent ,
10
- Environment ,
11
- EnvironmentPath ,
12
- EnvironmentsChangeEvent ,
13
- RefreshOptions ,
14
- ResolvedEnvironment ,
15
- Resource ,
16
- } from './pythonTypes' ;
5
+ import { Environment , EnvironmentPath , PythonExtension , Resource } from '@vscode/python-extension' ;
6
+ import { commands , EventEmitter , extensions , Uri , Event , Disposable } from 'vscode' ;
17
7
import { createDeferred } from './utils/async' ;
8
+ import { traceError , traceLog } from './log/logging' ;
18
9
19
10
interface IExtensionApi {
20
11
ready : Promise < void > ;
21
- debug : {
22
- getRemoteLauncherCommand ( host : string , port : number , waitUntilDebuggerAttaches : boolean ) : Promise < string [ ] > ;
23
- getDebuggerPackagePath ( ) : Promise < string | undefined > ;
24
- } ;
25
- environments : {
26
- known : Environment [ ] ;
27
- getActiveEnvironmentPath ( resource ?: Resource ) : EnvironmentPath ;
28
- resolveEnvironment (
29
- environment : Environment | EnvironmentPath | string | undefined ,
30
- ) : Promise < ResolvedEnvironment | undefined > ;
31
- readonly onDidChangeActiveEnvironmentPath : Event < ActiveEnvironmentPathChangeEvent > ;
32
- getEnvironmentVariables ( resource ?: Resource ) : EnvironmentVariables ;
33
- refreshEnvironments ( options ?: RefreshOptions ) : Promise < void > ;
34
- readonly onDidChangeEnvironments : Event < EnvironmentsChangeEvent > ;
35
- } ;
36
12
settings : {
37
13
getExecutionDetails ( resource ?: Resource ) : { execCommand : string [ ] | undefined } ;
38
14
} ;
@@ -45,7 +21,6 @@ export interface IInterpreterDetails {
45
21
46
22
const onDidChangePythonInterpreterEvent = new EventEmitter < IInterpreterDetails > ( ) ;
47
23
export const onDidChangePythonInterpreter : Event < IInterpreterDetails > = onDidChangePythonInterpreterEvent . event ;
48
-
49
24
async function activateExtension ( ) {
50
25
const extension = extensions . getExtension ( 'ms-python.python' ) ;
51
26
if ( extension ) {
@@ -61,9 +36,15 @@ async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
61
36
return extension ?. exports as IExtensionApi ;
62
37
}
63
38
39
+ async function getPythonExtensionEnviromentAPI ( ) : Promise < PythonExtension > {
40
+ // Load the Python extension API
41
+ await activateExtension ( ) ;
42
+ return await PythonExtension . api ( ) ;
43
+ }
44
+
64
45
export async function initializePython ( disposables : Disposable [ ] ) : Promise < void > {
65
46
try {
66
- const api = await getPythonExtensionAPI ( ) ;
47
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
67
48
68
49
if ( api ) {
69
50
disposables . push (
@@ -80,11 +61,6 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
80
61
}
81
62
}
82
63
83
- export async function getDebuggerPath ( ) : Promise < string | undefined > {
84
- const api = await getPythonExtensionAPI ( ) ;
85
- return api ?. debug . getDebuggerPackagePath ( ) ;
86
- }
87
-
88
64
export async function runPythonExtensionCommand ( command : string , ...rest : any [ ] ) {
89
65
await activateExtension ( ) ;
90
66
return await commands . executeCommand ( command , ...rest ) ;
@@ -96,51 +72,47 @@ export async function getSettingsPythonPath(resource?: Uri): Promise<string[] |
96
72
}
97
73
98
74
export async function getEnvironmentVariables ( resource ?: Resource ) {
99
- const api = await getPythonExtensionAPI ( ) ;
100
- return api ? .environments . getEnvironmentVariables ( resource ) ;
75
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
76
+ return api . environments . getEnvironmentVariables ( resource ) ;
101
77
}
102
78
103
- export async function resolveEnvironment ( env : Environment | EnvironmentPath | string | undefined ) {
104
- const api = await getPythonExtensionAPI ( ) ;
105
- return api ? .environments . resolveEnvironment ( env ) ;
79
+ export async function resolveEnvironment ( env : Environment | EnvironmentPath | string ) {
80
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
81
+ return api . environments . resolveEnvironment ( env ) ;
106
82
}
107
83
108
84
export async function getActiveEnvironmentPath ( resource ?: Resource ) {
109
- const api = await getPythonExtensionAPI ( ) ;
110
- return api ? .environments . getActiveEnvironmentPath ( resource ) ;
85
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
86
+ return api . environments . getActiveEnvironmentPath ( resource ) ;
111
87
}
112
88
113
89
export async function getInterpreterDetails ( resource ?: Uri ) : Promise < IInterpreterDetails > {
114
- const api = await getPythonExtensionAPI ( ) ;
115
- const environment = await api ?. environments . resolveEnvironment (
116
- api ?. environments . getActiveEnvironmentPath ( resource ) ,
117
- ) ;
90
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
91
+ const environment = await api . environments . resolveEnvironment ( api . environments . getActiveEnvironmentPath ( resource ) ) ;
118
92
if ( environment ?. executable . uri ) {
119
93
return { path : [ environment ?. executable . uri . fsPath ] , resource } ;
120
94
}
121
95
return { path : undefined , resource } ;
122
96
}
123
97
124
98
export async function hasInterpreters ( ) {
125
- const api = await getPythonExtensionAPI ( ) ;
126
- if ( api ) {
127
- const onAddedToCollection = createDeferred ( ) ;
128
- api ?. environments . onDidChangeEnvironments ( async ( ) => {
129
- if ( api . environments . known ) {
130
- onAddedToCollection . resolve ( ) ;
131
- }
132
- } ) ;
133
- const initialEnvs = api . environments . known ;
134
- if ( initialEnvs . length > 0 ) {
135
- return true ;
99
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
100
+ const onAddedToCollection = createDeferred ( ) ;
101
+ api . environments . onDidChangeEnvironments ( async ( ) => {
102
+ if ( api . environments . known ) {
103
+ onAddedToCollection . resolve ( ) ;
136
104
}
137
- await Promise . race ( [ onAddedToCollection . promise , api ?. environments . refreshEnvironments ( ) ] ) ;
138
-
139
- return api . environments . known . length > 0 ;
105
+ } ) ;
106
+ const initialEnvs = api . environments . known ;
107
+ if ( initialEnvs . length > 0 ) {
108
+ return true ;
140
109
}
110
+ await Promise . race ( [ onAddedToCollection . promise , api ?. environments . refreshEnvironments ( ) ] ) ;
111
+
112
+ return api . environments . known . length > 0 ;
141
113
}
142
114
143
115
export async function getInterpreters ( ) {
144
- const api = await getPythonExtensionAPI ( ) ;
145
- return api ? .environments . known || [ ] ;
116
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
117
+ return api . environments . known || [ ] ;
146
118
}
0 commit comments