-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprojectView.ts
More file actions
307 lines (274 loc) · 11.6 KB
/
projectView.ts
File metadata and controls
307 lines (274 loc) · 11.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
import {
Disposable,
Event,
EventEmitter,
ProviderResult,
TreeDataProvider,
TreeItem,
TreeView,
Uri,
window,
} from 'vscode';
import { PythonEnvironment } from '../../api';
import { ProjectViews } from '../../common/localize';
import { createSimpleDebounce } from '../../common/utils/debounce';
import { onDidChangeConfiguration, onDidDeleteFiles, onDidRenameFiles } from '../../common/workspace.apis';
import { EnvironmentManagers, PythonProjectManager } from '../../internal.api';
import {
GlobalProjectItem,
NoProjectEnvironment,
ProjectEnvironment,
ProjectEnvironmentInfo,
ProjectItem,
ProjectPackage,
ProjectTreeItem,
ProjectTreeItemKind,
} from './treeViewItems';
export class ProjectView implements TreeDataProvider<ProjectTreeItem> {
private treeView: TreeView<ProjectTreeItem>;
private _treeDataChanged: EventEmitter<ProjectTreeItem | ProjectTreeItem[] | null | undefined> = new EventEmitter<
ProjectTreeItem | ProjectTreeItem[] | null | undefined
>();
private projectViews: Map<string, ProjectItem> = new Map();
private revealMap: Map<string, ProjectEnvironment> = new Map();
private packageRoots: Map<string, ProjectEnvironment> = new Map();
private disposables: Disposable[] = [];
private debouncedUpdateProject = createSimpleDebounce(500, () => this.updateProject());
public constructor(private envManagers: EnvironmentManagers, private projectManager: PythonProjectManager) {
this.treeView = window.createTreeView<ProjectTreeItem>('python-projects', {
treeDataProvider: this,
});
this.disposables.push(
new Disposable(() => {
this.packageRoots.clear();
this.revealMap.clear();
this.projectViews.clear();
}),
this.treeView,
this._treeDataChanged,
this.projectManager.onDidChangeProjects(() => {
this.debouncedUpdateProject.trigger();
}),
this.envManagers.onDidChangeEnvironment(() => {
this.debouncedUpdateProject.trigger();
}),
this.envManagers.onDidChangeEnvironments(() => {
this.debouncedUpdateProject.trigger();
}),
this.envManagers.onDidChangePackages((e) => {
this.updatePackagesForEnvironment(e.environment);
}),
onDidChangeConfiguration(async (e) => {
if (
e.affectsConfiguration('python-envs.defaultEnvManager') ||
e.affectsConfiguration('python-envs.pythonProjects') ||
e.affectsConfiguration('python-envs.defaultPackageManager')
) {
this.debouncedUpdateProject.trigger();
}
}),
onDidRenameFiles((e) => {
this.handleFileRenames(e);
}),
onDidDeleteFiles((e) => {
this.handleFileDeletions(e);
}),
);
}
initialize(): void {
this.projectManager.initialize();
}
updateProject(): void {
this._treeDataChanged.fire(undefined);
}
private updatePackagesForEnvironment(e: PythonEnvironment): void {
const views: ProjectTreeItem[] = [];
// Look for environments matching this environment ID and refresh them
this.revealMap.forEach((v) => {
if (v.environment.envId.id === e.envId.id) {
views.push(v);
}
});
this._treeDataChanged.fire(views);
}
private revealInternal(view: ProjectEnvironment): void {
if (this.treeView.visible) {
setImmediate(async () => {
await this.treeView.reveal(view);
});
}
}
reveal(context: Uri | PythonEnvironment): PythonEnvironment | undefined {
if (context instanceof Uri) {
const pw = this.projectManager.get(context);
const key = pw ? pw.uri.fsPath : 'global';
const view = this.revealMap.get(key);
if (view) {
this.revealInternal(view);
return view.environment;
}
} else {
const view = Array.from(this.revealMap.values()).find((v) => v.environment.envId.id === context.envId.id);
if (view) {
this.revealInternal(view);
return view.environment;
}
}
return undefined;
}
onDidChangeTreeData: Event<void | ProjectTreeItem | ProjectTreeItem[] | null | undefined> | undefined =
this._treeDataChanged.event;
getTreeItem(element: ProjectTreeItem): TreeItem | Thenable<TreeItem> {
return element.treeItem;
}
/**
* Returns the children of a given element in the project tree view:
* If param is undefined, return root project items
* If param is a project, returns its environments.
* If param is an environment, returns its packages.
* @param element The tree item for which to get children.
*/
async getChildren(element?: ProjectTreeItem | undefined): Promise<ProjectTreeItem[] | undefined> {
if (element === undefined) {
// Return the root items
this.projectViews.clear();
const views: ProjectTreeItem[] = [];
const projects = this.projectManager.getProjects();
projects.forEach((w) => {
const view = new ProjectItem(w);
this.projectViews.set(w.uri.fsPath, view);
views.push(view);
});
if (projects.length === 0) {
views.push(new GlobalProjectItem());
}
return views;
}
if (element.kind === ProjectTreeItemKind.project) {
const projectItem = element as ProjectItem;
if (this.envManagers.managers.length === 0) {
return [
new NoProjectEnvironment(
projectItem.project,
projectItem,
ProjectViews.waitingForEnvManager,
undefined,
undefined,
'$(loading~spin)',
),
];
}
const uri = projectItem.id === 'global' ? undefined : projectItem.project.uri;
const manager = this.envManagers.getEnvironmentManager(uri);
if (!manager) {
return [
new NoProjectEnvironment(
projectItem.project,
projectItem,
ProjectViews.noEnvironmentManager,
ProjectViews.noEnvironmentManagerDescription,
),
];
}
const environment = await this.envManagers.getEnvironment(uri);
if (!environment) {
return [
new NoProjectEnvironment(
projectItem.project,
projectItem,
`${ProjectViews.noEnvironmentProvided} ${manager.displayName}`,
),
];
}
const view = new ProjectEnvironment(projectItem, environment);
this.revealMap.set(uri ? uri.fsPath : 'global', view);
return [view];
}
if (element.kind === ProjectTreeItemKind.environment) {
// Return packages directly under the environment
const environmentItem = element as ProjectEnvironment;
const parent = environmentItem.parent;
const uri = parent.id === 'global' ? undefined : parent.project.uri;
const pkgManager = this.envManagers.getPackageManager(uri);
const environment = environmentItem.environment;
if (!pkgManager) {
return [new ProjectEnvironmentInfo(environmentItem, ProjectViews.noPackageManager)];
}
let packages = await pkgManager.getPackages(environment);
if (!packages) {
return [new ProjectEnvironmentInfo(environmentItem, ProjectViews.noPackages)];
}
// Store the reference for refreshing packages
this.packageRoots.set(uri ? uri.fsPath : 'global', environmentItem);
return packages.map((p) => new ProjectPackage(environmentItem, p, pkgManager));
}
//return nothing if the element is not a project, environment, or undefined
return undefined;
}
getParent(element: ProjectTreeItem): ProviderResult<ProjectTreeItem> {
return element.parent;
}
private async handleFileRenames(e: {
readonly files: ReadonlyArray<{ readonly oldUri: Uri; readonly newUri: Uri }>;
}): Promise<void> {
const projects = this.projectManager.getProjects();
for (const { oldUri, newUri } of e.files) {
// Check if any project matches the old URI exactly or is contained within it
const affectedProjects = projects.filter((project) => {
const projectPath = project.uri.fsPath;
const oldPath = oldUri.fsPath;
// Check if the project path is the same as or is a child of the renamed path
return (
projectPath === oldPath ||
projectPath.startsWith(oldPath + '/') ||
projectPath.startsWith(oldPath + '\\')
);
});
for (const project of affectedProjects) {
const projectPath = project.uri.fsPath;
const oldPath = oldUri.fsPath;
const newPath = newUri.fsPath;
// Calculate the new project path
let newProjectPath: string;
if (projectPath === oldPath) {
// Project path is exactly the renamed path
newProjectPath = newPath;
} else {
// Project path is a child of the renamed path
const relativePath = projectPath.substring(oldPath.length);
newProjectPath = newPath + relativePath;
}
const newProjectUri = Uri.file(newProjectPath);
await this.projectManager.modifyProject(project.uri, undefined, newProjectUri);
}
if (affectedProjects.length > 0) {
// only trigger update if there are affected projects
this.debouncedUpdateProject.trigger();
}
}
}
private handleFileDeletions(e: { readonly files: ReadonlyArray<Uri> }): void {
const projects = this.projectManager.getProjects();
for (const deletedUri of e.files) {
// Check if any project matches the deleted URI exactly or is contained within it
const affectedProjects = projects.filter((project) => {
const projectPath = project.uri.fsPath;
const deletedPath = deletedUri.fsPath;
// Check if the project path is the same as or is a child of the deleted path
return (
projectPath === deletedPath ||
projectPath.startsWith(deletedPath + '/') ||
projectPath.startsWith(deletedPath + '\\')
);
});
if (affectedProjects.length > 0) {
this.projectManager.remove(affectedProjects);
// If there are affected projects, trigger an update
this.debouncedUpdateProject.trigger();
}
}
}
dispose() {
this.disposables.forEach((d) => d.dispose());
}
}