2
2
// Licensed under the MIT License.
3
3
4
4
import { inject , injectable } from 'inversify' ;
5
- import { ProgressOptions , ProgressLocation } from 'vscode' ;
6
- import { IExtensionSingleActivationService } from '../../activation/types' ;
7
- import { IApplicationShell , IApplicationEnvironment } from '../../common/application/types' ;
5
+ import { ProgressOptions , ProgressLocation , MarkdownString } from 'vscode' ;
6
+ import { IExtensionActivationService } from '../../activation/types' ;
7
+ import { IApplicationShell , IApplicationEnvironment , IWorkspaceService } from '../../common/application/types' ;
8
8
import { inTerminalEnvVarExperiment } from '../../common/experiments/helpers' ;
9
9
import { IPlatformService } from '../../common/platform/types' ;
10
10
import { identifyShellFromShellPath } from '../../common/terminal/shellDetectors/baseShellDetector' ;
@@ -14,6 +14,7 @@ import {
14
14
Resource ,
15
15
IDisposableRegistry ,
16
16
IConfigurationService ,
17
+ IPathUtils ,
17
18
} from '../../common/types' ;
18
19
import { Deferred , createDeferred } from '../../common/utils/async' ;
19
20
import { Interpreters } from '../../common/utils/localize' ;
@@ -23,14 +24,16 @@ import { defaultShells } from './service';
23
24
import { IEnvironmentActivationService } from './types' ;
24
25
25
26
@injectable ( )
26
- export class TerminalEnvVarCollectionService implements IExtensionSingleActivationService {
27
+ export class TerminalEnvVarCollectionService implements IExtensionActivationService {
27
28
public readonly supportedWorkspaceTypes = {
28
29
untrustedWorkspace : false ,
29
30
virtualWorkspace : false ,
30
31
} ;
31
32
32
33
private deferred : Deferred < void > | undefined ;
33
34
35
+ private registeredOnce = false ;
36
+
34
37
private previousEnvVars = _normCaseKeys ( process . env ) ;
35
38
36
39
constructor (
@@ -42,39 +45,51 @@ export class TerminalEnvVarCollectionService implements IExtensionSingleActivati
42
45
@inject ( IApplicationEnvironment ) private applicationEnvironment : IApplicationEnvironment ,
43
46
@inject ( IDisposableRegistry ) private disposables : IDisposableRegistry ,
44
47
@inject ( IEnvironmentActivationService ) private environmentActivationService : IEnvironmentActivationService ,
48
+ @inject ( IWorkspaceService ) private workspaceService : IWorkspaceService ,
45
49
@inject ( IConfigurationService ) private readonly configurationService : IConfigurationService ,
50
+ @inject ( IPathUtils ) private readonly pathUtils : IPathUtils ,
46
51
) { }
47
52
48
- public async activate ( ) : Promise < void > {
53
+ public async activate ( resource : Resource ) : Promise < void > {
49
54
if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
50
55
this . context . environmentVariableCollection . clear ( ) ;
51
56
return ;
52
57
}
53
- this . interpreterService . onDidChangeInterpreter (
54
- async ( resource ) => {
55
- this . showProgress ( ) ;
56
- await this . _applyCollection ( resource ) ;
57
- this . hideProgress ( ) ;
58
- } ,
59
- this ,
60
- this . disposables ,
61
- ) ;
62
- this . applicationEnvironment . onDidChangeShell (
63
- async ( shell : string ) => {
64
- this . showProgress ( ) ;
65
- // Pass in the shell where known instead of relying on the application environment, because of bug
66
- // on VSCode: https://github.com/microsoft/vscode/issues/160694
67
- await this . _applyCollection ( undefined , shell ) ;
68
- this . hideProgress ( ) ;
69
- } ,
70
- this ,
71
- this . disposables ,
72
- ) ;
73
-
74
- this . _applyCollection ( undefined ) . ignoreErrors ( ) ;
58
+ if ( ! this . registeredOnce ) {
59
+ this . interpreterService . onDidChangeInterpreter (
60
+ async ( r ) => {
61
+ this . showProgress ( ) ;
62
+ await this . _applyCollection ( r ) . ignoreErrors ( ) ;
63
+ this . hideProgress ( ) ;
64
+ } ,
65
+ this ,
66
+ this . disposables ,
67
+ ) ;
68
+ this . applicationEnvironment . onDidChangeShell (
69
+ async ( shell : string ) => {
70
+ this . showProgress ( ) ;
71
+ // Pass in the shell where known instead of relying on the application environment, because of bug
72
+ // on VSCode: https://github.com/microsoft/vscode/issues/160694
73
+ await this . _applyCollection ( undefined , shell ) . ignoreErrors ( ) ;
74
+ this . hideProgress ( ) ;
75
+ } ,
76
+ this ,
77
+ this . disposables ,
78
+ ) ;
79
+ this . registeredOnce = true ;
80
+ }
81
+ this . _applyCollection ( resource ) . ignoreErrors ( ) ;
75
82
}
76
83
77
84
public async _applyCollection ( resource : Resource , shell = this . applicationEnvironment . shell ) : Promise < void > {
85
+ let workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
86
+ if (
87
+ ! workspaceFolder &&
88
+ Array . isArray ( this . workspaceService . workspaceFolders ) &&
89
+ this . workspaceService . workspaceFolders . length > 0
90
+ ) {
91
+ [ workspaceFolder ] = this . workspaceService . workspaceFolders ;
92
+ }
78
93
const settings = this . configurationService . getSettings ( resource ) ;
79
94
if ( ! settings . terminal . activateEnvironment ) {
80
95
traceVerbose ( 'Activating environments in terminal is disabled for' , resource ?. fsPath ) ;
@@ -95,7 +110,7 @@ export class TerminalEnvVarCollectionService implements IExtensionSingleActivati
95
110
await this . _applyCollection ( resource , defaultShell ?. shell ) ;
96
111
return ;
97
112
}
98
- this . context . environmentVariableCollection . clear ( ) ;
113
+ this . context . environmentVariableCollection . clear ( { workspaceFolder } ) ;
99
114
this . previousEnvVars = _normCaseKeys ( process . env ) ;
100
115
return ;
101
116
}
@@ -107,20 +122,25 @@ export class TerminalEnvVarCollectionService implements IExtensionSingleActivati
107
122
if ( prevValue !== value ) {
108
123
if ( value !== undefined ) {
109
124
traceVerbose ( `Setting environment variable ${ key } in collection to ${ value } ` ) ;
110
- this . context . environmentVariableCollection . replace ( key , value ) ;
125
+ this . context . environmentVariableCollection . replace ( key , value , { workspaceFolder } ) ;
111
126
} else {
112
127
traceVerbose ( `Clearing environment variable ${ key } from collection` ) ;
113
- this . context . environmentVariableCollection . delete ( key ) ;
128
+ this . context . environmentVariableCollection . delete ( key , { workspaceFolder } ) ;
114
129
}
115
130
}
116
131
} ) ;
117
132
Object . keys ( previousEnv ) . forEach ( ( key ) => {
118
133
// If the previous env var is not in the current env, clear it from collection.
119
134
if ( ! ( key in env ) ) {
120
135
traceVerbose ( `Clearing environment variable ${ key } from collection` ) ;
121
- this . context . environmentVariableCollection . delete ( key ) ;
136
+ this . context . environmentVariableCollection . delete ( key , { workspaceFolder } ) ;
122
137
}
123
138
} ) ;
139
+ const displayPath = this . pathUtils . getDisplayName ( settings . pythonPath , workspaceFolder ?. uri . fsPath ) ;
140
+ const description = new MarkdownString ( `${ Interpreters . activateTerminalDescription } \`${ displayPath } \`` ) ;
141
+ this . context . environmentVariableCollection . setDescription ( description , {
142
+ workspaceFolder,
143
+ } ) ;
124
144
}
125
145
126
146
@traceDecoratorVerbose ( 'Display activating terminals' )
0 commit comments