3
3
4
4
/* eslint-disable @typescript-eslint/naming-convention */
5
5
import { Environment , EnvironmentPath , PythonExtension , Resource } from '@vscode/python-extension' ;
6
- import { commands , extensions , Uri } from 'vscode' ;
6
+ import { commands , EventEmitter , extensions , Uri , Event , Disposable } from 'vscode' ;
7
7
import { createDeferred } from './utils/async' ;
8
+ import { traceError , traceLog } from './log/logging' ;
8
9
9
10
interface IExtensionApi {
10
11
ready : Promise < void > ;
11
12
settings : {
12
13
getExecutionDetails ( resource ?: Resource ) : { execCommand : string [ ] | undefined } ;
13
14
} ;
14
15
}
16
+
15
17
export interface IInterpreterDetails {
16
18
path ?: string [ ] ;
17
19
resource ?: Uri ;
18
20
}
21
+
22
+ const onDidChangePythonInterpreterEvent = new EventEmitter < IInterpreterDetails > ( ) ;
23
+ export const onDidChangePythonInterpreter : Event < IInterpreterDetails > = onDidChangePythonInterpreterEvent . event ;
19
24
async function activateExtension ( ) {
20
25
const extension = extensions . getExtension ( 'ms-python.python' ) ;
21
26
if ( extension ) {
@@ -33,9 +38,29 @@ async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
33
38
34
39
async function getPythonExtensionEnviromentAPI ( ) : Promise < PythonExtension > {
35
40
// Load the Python extension API
41
+ await activateExtension ( ) ;
36
42
return await PythonExtension . api ( ) ;
37
43
}
38
44
45
+ export async function initializePython ( disposables : Disposable [ ] ) : Promise < void > {
46
+ try {
47
+ const api = await getPythonExtensionEnviromentAPI ( ) ;
48
+
49
+ if ( api ) {
50
+ disposables . push (
51
+ api . environments . onDidChangeActiveEnvironmentPath ( ( e ) => {
52
+ onDidChangePythonInterpreterEvent . fire ( { path : [ e . path ] , resource : e . resource ?. uri } ) ;
53
+ } ) ,
54
+ ) ;
55
+
56
+ traceLog ( 'Waiting for interpreter from python extension.' ) ;
57
+ onDidChangePythonInterpreterEvent . fire ( await getInterpreterDetails ( ) ) ;
58
+ }
59
+ } catch ( error ) {
60
+ traceError ( 'Error initializing python: ' , error ) ;
61
+ }
62
+ }
63
+
39
64
export async function runPythonExtensionCommand ( command : string , ...rest : any [ ] ) {
40
65
await activateExtension ( ) ;
41
66
return await commands . executeCommand ( command , ...rest ) ;
0 commit comments