Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@
{
"command": "python-envs.refreshPackages",
"group": "inline",
"when": "view == python-projects && viewItem == python-package-root"
"when": "view == python-projects && viewItem == python-env"
},
{
"command": "python-envs.removePythonProject",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
await Promise.all(envManagers.managers.map((m) => m.refresh(undefined)));
}),
commands.registerCommand('python-envs.refreshPackages', async (item) => {
await refreshPackagesCommand(item);
await refreshPackagesCommand(item, envManagers);
}),
commands.registerCommand('python-envs.create', async (item) => {
return await createEnvironmentCommand(item, envManagers, projectManager);
Expand Down
18 changes: 11 additions & 7 deletions src/features/envCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
ProjectEnvironment,
ProjectItem,
ProjectPackage,
ProjectPackageRootTreeItem,
PythonEnvTreeItem,
} from './views/treeViewItems';

Expand All @@ -48,11 +47,15 @@ export async function refreshManagerCommand(context: unknown): Promise<void> {
}
}

export async function refreshPackagesCommand(context: unknown) {
if (context instanceof ProjectPackageRootTreeItem) {
const view = context as ProjectPackageRootTreeItem;
const manager = view.manager;
await manager.refresh(view.environment);
export async function refreshPackagesCommand(context: unknown, managers?: EnvironmentManagers) {
if (context instanceof ProjectEnvironment) {
const view = context as ProjectEnvironment;
if (managers) {
const pkgManager = managers.getPackageManager(view.parent.project.uri);
if (pkgManager) {
await pkgManager.refresh(view.environment);
}
}
} else if (context instanceof PackageRootTreeItem) {
const view = context as PackageRootTreeItem;
const manager = view.manager;
Expand Down Expand Up @@ -192,7 +195,8 @@ export async function removeEnvironmentCommand(context: unknown, managers: Envir
export async function handlePackageUninstall(context: unknown, em: EnvironmentManagers) {
if (context instanceof PackageTreeItem || context instanceof ProjectPackage) {
const moduleName = context.pkg.name;
const environment = context.parent.environment;
const environment =
context instanceof ProjectPackage ? context.parent.environment : context.parent.environment;
const packageManager = em.getPackageManager(environment);
await packageManager?.manage(environment, { uninstall: [moduleName], install: [] });
return;
Expand Down
47 changes: 23 additions & 24 deletions src/features/views/projectView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
ProjectEnvironmentInfo,
ProjectItem,
ProjectPackage,
ProjectPackageRootInfoTreeItem,
ProjectPackageRootTreeItem,
ProjectTreeItem,
ProjectTreeItemKind,
} from './treeViewItems';
Expand All @@ -34,7 +32,7 @@ export class ProjectView implements TreeDataProvider<ProjectTreeItem> {
>();
private projectViews: Map<string, ProjectItem> = new Map();
private revealMap: Map<string, ProjectEnvironment> = new Map();
private packageRoots: Map<string, ProjectPackageRootTreeItem> = 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) {
Expand Down Expand Up @@ -83,7 +81,8 @@ export class ProjectView implements TreeDataProvider<ProjectTreeItem> {

private updatePackagesForEnvironment(e: PythonEnvironment): void {
const views: ProjectTreeItem[] = [];
this.packageRoots.forEach((v) => {
// 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);
}
Expand Down Expand Up @@ -125,8 +124,16 @@ export class ProjectView implements TreeDataProvider<ProjectTreeItem> {
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();
Expand Down Expand Up @@ -187,38 +194,30 @@ export class ProjectView implements TreeDataProvider<ProjectTreeItem> {
}

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;

const views: ProjectTreeItem[] = [];
if (!pkgManager) {
return [new ProjectEnvironmentInfo(environmentItem, ProjectViews.noPackageManager)];
}

if (pkgManager) {
const item = new ProjectPackageRootTreeItem(environmentItem, pkgManager, environment);
this.packageRoots.set(uri ? uri.fsPath : 'global', item);
views.push(item);
} else {
views.push(new ProjectEnvironmentInfo(environmentItem, ProjectViews.noPackageManager));
let packages = await pkgManager.getPackages(environment);
if (!packages) {
return [new ProjectEnvironmentInfo(environmentItem, ProjectViews.noPackages)];
}
return views;
}

if (element.kind === ProjectTreeItemKind.packageRoot) {
const root = element as ProjectPackageRootTreeItem;
const manager = root.manager;
const environment = root.environment;
let packages = await manager.getPackages(environment);
const views: ProjectTreeItem[] = [];
// Store the reference for refreshing packages
this.packageRoots.set(uri ? uri.fsPath : 'global', environmentItem);

if (packages) {
return packages.map((p) => new ProjectPackage(root, p, manager));
} else {
views.push(new ProjectPackageRootInfoTreeItem(root, ProjectViews.noPackages));
}
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> {
Expand Down
4 changes: 2 additions & 2 deletions src/features/views/treeViewItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class ProjectPackage implements ProjectTreeItem {
public readonly id: string;
public readonly treeItem: TreeItem;
constructor(
public readonly parent: ProjectPackageRootTreeItem,
public readonly parent: ProjectEnvironment,
public readonly pkg: Package,
public readonly manager: InternalPackageManager,
) {
Expand All @@ -395,7 +395,7 @@ export class ProjectPackage implements ProjectTreeItem {
this.treeItem = item;
}

static getId(projectEnv: ProjectPackageRootTreeItem, pkg: Package): string {
static getId(projectEnv: ProjectEnvironment, pkg: Package): string {
return `${projectEnv.id}>>>${pkg.pkgId}`;
}
}
Expand Down