22// Licensed under the MIT License.
33
44/* 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' ;
177import { createDeferred } from './utils/async' ;
8+ import { traceError , traceLog } from './log/logging' ;
189
1910interface IExtensionApi {
2011 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- } ;
3612 settings : {
3713 getExecutionDetails ( resource ?: Resource ) : { execCommand : string [ ] | undefined } ;
3814 } ;
@@ -45,7 +21,6 @@ export interface IInterpreterDetails {
4521
4622const onDidChangePythonInterpreterEvent = new EventEmitter < IInterpreterDetails > ( ) ;
4723export const onDidChangePythonInterpreter : Event < IInterpreterDetails > = onDidChangePythonInterpreterEvent . event ;
48-
4924async function activateExtension ( ) {
5025 const extension = extensions . getExtension ( 'ms-python.python' ) ;
5126 if ( extension ) {
@@ -61,9 +36,15 @@ async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
6136 return extension ?. exports as IExtensionApi ;
6237}
6338
39+ async function getPythonExtensionEnviromentAPI ( ) : Promise < PythonExtension > {
40+ // Load the Python extension API
41+ await activateExtension ( ) ;
42+ return await PythonExtension . api ( ) ;
43+ }
44+
6445export async function initializePython ( disposables : Disposable [ ] ) : Promise < void > {
6546 try {
66- const api = await getPythonExtensionAPI ( ) ;
47+ const api = await getPythonExtensionEnviromentAPI ( ) ;
6748
6849 if ( api ) {
6950 disposables . push (
@@ -80,11 +61,6 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
8061 }
8162}
8263
83- export async function getDebuggerPath ( ) : Promise < string | undefined > {
84- const api = await getPythonExtensionAPI ( ) ;
85- return api ?. debug . getDebuggerPackagePath ( ) ;
86- }
87-
8864export async function runPythonExtensionCommand ( command : string , ...rest : any [ ] ) {
8965 await activateExtension ( ) ;
9066 return await commands . executeCommand ( command , ...rest ) ;
@@ -96,51 +72,47 @@ export async function getSettingsPythonPath(resource?: Uri): Promise<string[] |
9672}
9773
9874export 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 ) ;
10177}
10278
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 ) ;
10682}
10783
10884export 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 ) ;
11187}
11288
11389export 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 ) ) ;
11892 if ( environment ?. executable . uri ) {
11993 return { path : [ environment ?. executable . uri . fsPath ] , resource } ;
12094 }
12195 return { path : undefined , resource } ;
12296}
12397
12498export 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 ( ) ;
136104 }
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 ;
140109 }
110+ await Promise . race ( [ onAddedToCollection . promise , api ?. environments . refreshEnvironments ( ) ] ) ;
111+
112+ return api . environments . known . length > 0 ;
141113}
142114
143115export async function getInterpreters ( ) {
144- const api = await getPythonExtensionAPI ( ) ;
145- return api ? .environments . known || [ ] ;
116+ const api = await getPythonExtensionEnviromentAPI ( ) ;
117+ return api . environments . known || [ ] ;
146118}
0 commit comments