Skip to content

Commit 6ccbb2f

Browse files
Hide "Add Python Project" context menu for already selected projects & Rename button (#432)
edited by @eleanorjboyd for clarity ## Problem Currently, when right-clicking on a folder or file that is already added as a Python project, the "Add Python Project" context menu item is still shown. This can be confusing to users as they might think they need to add the project again, or that the project wasn't properly added the first time. ## To Test - When user right-clicks on a file/folder not yet added as a Python project: "Add as Python Project" is shown - When user right-clicks on a file/folder already added as a Python project: The menu item is hidden This change improves the user experience by reducing confusion and providing clearer contextual actions. Fixes #272 and #327 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent 596d86a commit 6ccbb2f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,12 @@
457457
{
458458
"command": "python-envs.addPythonProject",
459459
"group": "inline",
460-
"when": "explorerViewletVisible && explorerResourceIsFolder"
460+
"when": "explorerViewletVisible && explorerResourceIsFolder && !python-envs:isExistingProject"
461461
},
462462
{
463463
"command": "python-envs.addPythonProject",
464464
"group": "inline",
465-
"when": "explorerViewletVisible && resourceExtname == .py"
465+
"when": "explorerViewletVisible && resourceExtname == .py && !python-envs:isExistingProject"
466466
}
467467
],
468468
"editor/title/run": [

package.nls.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"python-envs.terminal.revertStartupScriptChanges.title": "Revert Shell Startup Script Changes",
1414
"python-envs.setEnvManager.title": "Set Environment Manager",
1515
"python-envs.setPkgManager.title": "Set Package Manager",
16-
"python-envs.addPythonProject.title": "Add Python Project",
16+
"python-envs.addPythonProject.title": "Add as Python Project",
1717
"python-envs.removePythonProject.title": "Remove Python Project",
1818
"python-envs.copyEnvPath.title": "Copy Environment Path",
1919
"python-envs.copyProjectPath.title": "Copy Project Path",
@@ -37,4 +37,4 @@
3737
"python-envs.uninstallPackage.title": "Uninstall Package",
3838
"python.languageModelTools.python_environment.userDescription": "Get Python environment info for a file or path, including version, packages, and the command to run it.",
3939
"python.languageModelTools.python_install_package.userDescription": "Installs Python packages in the given workspace."
40-
}
40+
}

src/extension.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, ExtensionContext, LogOutputChannel, Terminal, Uri } from 'vscode';
1+
import { commands, ExtensionContext, LogOutputChannel, Terminal, Uri, window } from 'vscode';
22
import { PythonEnvironment, PythonEnvironmentApi } from './api';
33
import { ensureCorrectVersion } from './common/extVersion';
44
import { registerTools } from './common/lm.apis';
@@ -13,7 +13,6 @@ import {
1313
activeTerminal,
1414
createLogOutputChannel,
1515
onDidChangeActiveTerminal,
16-
onDidChangeActiveTextEditor,
1716
onDidChangeTerminalShellIntegration,
1817
} from './common/window.apis';
1918
import { createManagerReady } from './features/common/managerReady';
@@ -88,6 +87,14 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
8887
const projectManager: PythonProjectManager = new PythonProjectManagerImpl();
8988
context.subscriptions.push(projectManager);
9089

90+
// Helper function to check if a resource is an existing Python project
91+
const isExistingProject = (uri: Uri | undefined): boolean => {
92+
if (!uri) {
93+
return false;
94+
}
95+
return projectManager.get(uri) !== undefined;
96+
};
97+
9198
const envVarManager: EnvVarManager = new PythonEnvVariableManager(projectManager);
9299
context.subscriptions.push(envVarManager);
93100

@@ -194,6 +201,10 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
194201
await setPackageManagerCommand(envManagers, projectManager);
195202
}),
196203
commands.registerCommand('python-envs.addPythonProject', async (resource) => {
204+
// Set context to show/hide menu item depending on whether the resource is already a Python project
205+
if (resource instanceof Uri) {
206+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(resource));
207+
}
197208
await addPythonProjectCommand(resource, projectManager, envManagers, projectCreators);
198209
}),
199210
commands.registerCommand('python-envs.removePythonProject', async (item) => {
@@ -254,7 +265,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
254265
}
255266
}
256267
}),
257-
onDidChangeActiveTextEditor(async () => {
268+
window.onDidChangeActiveTextEditor(async () => {
258269
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
259270
}),
260271
envManagers.onDidChangeEnvironment(async () => {

0 commit comments

Comments
 (0)