From dd06e98376ff05ef4484b282ecd2bceb6717941b Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Wed, 20 Mar 2024 18:39:42 -0700 Subject: [PATCH] Fix start and end events --- .../common/capabilities/capabilities.ts | 2 +- .../commandDetectionCapability.ts | 10 ++++----- .../mainThreadTerminalShellIntegration.ts | 3 ++- .../common/extHostTerminalShellIntegration.ts | 21 ++++++++++++------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/vs/platform/terminal/common/capabilities/capabilities.ts b/src/vs/platform/terminal/common/capabilities/capabilities.ts index debdaf7a926..8c6575f872e 100644 --- a/src/vs/platform/terminal/common/capabilities/capabilities.ts +++ b/src/vs/platform/terminal/common/capabilities/capabilities.ts @@ -175,7 +175,7 @@ export interface ICommandDetectionCapability { readonly currentCommand: ICurrentPartialCommand | undefined; readonly onCommandStarted: Event; readonly onCommandFinished: Event; - readonly onCommandExecuted: Event; + readonly onCommandExecuted: Event; readonly onCommandInvalidated: Event; readonly onCurrentCommandInvalidated: Event; setCwd(value: string): void; diff --git a/src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts b/src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts index 685bae340bb..0d4309f98dd 100644 --- a/src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts +++ b/src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts @@ -74,7 +74,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe readonly onBeforeCommandFinished = this._onBeforeCommandFinished.event; private readonly _onCommandFinished = this._register(new Emitter()); readonly onCommandFinished = this._onCommandFinished.event; - private readonly _onCommandExecuted = this._register(new Emitter()); + private readonly _onCommandExecuted = this._register(new Emitter()); readonly onCommandExecuted = this._onCommandExecuted.event; private readonly _onCommandInvalidated = this._register(new Emitter()); readonly onCommandInvalidated = this._onCommandInvalidated.event; @@ -408,7 +408,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe interface ICommandDetectionHeuristicsHooks { readonly onCurrentCommandInvalidatedEmitter: Emitter; readonly onCommandStartedEmitter: Emitter; - readonly onCommandExecutedEmitter: Emitter; + readonly onCommandExecutedEmitter: Emitter; readonly dimensions: ITerminalDimensions; readonly isCommandStorageDisabled: boolean; @@ -495,7 +495,7 @@ class UnixPtyHeuristics extends Disposable { if (y === commandExecutedLine) { currentCommand.command += this._terminal.buffer.active.getLine(commandExecutedLine)?.translateToString(true, undefined, currentCommand.commandExecutedX) || ''; } - this._hooks.onCommandExecutedEmitter.fire(); + this._hooks.onCommandExecutedEmitter.fire(currentCommand as ITerminalCommand); } } @@ -733,7 +733,7 @@ class WindowsPtyHeuristics extends Disposable { this._onCursorMoveListener.clear(); this._evaluateCommandMarkers(); this._capability.currentCommand.commandExecutedX = this._terminal.buffer.active.cursorX; - this._hooks.onCommandExecutedEmitter.fire(); + this._hooks.onCommandExecutedEmitter.fire(this._capability.currentCommand as ITerminalCommand); this._logService.debug('CommandDetectionCapability#handleCommandExecuted', this._capability.currentCommand.commandExecutedX, this._capability.currentCommand.commandExecutedMarker?.line); } @@ -827,7 +827,7 @@ class WindowsPtyHeuristics extends Disposable { } this._capability.currentCommand.commandExecutedMarker = this._hooks.commandMarkers[this._hooks.commandMarkers.length - 1]; // Fire this now to prevent issues like #197409 - this._hooks.onCommandExecutedEmitter.fire(); + this._hooks.onCommandExecutedEmitter.fire(this._capability.currentCommand as ITerminalCommand); } private _cursorOnNextLine(): boolean { diff --git a/src/vs/workbench/api/browser/mainThreadTerminalShellIntegration.ts b/src/vs/workbench/api/browser/mainThreadTerminalShellIntegration.ts index fe906b55840..dee2502b476 100644 --- a/src/vs/workbench/api/browser/mainThreadTerminalShellIntegration.ts +++ b/src/vs/workbench/api/browser/mainThreadTerminalShellIntegration.ts @@ -34,7 +34,7 @@ export class MainThreadTerminalShellIntegration extends Disposable implements Ma // onDidStartTerminalShellExecution const commandDetectionStartEvent = this._store.add( - this._terminalService.createOnInstanceCapabilityEvent(TerminalCapability.CommandDetection, e => e.onCommandStarted) + this._terminalService.createOnInstanceCapabilityEvent(TerminalCapability.CommandDetection, e => e.onCommandExecuted) ); this._store.add(commandDetectionStartEvent.event(e => { const command = e.data; @@ -46,6 +46,7 @@ export class MainThreadTerminalShellIntegration extends Disposable implements Ma this._terminalService.createOnInstanceCapabilityEvent(TerminalCapability.CommandDetection, e => e.onCommandFinished) ); this._store.add(commandDetectionEndEvent.event(e => { + console.log('$acceptTerminalShellExecutionEnd'); this._proxy.$acceptTerminalShellExecutionEnd(e.instance.instanceId, e.data.exitCode); })); diff --git a/src/vs/workbench/api/common/extHostTerminalShellIntegration.ts b/src/vs/workbench/api/common/extHostTerminalShellIntegration.ts index b027e994bd1..e384c387ea1 100644 --- a/src/vs/workbench/api/common/extHostTerminalShellIntegration.ts +++ b/src/vs/workbench/api/common/extHostTerminalShellIntegration.ts @@ -45,6 +45,8 @@ export class ExtHostTerminalShellIntegration extends Disposable implements IExtH this._proxy = extHostRpc.getProxy(MainContext.MainThreadTerminalShellIntegration); + // TODO: Dispose shell integration when terminal is disposed + // TODO: Remove test code this.onDidChangeTerminalShellIntegration(e => { console.log('*** onDidChangeTerminalShellIntegration', e); @@ -65,6 +67,7 @@ export class ExtHostTerminalShellIntegration extends Disposable implements IExtH this.onDidEndTerminalShellExecution(e => { console.log('*** onDidEndTerminalShellExecution', e); }); + setTimeout(() => { Array.from(this._activeShellIntegrations.values())[0].value.executeCommand('echo hello'); }, 4000); @@ -98,7 +101,7 @@ export class ExtHostTerminalShellIntegration extends Disposable implements IExtH } public $acceptTerminalShellExecutionEnd(id: number, exitCode: number | undefined): void { - this._activeShellIntegrations.get(id)?.currentExecution?.endExecution(exitCode); + this._activeShellIntegrations.get(id)?.endShellExecution(exitCode); } public $acceptTerminalShellExecutionData(id: number, data: string): void { @@ -106,7 +109,7 @@ export class ExtHostTerminalShellIntegration extends Disposable implements IExtH } } -class InternalTerminalShellIntegration { +class InternalTerminalShellIntegration extends Disposable { private _currentExecution: InternalTerminalShellExecution | undefined; get currentExecution(): InternalTerminalShellExecution | undefined { return this._currentExecution; } @@ -114,16 +117,17 @@ class InternalTerminalShellIntegration { readonly value: vscode.TerminalShellIntegration; - protected readonly _onDidRequestShellExecution = new Emitter(); + protected readonly _onDidRequestShellExecution = this._register(new Emitter()); readonly onDidRequestShellExecution = this._onDidRequestShellExecution.event; - protected readonly _onDidRequestEndExecution = new Emitter(); + protected readonly _onDidRequestEndExecution = this._register(new Emitter()); readonly onDidRequestEndExecution = this._onDidRequestEndExecution.event; constructor( private readonly _terminal: vscode.Terminal, private readonly _onDidStartTerminalShellExecution: Emitter ) { - // TODO: impl + super(); + const that = this; this.value = { // TODO: Fill in cwd @@ -158,8 +162,11 @@ class InternalTerminalShellIntegration { } endShellExecution(exitCode: number | undefined): void { - this._currentExecution?.endExecution(exitCode); - this._currentExecution = undefined; + if (this._currentExecution) { + this._currentExecution.endExecution(exitCode); + this._onDidRequestEndExecution.fire(this._currentExecution); + this._currentExecution = undefined; + } } }