1- import { commands , ExtensionContext , LogOutputChannel , Terminal , Uri } from 'vscode' ;
1+ import { commands , ExtensionContext , LogOutputChannel , Terminal , Uri , workspace , window } from 'vscode' ;
22import { PythonEnvironment , PythonEnvironmentApi } from './api' ;
33import { ensureCorrectVersion } from './common/extVersion' ;
44import { 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' ;
1918import { 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