-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtreeViewItems.ts
More file actions
404 lines (377 loc) · 15.6 KB
/
treeViewItems.ts
File metadata and controls
404 lines (377 loc) · 15.6 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { Command, MarkdownString, ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
import { EnvironmentGroupInfo, IconPath, Package, PythonEnvironment, PythonProject } from '../../api';
import { EnvViewStrings } from '../../common/localize';
import { InternalEnvironmentManager, InternalPackageManager } from '../../internal.api';
import { isActivatableEnvironment } from '../common/activation';
import { removable } from './utils';
export enum EnvTreeItemKind {
manager = 'python-env-manager',
environment = 'python-env',
environmentGroup = 'python-env-group',
noEnvironment = 'python-no-env',
package = 'python-package',
managerInfo = 'python-env-manager-info',
environmentInfo = 'python-env-info',
packageInfo = 'python-package-info',
}
export interface EnvTreeItem {
kind: EnvTreeItemKind;
treeItem: TreeItem;
parent?: EnvTreeItem;
}
export class EnvManagerTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.manager;
public readonly treeItem: TreeItem;
public readonly parent: undefined;
constructor(public readonly manager: InternalEnvironmentManager) {
const item = new TreeItem(manager.displayName, TreeItemCollapsibleState.Collapsed);
item.contextValue = this.getContextValue();
item.description = manager.description;
item.tooltip = manager.tooltip ?? manager.description;
item.iconPath = manager.iconPath;
this.treeItem = item;
}
private getContextValue() {
const create = this.manager.supportsCreate ? 'create' : '';
const parts = ['pythonEnvManager', create, this.manager.id].filter(Boolean);
return parts.join(';') + ';';
}
}
export class PythonGroupEnvTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.environmentGroup;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: EnvManagerTreeItem,
public readonly group: string | EnvironmentGroupInfo,
) {
const label = typeof group === 'string' ? group : group.name;
const item = new TreeItem(label, TreeItemCollapsibleState.Collapsed);
item.contextValue = `pythonEnvGroup;${this.parent.manager.id}:${label};`;
this.treeItem = item;
if (typeof group !== 'string') {
item.description = group.description;
item.tooltip = group.tooltip;
item.iconPath = group.iconPath;
}
}
}
export class PythonEnvTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.environment;
public readonly treeItem: TreeItem;
constructor(
public readonly environment: PythonEnvironment,
public readonly parent: EnvManagerTreeItem | PythonGroupEnvTreeItem,
public readonly selected?: string,
) {
let name = environment.displayName ?? environment.name;
let tooltip = environment.tooltip ?? environment.description;
const isBroken = !!environment.error;
if (selected) {
const selectedTooltip =
selected === 'global' ? EnvViewStrings.selectedGlobalTooltip : EnvViewStrings.selectedWorkspaceTooltip;
tooltip = tooltip ? `${selectedTooltip} ● ${tooltip}` : tooltip;
}
const item = new TreeItem(name, TreeItemCollapsibleState.Collapsed);
item.contextValue = this.getContextValue();
// Show error message for broken environments
item.description = isBroken ? environment.error : environment.description;
item.tooltip = isBroken ? environment.error : tooltip;
// Show warning icon for broken environments
item.iconPath = isBroken ? new ThemeIcon('warning') : environment.iconPath;
this.treeItem = item;
}
private getContextValue() {
const isBroken = !!this.environment.error;
const activatable = !isBroken && isActivatableEnvironment(this.environment) ? 'activatable' : '';
let remove = '';
if (this.parent.kind === EnvTreeItemKind.environmentGroup) {
remove = this.parent.parent.manager.supportsRemove ? 'remove' : '';
} else if (this.parent.kind === EnvTreeItemKind.manager) {
remove = this.parent.manager.supportsRemove ? 'remove' : '';
}
// Use different base context for broken environments so normal actions don't show
const baseContext = isBroken ? 'pythonBrokenEnvironment' : 'pythonEnvironment';
const parts = [baseContext, remove, activatable].filter(Boolean);
return parts.join(';') + ';';
}
}
export class NoPythonEnvTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.environment;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: EnvManagerTreeItem,
private readonly description?: string,
private readonly tooltip?: string | MarkdownString,
private readonly iconPath?: string | IconPath,
) {
const item = new TreeItem(
this.parent.manager.supportsCreate
? 'No environment found, click to create'
: 'No python environments found.',
TreeItemCollapsibleState.None,
);
item.contextValue = 'python-no-environment';
item.description = this.description;
item.tooltip = this.tooltip;
item.iconPath = this.iconPath ?? new ThemeIcon('circle-slash');
if (this.parent.manager.supportsCreate) {
item.command = {
command: 'python-envs.create',
title: 'Create Environment',
arguments: [this.parent],
};
}
this.treeItem = item;
}
}
export class PackageTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.package;
public readonly treeItem: TreeItem;
constructor(
public readonly pkg: Package,
public readonly parent: PythonEnvTreeItem,
public readonly manager: InternalPackageManager,
) {
const item = new TreeItem(pkg.displayName);
item.iconPath = pkg.iconPath;
item.contextValue = 'python-package';
item.description = pkg.description ?? pkg.version;
item.tooltip = pkg.tooltip;
this.treeItem = item;
}
}
export class EnvInfoTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.environmentInfo;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: PythonEnvTreeItem,
name: string,
description?: string,
tooltip?: string | MarkdownString,
iconPath?: string | IconPath,
command?: Command,
) {
const item = new TreeItem(name, TreeItemCollapsibleState.None);
item.contextValue = 'python-env-manager-info';
item.description = description;
item.tooltip = tooltip;
this.treeItem = item;
this.treeItem.iconPath = iconPath;
this.treeItem.command = command;
}
}
export class PackageRootInfoTreeItem implements EnvTreeItem {
public readonly kind = EnvTreeItemKind.packageInfo;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: PythonEnvTreeItem,
name: string,
description?: string,
tooltip?: string | MarkdownString,
iconPath?: string | IconPath,
command?: Command,
) {
const item = new TreeItem(name, TreeItemCollapsibleState.None);
item.contextValue = 'python-package-root-info';
item.description = description;
item.tooltip = tooltip;
this.treeItem = item;
this.treeItem.iconPath = iconPath;
this.treeItem.command = command;
}
}
export enum ProjectTreeItemKind {
project = 'project',
environment = 'project-environment',
none = 'project-no-environment',
environmentInfo = 'environment-info',
package = 'project-package',
packageRootInfo = 'project-package-root-info',
}
export interface ProjectTreeItem {
kind: ProjectTreeItemKind;
parent?: ProjectTreeItem;
id: string;
treeItem: TreeItem;
}
export class ProjectItem implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.project;
public readonly parent: undefined;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(public readonly project: PythonProject) {
this.id = ProjectItem.getId(this.project);
const item = new TreeItem(this.project.name, TreeItemCollapsibleState.Expanded);
item.contextValue = removable(this.project) ? 'python-workspace-removable' : 'python-workspace';
item.description = this.project.description;
item.tooltip = this.project.tooltip;
item.resourceUri = project.uri.fsPath.endsWith('.py') ? this.project.uri : undefined;
this.treeItem = item;
}
static getId(workspace: PythonProject): string {
return workspace.uri.toString();
}
}
export class GlobalProjectItem implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.project;
public readonly parent: undefined;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor() {
this.id = 'global';
const item = new TreeItem('Global', TreeItemCollapsibleState.Expanded);
item.contextValue = 'python-workspace';
item.description = 'Global Python environment';
item.tooltip = 'Global Python environment';
this.treeItem = item;
}
}
export class ProjectEnvironment implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.environment;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: ProjectItem,
public readonly environment: PythonEnvironment,
) {
this.id = this.getId(parent, environment);
const isBroken = !!environment.error;
const item = new TreeItem(
this.environment.displayName ?? this.environment.name,
TreeItemCollapsibleState.Collapsed,
);
item.contextValue = isBroken ? 'python-env;broken;' : 'python-env';
// Show error message for broken environments
item.description = isBroken ? this.environment.error : this.environment.description;
item.tooltip = isBroken ? this.environment.error : this.environment.tooltip;
// Show warning icon for broken environments
item.iconPath = isBroken ? new ThemeIcon('warning') : this.environment.iconPath;
this.treeItem = item;
}
getId(workspace: ProjectItem, environment: PythonEnvironment): string {
return `${workspace.id}>>>${environment.envId}`;
}
}
export class NoProjectEnvironment implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.none;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly project: PythonProject | undefined,
public readonly parent: ProjectItem,
private readonly label: string,
private readonly description?: string,
private readonly tooltip?: string | MarkdownString,
private readonly iconPath?: string | IconPath,
) {
const randomStr1 = Math.random().toString(36).substring(2);
this.id = `${this.parent.id}>>>none>>>${randomStr1}`;
const item = new TreeItem(this.label, TreeItemCollapsibleState.None);
item.contextValue = 'no-environment';
item.description = this.description;
item.tooltip = this.tooltip;
item.iconPath = this.iconPath ?? new ThemeIcon('circle-slash');
item.command = {
command: 'python-envs.set',
title: 'Set Environment',
arguments: this.project ? [this.project.uri] : undefined,
};
this.treeItem = item;
}
}
export class NoPackagesEnvironment implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.none;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly project: PythonProject,
public readonly parent: ProjectEnvironment,
private readonly description?: string,
private readonly tooltip?: string | MarkdownString,
private readonly iconPath?: string | IconPath,
) {
const randomStr1 = Math.random().toString(36).substring(2);
this.id = `${this.parent.id}>>>packages-none>>>${randomStr1}`;
const item = new TreeItem('Please select a package manager', TreeItemCollapsibleState.None);
item.contextValue = 'no-packages';
item.description = this.description;
item.tooltip = this.tooltip;
item.iconPath = this.iconPath ?? new ThemeIcon('circle-slash');
item.command = {
command: 'pythonEnvs.setPkgManager',
title: 'Set Package Manager',
arguments: [this.project.uri],
};
this.treeItem = item;
}
}
export class ProjectEnvironmentInfo implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.environmentInfo;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: ProjectEnvironment,
public readonly label: string,
private readonly description?: string,
private readonly tooltip?: string | MarkdownString,
private readonly iconPath?: string | IconPath,
private readonly command?: Command,
) {
const randomStr1 = Math.random().toString(36).substring(2);
const randomStr2 = Math.random().toString(36).substring(2);
this.id = `${this.parent.id}>>>info>>>${randomStr1}-${randomStr2}`;
const item = new TreeItem(this.label, TreeItemCollapsibleState.None);
item.contextValue = 'python-env-manager-info';
item.description = this.description;
item.tooltip = this.tooltip;
item.iconPath = this.iconPath;
item.command = this.command;
this.treeItem = item;
}
}
export class ProjectPackage implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.package;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: ProjectEnvironment,
public readonly pkg: Package,
public readonly manager: InternalPackageManager,
) {
this.id = ProjectPackage.getId(parent, pkg);
const item = new TreeItem(this.pkg.displayName, TreeItemCollapsibleState.None);
item.iconPath = this.pkg.iconPath;
item.contextValue = 'python-package';
item.description = this.pkg.description ?? this.pkg.version;
item.tooltip = this.pkg.tooltip;
this.treeItem = item;
}
static getId(projectEnv: ProjectEnvironment, pkg: Package): string {
return `${projectEnv.id}>>>${pkg.pkgId}`;
}
}
export class ProjectPackageRootInfoTreeItem implements ProjectTreeItem {
public readonly kind = ProjectTreeItemKind.packageRootInfo;
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: ProjectEnvironment,
name: string,
description?: string,
tooltip?: string | MarkdownString,
iconPath?: string | IconPath,
command?: Command,
) {
const item = new TreeItem(name, TreeItemCollapsibleState.None);
this.id = ProjectPackageRootInfoTreeItem.getId(parent, 'no-package');
item.contextValue = 'python-package-root-info';
item.description = description;
item.tooltip = tooltip;
this.treeItem = item;
this.treeItem.iconPath = iconPath;
this.treeItem.command = command;
}
static getId(projectEnv: ProjectEnvironment, name: string): string {
return `${projectEnv.id}>>>${name}`;
}
}