Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@
"category": "Python Envs",
"icon": "$(folder-opened)"
},
{
"command": "python-envs.revealEnvInManagerView",
"title": "%python-envs.revealEnvInManagerView.title%",
"category": "Python Envs",
"icon": "$(eye)"
},
{
"command": "python-envs.runPetInTerminal",
"title": "%python-envs.runPetInTerminal.title%",
Expand Down Expand Up @@ -423,6 +429,10 @@
"command": "python-envs.revealProjectInExplorer",
"when": "false"
},
{
"command": "python-envs.revealEnvInManagerView",
"when": "false"
},
{
"command": "python-envs.createNewProjectFromTemplate",
"when": "config.python.useEnvironmentsExtension != false"
Expand Down Expand Up @@ -531,6 +541,11 @@
"command": "python-envs.uninstallPackage",
"group": "inline",
"when": "view == python-projects && viewItem == python-package"
},
{
"command": "python-envs.revealEnvInManagerView",
"group": "inline",
"when": "view == python-projects && viewItem == python-env"
}
],
"view/title": [
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"python-envs.terminal.deactivate.title": "Deactivate Environment in Current Terminal",
"python-envs.uninstallPackage.title": "Uninstall Package",
"python-envs.revealProjectInExplorer.title": "Reveal Project in Explorer",
"python-envs.revealEnvInManagerView.title": "Reveal in Environment Managers View",
"python-envs.runPetInTerminal.title": "Run Python Environment Tool (PET) in Terminal...",
"python-envs.alwaysUseUv.description": "When set to true, uv will be used to manage all virtual environments if available. When set to false, uv will only manage virtual environments explicitly created by uv."
}
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
refreshPackagesCommand,
removeEnvironmentCommand,
removePythonProject,
revealEnvInManagerView,
revealProjectInExplorer,
runAsTaskCommand,
runInDedicatedTerminalCommand,
Expand Down Expand Up @@ -312,6 +313,9 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
commands.registerCommand('python-envs.revealProjectInExplorer', async (item) => {
await revealProjectInExplorer(item);
}),
commands.registerCommand('python-envs.revealEnvInManagerView', async (item) => {
await revealEnvInManagerView(item, managerView);
}),
commands.registerCommand('python-envs.terminal.activate', async () => {
const terminal = activeTerminal();
if (terminal) {
Expand Down
14 changes: 14 additions & 0 deletions src/features/envCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
import { runAsTask } from './execution/runAsTask';
import { runInTerminal } from './terminal/runInTerminal';
import { TerminalManager } from './terminal/terminalManager';
import { EnvManagerView } from './views/envManagersView';
import {
EnvManagerTreeItem,
EnvTreeItemKind,
Expand Down Expand Up @@ -751,3 +752,16 @@ export async function revealProjectInExplorer(item: unknown): Promise<void> {
traceVerbose(`Invalid context for reveal project in explorer: ${item}`);
}
}

/**
* Focuses the Environment Managers view and reveals the given project environment.
*/
export async function revealEnvInManagerView(item: unknown, managerView: EnvManagerView): Promise<void> {
if (item instanceof ProjectEnvironment) {
await commands.executeCommand('env-managers.focus');
await managerView.reveal(item.environment);
return;
}

traceVerbose(`Invalid context for reveal environment in manager view: ${item}`);
}
58 changes: 51 additions & 7 deletions src/features/views/envManagersView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ export class EnvManagerView implements TreeDataProvider<EnvTreeItem>, Disposable
>();
private revealMap = new Map<string, PythonEnvTreeItem>();
private managerViews = new Map<string, EnvManagerTreeItem>();
private groupViews = new Map<string, PythonGroupEnvTreeItem>();
private selected: Map<string, string> = new Map();
private disposables: Disposable[] = [];

public constructor(public providers: EnvironmentManagers, private stateManager: ITemporaryStateManager) {
public constructor(
public providers: EnvironmentManagers,
private stateManager: ITemporaryStateManager,
) {
this.treeView = window.createTreeView<EnvTreeItem>('env-managers', {
treeDataProvider: this,
});
Expand All @@ -46,6 +50,7 @@ export class EnvManagerView implements TreeDataProvider<EnvTreeItem>, Disposable
new Disposable(() => {
this.revealMap.clear();
this.managerViews.clear();
this.groupViews.clear();
this.selected.clear();
}),
this.treeView,
Expand Down Expand Up @@ -107,6 +112,7 @@ export class EnvManagerView implements TreeDataProvider<EnvTreeItem>, Disposable
if (!element) {
const views: EnvTreeItem[] = [];
this.managerViews.clear();
this.groupViews.clear();
this.providers.managers.forEach((m) => {
const view = new EnvManagerTreeItem(m);
views.push(view);
Expand Down Expand Up @@ -137,7 +143,10 @@ export class EnvManagerView implements TreeDataProvider<EnvTreeItem>, Disposable
});

groupObjects.forEach((group) => {
views.push(new PythonGroupEnvTreeItem(element as EnvManagerTreeItem, group));
const groupView = new PythonGroupEnvTreeItem(element as EnvManagerTreeItem, group);
const groupName = typeof group === 'string' ? group : group.name;
this.groupViews.set(`${manager.id}:${groupName}`, groupView);
views.push(groupView);
});

if (views.length === 0) {
Expand Down Expand Up @@ -202,12 +211,47 @@ export class EnvManagerView implements TreeDataProvider<EnvTreeItem>, Disposable
return element.parent;
}

reveal(environment?: PythonEnvironment) {
const view = environment ? this.revealMap.get(environment.envId.id) : undefined;
/**
* Reveals and focuses on the given environment in the Environment Managers view.
*
* @param environment - The Python environment to reveal
*/
async reveal(environment?: PythonEnvironment): Promise<void> {
if (!environment) {
return;
}

const manager = this.providers.getEnvironmentManager(environment);
if (!manager) {
return;
}

if (!this.managerViews.has(manager.id)) {
await this.getChildren(undefined);
}

const managerView = this.managerViews.get(manager.id);
if (!managerView) {
return;
}

const groupName = typeof environment.group === 'string' ? environment.group : environment.group?.name;
if (groupName) {
if (!this.groupViews.has(`${manager.id}:${groupName}`)) {
await this.getChildren(managerView);
}

const groupView = this.groupViews.get(`${manager.id}:${groupName}`);
if (groupView) {
await this.getChildren(groupView);
}
} else {
await this.getChildren(managerView);
}

const view = this.revealMap.get(environment.envId.id);
if (view && this.treeView.visible) {
setImmediate(async () => {
await this.treeView.reveal(view);
});
await this.treeView.reveal(view, { expand: false, focus: true, select: true });
}
}

Expand Down
52 changes: 50 additions & 2 deletions src/test/features/envCommands.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as typeMoq from 'typemoq';
import { Uri } from 'vscode';
import { commands, Uri } from 'vscode';
import { PythonEnvironment, PythonProject } from '../../api';
import * as managerApi from '../../common/pickers/managers';
import * as projectApi from '../../common/pickers/projects';
import { createAnyEnvironmentCommand } from '../../features/envCommands';
import { createAnyEnvironmentCommand, revealEnvInManagerView } from '../../features/envCommands';
import { EnvManagerView } from '../../features/views/envManagersView';
import { ProjectEnvironment, ProjectItem } from '../../features/views/treeViewItems';
import { EnvironmentManagers, InternalEnvironmentManager, PythonProjectManager } from '../../internal.api';
import { setupNonThenable } from '../mocks/helper';

Expand Down Expand Up @@ -175,3 +177,49 @@ suite('Create Any Environment Command Tests', () => {
em.verifyAll();
});
});

suite('Reveal Env In Manager View Command Tests', () => {
let managerView: typeMoq.IMock<EnvManagerView>;
let executeCommandStub: sinon.SinonStub;

setup(() => {
managerView = typeMoq.Mock.ofType<EnvManagerView>();
setupNonThenable(managerView);
executeCommandStub = sinon.stub(commands, 'executeCommand');
});

teardown(() => {
sinon.restore();
});

test('Focuses env-managers view and reveals environment when given a ProjectEnvironment', async () => {
// Mock
const project: PythonProject = {
uri: Uri.file('/test/project'),
name: 'test-project',
};
const projectItem = new ProjectItem(project);

const environment: PythonEnvironment = {
envId: { id: 'test-env-id', managerId: 'test-manager' },
name: 'test-env',
displayName: 'Test Environment',
displayPath: '/path/to/env',
version: '3.10.0',
environmentPath: Uri.file('/path/to/env'),
execInfo: { run: { executable: '/path/to/python' }, activatedRun: { executable: '/path/to/python' } },
sysPrefix: '/path/to/env',
};
const projectEnv = new ProjectEnvironment(projectItem, environment);

executeCommandStub.resolves();
managerView.setup((m) => m.reveal(environment)).returns(() => Promise.resolve());

// Run
await revealEnvInManagerView(projectEnv, managerView.object);

// Assert
assert.ok(executeCommandStub.calledOnceWith('env-managers.focus'), 'Should focus the env-managers view');
managerView.verify((m) => m.reveal(environment), typeMoq.Times.once());
});
});
Loading
Loading