From 168b10d3e37a4b8e73f887604165284416cc5dbe Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Sat, 29 Mar 2025 00:07:15 -0700 Subject: [PATCH 1/2] chore: refactor `ensureTerminal` to return a Terminal --- src/client/common/terminal/service.ts | 6 +++--- src/client/common/terminal/types.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/client/common/terminal/service.ts b/src/client/common/terminal/service.ts index e92fbd3d494f..ea2503ba6e3b 100644 --- a/src/client/common/terminal/service.ts +++ b/src/client/common/terminal/service.ts @@ -141,9 +141,9 @@ export class TerminalService implements ITerminalService, Disposable { } } // TODO: Debt switch to Promise ---> breaks 20 tests - public async ensureTerminal(preserveFocus: boolean = true): Promise { + public async ensureTerminal(preserveFocus: boolean = true): Promise { if (this.terminal) { - return; + return this.terminal; } if (useEnvExtension()) { @@ -174,7 +174,7 @@ export class TerminalService implements ITerminalService, Disposable { } this.sendTelemetry().ignoreErrors(); - return; + return this.terminal; } private terminalCloseHandler(terminal: Terminal) { if (terminal === this.terminal) { diff --git a/src/client/common/terminal/types.ts b/src/client/common/terminal/types.ts index 3e54458a57fd..cbc2b6fd5a51 100644 --- a/src/client/common/terminal/types.ts +++ b/src/client/common/terminal/types.ts @@ -56,6 +56,14 @@ export interface ITerminalService extends IDisposable { sendText(text: string): Promise; executeCommand(commandLine: string, isPythonShell: boolean): Promise; show(preserveFocus?: boolean): Promise; + /** + * Ensures that a terminal exists, creating one if necessary. + * + * @param {boolean} [preserveFocus] Whether the editor should keep focus after the terminal is shown. Defaults to `true`. + * @returns {Promise} The terminal instance. + * @memberof ITerminalService + */ + ensureTerminal(preserveFocus?: boolean): Promise; } export const ITerminalServiceFactory = Symbol('ITerminalServiceFactory'); From 9612470bda89408f64a47948c900e11fc1b479ac Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 1 Apr 2025 17:41:18 -0700 Subject: [PATCH 2/2] fix: build failure --- src/client/common/terminal/syncTerminalService.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/client/common/terminal/syncTerminalService.ts b/src/client/common/terminal/syncTerminalService.ts index 0b46a86ee51e..c1e7a655e94d 100644 --- a/src/client/common/terminal/syncTerminalService.ts +++ b/src/client/common/terminal/syncTerminalService.ts @@ -4,7 +4,7 @@ 'use strict'; import { inject } from 'inversify'; -import { CancellationToken, Disposable, Event, TerminalShellExecution } from 'vscode'; +import { CancellationToken, Disposable, Event, Terminal, TerminalShellExecution } from 'vscode'; import { IInterpreterService } from '../../interpreter/contracts'; import { traceVerbose } from '../../logging'; import { PythonEnvironment } from '../../pythonEnvironments/info'; @@ -152,6 +152,17 @@ export class SynchronousTerminalService implements ITerminalService, Disposable return this.terminalService.show(preserveFocus); } + /** + * Ensures that a terminal exists, creating one if necessary. + * + * @param {boolean} [preserveFocus] Whether the editor should keep focus after the terminal is shown. Defaults to `true`. + * @returns {Promise} The terminal instance. + * @memberof SynchronousTerminalService + */ + public async ensureTerminal(preserveFocus: boolean = true): Promise { + return this.terminalService.ensureTerminal(preserveFocus); + } + private createLockFile(): Promise { return this.fs.createTemporaryFile('.log').then((l) => { this.disposables.push(l);