@@ -8,10 +8,12 @@ import * as path from 'path';
8
8
import { ConfigurationTarget , Disposable , QuickPickItem , Uri } from 'vscode' ;
9
9
import { IExtensionSingleActivationService } from '../../../../activation/types' ;
10
10
import { IApplicationShell , ICommandManager , IWorkspaceService } from '../../../../common/application/types' ;
11
- import { IDisposable , Resource } from '../../../../common/types' ;
12
- import { Interpreters } from '../../../../common/utils/localize' ;
11
+ import { IConfigurationService , IDisposable , IPathUtils , Resource } from '../../../../common/types' ;
12
+ import { Common , Interpreters } from '../../../../common/utils/localize' ;
13
13
import { IPythonPathUpdaterServiceManager } from '../../types' ;
14
-
14
+ export interface WorkspaceSelectionQuickPickItem extends QuickPickItem {
15
+ uri ?: Uri ;
16
+ }
15
17
@injectable ( )
16
18
export abstract class BaseInterpreterSelectorCommand implements IExtensionSingleActivationService , IDisposable {
17
19
public readonly supportedWorkspaceTypes = { untrustedWorkspace : false , virtualWorkspace : true } ;
@@ -21,6 +23,8 @@ export abstract class BaseInterpreterSelectorCommand implements IExtensionSingle
21
23
@unmanaged ( ) protected readonly commandManager : ICommandManager ,
22
24
@unmanaged ( ) protected readonly applicationShell : IApplicationShell ,
23
25
@unmanaged ( ) protected readonly workspaceService : IWorkspaceService ,
26
+ @unmanaged ( ) protected readonly pathUtils : IPathUtils ,
27
+ @unmanaged ( ) protected readonly configurationService : IConfigurationService ,
24
28
) {
25
29
this . disposables . push ( this ) ;
26
30
}
@@ -31,52 +35,85 @@ export abstract class BaseInterpreterSelectorCommand implements IExtensionSingle
31
35
32
36
public abstract activate ( ) : Promise < void > ;
33
37
34
- protected async getConfigTarget ( ) : Promise <
38
+ protected async getConfigTargets ( options ?: {
39
+ resetTarget ?: boolean ;
40
+ } ) : Promise <
35
41
| {
36
42
folderUri : Resource ;
37
43
configTarget : ConfigurationTarget ;
38
- }
44
+ } [ ]
39
45
| undefined
40
46
> {
41
- if (
42
- ! Array . isArray ( this . workspaceService . workspaceFolders ) ||
43
- this . workspaceService . workspaceFolders . length === 0
44
- ) {
45
- return {
46
- folderUri : undefined ,
47
- configTarget : ConfigurationTarget . Global ,
48
- } ;
47
+ const workspaceFolders = this . workspaceService . workspaceFolders ;
48
+ if ( workspaceFolders === undefined || workspaceFolders . length === 0 ) {
49
+ return [
50
+ {
51
+ folderUri : undefined ,
52
+ configTarget : ConfigurationTarget . Global ,
53
+ } ,
54
+ ] ;
49
55
}
50
- if ( ! this . workspaceService . workspaceFile && this . workspaceService . workspaceFolders . length === 1 ) {
51
- return {
52
- folderUri : this . workspaceService . workspaceFolders [ 0 ] . uri ,
53
- configTarget : ConfigurationTarget . WorkspaceFolder ,
54
- } ;
56
+ if ( ! this . workspaceService . workspaceFile && workspaceFolders . length === 1 ) {
57
+ return [
58
+ {
59
+ folderUri : workspaceFolders [ 0 ] . uri ,
60
+ configTarget : ConfigurationTarget . WorkspaceFolder ,
61
+ } ,
62
+ ] ;
55
63
}
56
64
57
65
// Ok we have multiple workspaces, get the user to pick a folder.
58
66
59
- type WorkspaceSelectionQuickPickItem = QuickPickItem & { uri : Uri } ;
60
- const quickPickItems : WorkspaceSelectionQuickPickItem [ ] = [
61
- ...this . workspaceService . workspaceFolders . map ( ( w ) => ( {
62
- label : w . name ,
63
- description : path . dirname ( w . uri . fsPath ) ,
64
- uri : w . uri ,
65
- } ) ) ,
67
+ let quickPickItems : WorkspaceSelectionQuickPickItem [ ] = options ?. resetTarget
68
+ ? [
69
+ {
70
+ label : Common . clearAll ( ) ,
71
+ } ,
72
+ ]
73
+ : [ ] ;
74
+ quickPickItems . push (
75
+ ...workspaceFolders . map ( ( w ) => {
76
+ const selectedInterpreter = this . pathUtils . getDisplayName (
77
+ this . configurationService . getSettings ( w . uri ) . pythonPath ,
78
+ w . uri . fsPath ,
79
+ ) ;
80
+ return {
81
+ label : w . name ,
82
+ description : this . pathUtils . getDisplayName ( path . dirname ( w . uri . fsPath ) ) ,
83
+ uri : w . uri ,
84
+ detail : selectedInterpreter ,
85
+ } ;
86
+ } ) ,
66
87
{
67
- label : Interpreters . entireWorkspace ( ) ,
68
- uri : this . workspaceService . workspaceFolders [ 0 ] . uri ,
88
+ label : options ?. resetTarget ? Interpreters . clearAtWorkspace ( ) : Interpreters . entireWorkspace ( ) ,
89
+ uri : workspaceFolders [ 0 ] . uri ,
69
90
} ,
70
- ] ;
91
+ ) ;
71
92
72
93
const selection = await this . applicationShell . showQuickPick ( quickPickItems , {
73
- placeHolder : 'Select the workspace to set the interpreter' ,
94
+ placeHolder : options ?. resetTarget
95
+ ? 'Select the workspace folder to clear the interpreter for'
96
+ : 'Select the workspace folder to set the interpreter' ,
74
97
} ) ;
75
98
99
+ if ( selection ?. label === Common . clearAll ( ) ) {
100
+ const folderTargets : {
101
+ folderUri : Resource ;
102
+ configTarget : ConfigurationTarget ;
103
+ } [ ] = workspaceFolders . map ( ( w ) => ( {
104
+ folderUri : w . uri ,
105
+ configTarget : ConfigurationTarget . WorkspaceFolder ,
106
+ } ) ) ;
107
+ return [
108
+ ...folderTargets ,
109
+ { folderUri : workspaceFolders [ 0 ] . uri , configTarget : ConfigurationTarget . Workspace } ,
110
+ ] ;
111
+ }
112
+
76
113
return selection
77
- ? selection . label === Interpreters . entireWorkspace ( )
78
- ? { folderUri : selection . uri , configTarget : ConfigurationTarget . Workspace }
79
- : { folderUri : selection . uri , configTarget : ConfigurationTarget . WorkspaceFolder }
114
+ ? selection . label === Interpreters . entireWorkspace ( ) || selection . label === Interpreters . clearAtWorkspace ( )
115
+ ? [ { folderUri : selection . uri , configTarget : ConfigurationTarget . Workspace } ]
116
+ : [ { folderUri : selection . uri , configTarget : ConfigurationTarget . WorkspaceFolder } ]
80
117
: undefined ;
81
118
}
82
119
}
0 commit comments