1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- import { Terminal , Uri } from 'vscode' ;
4
+ import { EventEmitter , Terminal , Uri , Disposable , ConfigurationTarget } from 'vscode' ;
5
5
import { getExtension } from '../common/vscodeApis/extensionsApi' ;
6
6
import {
7
7
GetEnvironmentScope ,
@@ -10,8 +10,10 @@ import {
10
10
PythonEnvironmentApi ,
11
11
PythonProcess ,
12
12
RefreshEnvironmentsScope ,
13
+ DidChangeEnvironmentEventArgs ,
13
14
} from './types' ;
14
15
import { executeCommand } from '../common/vscodeApis/commandApis' ;
16
+ import { IInterpreterPathService } from '../common/types' ;
15
17
16
18
export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs' ;
17
19
@@ -24,6 +26,17 @@ export function useEnvExtension(): boolean {
24
26
return _useExt ;
25
27
}
26
28
29
+ const onDidChangeEnvironmentEnvExtEmitter : EventEmitter < DidChangeEnvironmentEventArgs > = new EventEmitter <
30
+ DidChangeEnvironmentEventArgs
31
+ > ( ) ;
32
+ export function onDidChangeEnvironmentEnvExt (
33
+ listener : ( e : DidChangeEnvironmentEventArgs ) => unknown ,
34
+ thisArgs ?: unknown ,
35
+ disposables ?: Disposable [ ] ,
36
+ ) : Disposable {
37
+ return onDidChangeEnvironmentEnvExtEmitter . event ( listener , thisArgs , disposables ) ;
38
+ }
39
+
27
40
let _extApi : PythonEnvironmentApi | undefined ;
28
41
export async function getEnvExtApi ( ) : Promise < PythonEnvironmentApi > {
29
42
if ( _extApi ) {
@@ -33,14 +46,15 @@ export async function getEnvExtApi(): Promise<PythonEnvironmentApi> {
33
46
if ( ! extension ) {
34
47
throw new Error ( 'Python Environments extension not found.' ) ;
35
48
}
36
- if ( extension ?. isActive ) {
37
- _extApi = extension . exports as PythonEnvironmentApi ;
38
- return _extApi ;
49
+ if ( ! extension ?. isActive ) {
50
+ await extension . activate ( ) ;
39
51
}
40
52
41
- await extension . activate ( ) ;
42
-
43
53
_extApi = extension . exports as PythonEnvironmentApi ;
54
+ _extApi . onDidChangeEnvironment ( ( e ) => {
55
+ onDidChangeEnvironmentEnvExtEmitter . fire ( e ) ;
56
+ } ) ;
57
+
44
58
return _extApi ;
45
59
}
46
60
@@ -106,3 +120,32 @@ export async function clearCache(): Promise<void> {
106
120
await executeCommand ( 'python-envs.clearCache' ) ;
107
121
}
108
122
}
123
+
124
+ export function registerEnvExtFeatures (
125
+ disposables : Disposable [ ] ,
126
+ interpreterPathService : IInterpreterPathService ,
127
+ ) : void {
128
+ if ( useEnvExtension ( ) ) {
129
+ disposables . push (
130
+ onDidChangeEnvironmentEnvExt ( async ( e : DidChangeEnvironmentEventArgs ) => {
131
+ const previousPath = interpreterPathService . get ( e . uri ) ;
132
+
133
+ if ( previousPath !== e . new ?. environmentPath . fsPath ) {
134
+ if ( e . uri ) {
135
+ await interpreterPathService . update (
136
+ e . uri ,
137
+ ConfigurationTarget . WorkspaceFolder ,
138
+ e . new ?. environmentPath . fsPath ,
139
+ ) ;
140
+ } else {
141
+ await interpreterPathService . update (
142
+ undefined ,
143
+ ConfigurationTarget . Global ,
144
+ e . new ?. environmentPath . fsPath ,
145
+ ) ;
146
+ }
147
+ }
148
+ } ) ,
149
+ ) ;
150
+ }
151
+ }
0 commit comments