Skip to content

Commit f5aeea3

Browse files
Copiloteleanorjboyd
andcommitted
Hide 'Add Python Project' context menu for existing projects
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent 152a2a7 commit f5aeea3

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

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: 1 addition & 1 deletion
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",

src/extension.ts

Lines changed: 34 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, workspace, 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,34 @@ 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+
98+
// Update the context key when explorer selection changes
99+
context.subscriptions.push(
100+
window.onDidChangeActiveTextEditor((editor) => {
101+
if (editor) {
102+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(editor.document.uri));
103+
}
104+
}),
105+
window.onDidChangeWindowState(() => {
106+
const activeEditor = window.activeTextEditor;
107+
if (activeEditor) {
108+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(activeEditor.document.uri));
109+
}
110+
}),
111+
workspace.onDidOpenTextDocument((document) => {
112+
if (window.activeTextEditor && window.activeTextEditor.document === document) {
113+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(document.uri));
114+
}
115+
})
116+
);
117+
91118
const envVarManager: EnvVarManager = new PythonEnvVariableManager(projectManager);
92119
context.subscriptions.push(envVarManager);
93120

@@ -194,6 +221,10 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
194221
await setPackageManagerCommand(envManagers, projectManager);
195222
}),
196223
commands.registerCommand('python-envs.addPythonProject', async (resource) => {
224+
// Set context to show/hide menu item depending on whether the resource is already a Python project
225+
if (resource instanceof Uri) {
226+
commands.executeCommand('setContext', 'python-envs:isExistingProject', isExistingProject(resource));
227+
}
197228
await addPythonProjectCommand(resource, projectManager, envManagers, projectCreators);
198229
}),
199230
commands.registerCommand('python-envs.removePythonProject', async (item) => {
@@ -254,7 +285,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
254285
}
255286
}
256287
}),
257-
onDidChangeActiveTextEditor(async () => {
288+
window.onDidChangeActiveTextEditor(async () => {
258289
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
259290
}),
260291
envManagers.onDidChangeEnvironment(async () => {

0 commit comments

Comments
 (0)