@@ -20,6 +20,7 @@ import {
2020 resetEnvironmentCommand ,
2121 refreshPackagesCommand ,
2222 createAnyEnvironmentCommand ,
23+ runInDedicatedTerminalCommand ,
2324} from './features/envCommands' ;
2425import { registerCondaFeatures } from './managers/conda/main' ;
2526import { registerSystemPythonFeatures } from './managers/sysPython/main' ;
@@ -37,6 +38,13 @@ import {
3738} from './features/projectCreators' ;
3839import { WorkspaceView } from './features/views/projectView' ;
3940import { registerCompletionProvider } from './features/settings/settingCompletions' ;
41+ import { TerminalManager , TerminalManagerImpl } from './features/terminal/terminalManager' ;
42+ import { activeTerminal , onDidChangeActiveTerminal , onDidChangeActiveTextEditor } from './common/window.apis' ;
43+ import {
44+ getEnvironmentForTerminal ,
45+ setActivateMenuButtonContext ,
46+ updateActivateMenuButtonContext ,
47+ } from './features/terminal/activateMenuButton' ;
4048
4149export async function activate ( context : ExtensionContext ) : Promise < PythonEnvironmentApi > {
4250 // Logging should be set up before anything else.
@@ -46,6 +54,9 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
4654 // Setup the persistent state for the extension.
4755 setPersistentState ( context ) ;
4856
57+ const terminalManager : TerminalManager = new TerminalManagerImpl ( ) ;
58+ context . subscriptions . push ( terminalManager ) ;
59+
4960 const projectManager : PythonProjectManager = new PythonProjectManagerImpl ( ) ;
5061 context . subscriptions . push ( projectManager ) ;
5162
@@ -104,23 +115,25 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
104115 if ( result ) {
105116 const projects : PythonProject [ ] = [ ] ;
106117 result . forEach ( ( r ) => {
107- if ( r . projects ) {
108- projects . push ( r . projects ) ;
118+ if ( r . project ) {
119+ projects . push ( r . project ) ;
109120 }
110121 } ) ;
111122 workspaceView . updateProject ( projects ) ;
123+ await updateActivateMenuButtonContext ( terminalManager , projectManager , envManagers ) ;
112124 }
113125 } ) ,
114126 commands . registerCommand ( 'python-envs.setEnv' , async ( item ) => {
115127 const result = await setEnvironmentCommand ( item , envManagers , projectManager ) ;
116128 if ( result ) {
117129 const projects : PythonProject [ ] = [ ] ;
118130 result . forEach ( ( r ) => {
119- if ( r . projects ) {
120- projects . push ( r . projects ) ;
131+ if ( r . project ) {
132+ projects . push ( r . project ) ;
121133 }
122134 } ) ;
123135 workspaceView . updateProject ( projects ) ;
136+ await updateActivateMenuButtonContext ( terminalManager , projectManager , envManagers ) ;
124137 }
125138 } ) ,
126139 commands . registerCommand ( 'python-envs.reset' , async ( item ) => {
@@ -143,15 +156,44 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
143156 await envManagers . clearCache ( undefined ) ;
144157 } ) ,
145158 commands . registerCommand ( 'python-envs.runInTerminal' , ( item ) => {
146- return runInTerminalCommand ( item , api ) ;
159+ return runInTerminalCommand ( item , api , terminalManager ) ;
160+ } ) ,
161+ commands . registerCommand ( 'python-envs.runInDedicatedTerminal' , ( item ) => {
162+ return runInDedicatedTerminalCommand ( item , api , terminalManager ) ;
147163 } ) ,
148164 commands . registerCommand ( 'python-envs.runAsTask' , ( item ) => {
149165 return runAsTaskCommand ( item , api ) ;
150166 } ) ,
151167 commands . registerCommand ( 'python-envs.createTerminal' , ( item ) => {
152- return createTerminalCommand ( item , api ) ;
168+ return createTerminalCommand ( item , api , terminalManager ) ;
169+ } ) ,
170+ commands . registerCommand ( 'python-envs.terminal.activate' , async ( ) => {
171+ const terminal = activeTerminal ( ) ;
172+ if ( terminal ) {
173+ const env = await getEnvironmentForTerminal ( terminalManager , projectManager , envManagers , terminal ) ;
174+ if ( env ) {
175+ await terminalManager . activate ( terminal , env ) ;
176+ await setActivateMenuButtonContext ( terminalManager , terminal , env ) ;
177+ }
178+ }
179+ } ) ,
180+ commands . registerCommand ( 'python-envs.terminal.deactivate' , async ( ) => {
181+ const terminal = activeTerminal ( ) ;
182+ if ( terminal ) {
183+ await terminalManager . deactivate ( terminal ) ;
184+ const env = await getEnvironmentForTerminal ( terminalManager , projectManager , envManagers , terminal ) ;
185+ if ( env ) {
186+ await setActivateMenuButtonContext ( terminalManager , terminal , env ) ;
187+ }
188+ }
189+ } ) ,
190+ envManagers . onDidChangeEnvironmentManager ( async ( ) => {
191+ await updateActivateMenuButtonContext ( terminalManager , projectManager , envManagers ) ;
192+ } ) ,
193+ onDidChangeActiveTerminal ( async ( t ) => {
194+ await updateActivateMenuButtonContext ( terminalManager , projectManager , envManagers , t ) ;
153195 } ) ,
154- window . onDidChangeActiveTextEditor ( async ( e : TextEditor | undefined ) => {
196+ onDidChangeActiveTextEditor ( async ( e : TextEditor | undefined ) => {
155197 if ( e && ! e . document . isUntitled && e . document . uri . scheme === 'file' ) {
156198 if (
157199 e . document . languageId === 'python' ||
0 commit comments