22// Licensed under the MIT License.
33
44import { inject , injectable } from 'inversify' ;
5- import { CancellationToken , Disposable , Event , EventEmitter , Terminal } from 'vscode' ;
5+ import { CancellationToken , Disposable , Event , EventEmitter , Terminal , TerminalShellExecution } from 'vscode' ;
66import '../../common/extensions' ;
77import { IInterpreterService } from '../../interpreter/contracts' ;
88import { IServiceContainer } from '../../ioc/types' ;
@@ -18,7 +18,6 @@ import {
1818 ITerminalService ,
1919 TerminalCreationOptions ,
2020 TerminalShellType ,
21- ITerminalExecutedCommand ,
2221} from './types' ;
2322import { traceVerbose } from '../../logging' ;
2423
@@ -35,6 +34,8 @@ export class TerminalService implements ITerminalService, Disposable {
3534 public get onDidCloseTerminal ( ) : Event < void > {
3635 return this . terminalClosed . event . bind ( this . terminalClosed ) ;
3736 }
37+
38+ // private _shellIntegrationEnabled
3839 constructor (
3940 @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ,
4041 private readonly options ?: TerminalCreationOptions ,
@@ -73,7 +74,7 @@ export class TerminalService implements ITerminalService, Disposable {
7374 }
7475 this . terminal ! . sendText ( text ) ;
7576 }
76- public async executeCommand ( commandLine : string ) : Promise < ITerminalExecutedCommand | undefined > {
77+ public async executeCommand ( commandLine : string ) : Promise < TerminalShellExecution | undefined > {
7778 const terminal = this . terminal ! ;
7879 if ( ! this . options ?. hideFromUser ) {
7980 terminal . show ( true ) ;
@@ -82,15 +83,14 @@ export class TerminalService implements ITerminalService, Disposable {
8283 // If terminal was just launched, wait some time for shell integration to onDidChangeShellIntegration.
8384 if ( ! terminal . shellIntegration ) {
8485 const promise = new Promise < boolean > ( ( resolve ) => {
85- const shellIntegrationChangeEventListener = this . terminalManager . onDidChangeTerminalShellIntegration (
86- ( ) => {
87- this . executeCommandListeners . delete ( shellIntegrationChangeEventListener ) ;
88- resolve ( true ) ;
89- } ,
90- ) ;
86+ const disposable = this . terminalManager . onDidChangeTerminalShellIntegration ( ( ) => {
87+ clearTimeout ( timer ) ; //racetimeout
88+ disposable . dispose ( ) ;
89+ resolve ( true ) ;
90+ } ) ;
9191 const TIMEOUT_DURATION = 500 ;
92- setTimeout ( ( ) => {
93- this . executeCommandListeners . add ( shellIntegrationChangeEventListener ) ;
92+ const timer = setTimeout ( ( ) => {
93+ disposable . dispose ( ) ;
9494 resolve ( true ) ;
9595 } , TIMEOUT_DURATION ) ;
9696 } ) ;
@@ -101,28 +101,7 @@ export class TerminalService implements ITerminalService, Disposable {
101101 // TODO: executeCommand would not execute command manually typed inside Python Terminal REPL.
102102 // We only run executeCommand when user shift+enter in .py file, and hence run command in terminal on user's behalf.
103103 const execution = terminal . shellIntegration . executeCommand ( commandLine ) ;
104- traceVerbose ( `Shell Integration is enabled, executeCommand: ${ commandLine } ` ) ;
105- // exitCode as promise for the case:
106- // In the case where SI is enabled in zsh/pwsh in Windows but not inside Python REPL so Python command won't finish until user exit()
107- // This means OnDidEndTerminalShellExecution would not fire inside REPL launched once REPL is launched for above case.
108-
109- return {
110- execution,
111- exitCode : new Promise ( ( resolve ) => {
112- const listener = this . terminalManager . onDidEndTerminalShellExecution ( ( e ) => {
113- if ( e . execution === execution ) {
114- this . executeCommandListeners . delete ( listener ) ;
115- resolve ( e . exitCode ) ;
116- traceVerbose (
117- `onDidEndTerminalShellExecution handler is called: Shell Integration exitCode: ${ e . exitCode } ` ,
118- ) ;
119- }
120- } ) ;
121- if ( listener ) {
122- this . executeCommandListeners . add ( listener ) ;
123- }
124- } ) ,
125- } ;
104+ return execution ;
126105 } else {
127106 terminal . sendText ( commandLine ) ;
128107 traceVerbose ( `Shell Integration is disabled, sendText: ${ commandLine } ` ) ;
0 commit comments