-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmanagers.ts
More file actions
221 lines (202 loc) · 7.26 KB
/
managers.ts
File metadata and controls
221 lines (202 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { commands, QuickInputButtons, QuickPickItem, QuickPickItemKind } from 'vscode';
import { PythonProjectCreator } from '../../api';
import { InternalEnvironmentManager, InternalPackageManager } from '../../internal.api';
import { Common, Pickers } from '../localize';
import { showQuickPickWithButtons } from '../window.apis';
function getDescription(mgr: InternalEnvironmentManager | InternalPackageManager): string | undefined {
if (mgr.description) {
return mgr.description;
}
if (mgr.tooltip) {
const tooltip = mgr.tooltip;
if (typeof tooltip === 'string') {
return tooltip;
}
return tooltip.value;
}
return undefined;
}
export async function pickEnvironmentManager(
managers: InternalEnvironmentManager[],
defaultManagers?: InternalEnvironmentManager[],
showBackButton?: boolean,
): Promise<string | undefined> {
if (managers.length === 0) {
return;
}
if (managers.length === 1 && !managers[0].supportsQuickCreate) {
// If there's only one manager and it doesn't support quick create, return its ID directly.
return managers[0].id;
}
const items: (QuickPickItem | (QuickPickItem & { id: string }))[] = [];
if (defaultManagers && defaultManagers.length > 0) {
items.push({
label: Common.recommended,
kind: QuickPickItemKind.Separator,
});
if (defaultManagers.length === 1 && defaultManagers[0].supportsQuickCreate) {
const defaultMgr = defaultManagers[0];
const details = defaultMgr.quickCreateConfig();
if (details) {
items.push({
label: Common.quickCreate,
description: `${defaultMgr.displayName} • ${details.description}`,
detail: details.detail,
id: `QuickCreate#${defaultMgr.id}`,
});
}
}
items.push(
...defaultManagers.map((defaultMgr) => ({
label: defaultMgr.displayName,
description: getDescription(defaultMgr),
id: defaultMgr.id,
})),
{
label: '',
kind: QuickPickItemKind.Separator,
},
);
}
items.push(
...managers
.filter((m) => !defaultManagers?.includes(m))
.map((m) => ({
label: m.displayName,
description: getDescription(m),
id: m.id,
})),
);
const item = await showQuickPickWithButtons(items, {
placeHolder: Pickers.Managers.selectEnvironmentManager,
ignoreFocusOut: true,
showBackButton,
});
return (item as QuickPickItem & { id: string })?.id;
}
export async function pickPackageManager(
managers: InternalPackageManager[],
defaultManagers?: InternalPackageManager[],
): Promise<string | undefined> {
if (managers.length === 0) {
return;
}
if (managers.length === 1) {
return managers[0].id;
}
const items: (QuickPickItem | (QuickPickItem & { id: string }))[] = [];
if (defaultManagers && defaultManagers.length > 0) {
items.push(
{
label: Common.recommended,
kind: QuickPickItemKind.Separator,
},
...defaultManagers.map((defaultMgr) => ({
label: defaultMgr.displayName,
description: getDescription(defaultMgr),
id: defaultMgr.id,
})),
{
label: '',
kind: QuickPickItemKind.Separator,
},
);
}
items.push(
...managers
.filter((m) => !defaultManagers?.includes(m))
.map((m) => ({
label: m.displayName,
description: getDescription(m),
id: m.id,
})),
);
const item = await showQuickPickWithButtons(items, {
placeHolder: Pickers.Managers.selectPackageManager,
ignoreFocusOut: true,
});
return (item as QuickPickItem & { id: string })?.id;
}
export async function pickCreator(creators: PythonProjectCreator[]): Promise<PythonProjectCreator | undefined> {
if (creators.length === 0) {
return;
}
if (creators.length === 1) {
return creators[0];
}
// First level menu
const autoFindCreator = creators.find((c) => c.name === 'autoProjects');
const existingProjectsCreator = creators.find((c) => c.name === 'existingProjects');
const items: QuickPickItem[] = [
{
label: 'Auto Find',
description: autoFindCreator?.description ?? 'Automatically find Python projects',
},
{
label: 'Select Existing',
description: existingProjectsCreator?.description ?? 'Select existing Python projects',
},
{
label: 'Create New',
description: 'Create a Python project from a template',
},
];
const selected = await showQuickPickWithButtons(items, {
placeHolder: Pickers.Managers.selectProjectCreator,
ignoreFocusOut: true,
});
if (!selected) {
return undefined;
}
// Return appropriate creator based on selection
// Handle case where selected could be an array (should not happen, but for type safety)
const selectedItem = Array.isArray(selected) ? selected[0] : selected;
if (!selectedItem) {
return undefined;
}
switch (selectedItem.label) {
case 'Auto Find':
return autoFindCreator;
case 'Select Existing':
return existingProjectsCreator;
case 'Create New':
return newProjectSelection(creators);
}
return undefined;
}
export async function newProjectSelection(creators: PythonProjectCreator[]): Promise<PythonProjectCreator | undefined> {
const otherCreators = creators.filter((c) => c.name !== 'autoProjects' && c.name !== 'existingProjects');
// Show second level menu for other creators
if (otherCreators.length === 0) {
return undefined;
}
const newItems: (QuickPickItem & { c: PythonProjectCreator })[] = otherCreators.map((c) => ({
label: c.displayName ?? c.name,
description: c.description,
c: c,
}));
try {
const newSelected = await showQuickPickWithButtons(newItems, {
placeHolder: 'Select project type for new project',
ignoreFocusOut: true,
showBackButton: true,
});
if (!newSelected) {
// User cancelled the picker
return undefined;
}
// Handle back button
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((newSelected as any)?.kind === -1 || (newSelected as any)?.back === true) {
// User pressed the back button, re-show the first menu
return pickCreator(creators);
}
// Handle case where newSelected could be an array (should not happen, but for type safety)
const selectedCreator = Array.isArray(newSelected) ? newSelected[0] : newSelected;
return selectedCreator?.c;
} catch (ex) {
if (ex === QuickInputButtons.Back) {
await commands.executeCommand('python-envs.addPythonProject');
}
}
}