From 2044f831514440a0bf708e4f449f67f9cb78db03 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Mon, 11 Nov 2024 11:48:58 -0800 Subject: [PATCH] figure out if we are inside Python REPL --- src/client/common/terminal/service.ts | 22 ++++++++++++++----- .../common/terminal/syncTerminalService.ts | 7 ++++-- src/client/common/terminal/types.ts | 2 +- .../codeExecution/terminalCodeExecution.ts | 2 +- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/client/common/terminal/service.ts b/src/client/common/terminal/service.ts index f20ad8496bdf..6ec147c6da7a 100644 --- a/src/client/common/terminal/service.ts +++ b/src/client/common/terminal/service.ts @@ -21,6 +21,7 @@ import { TerminalShellType, } from './types'; import { traceVerbose } from '../../logging'; +import { getConfiguration } from '../vscodeApis/workspaceApis'; @injectable() export class TerminalService implements ITerminalService, Disposable { @@ -69,7 +70,7 @@ export class TerminalService implements ITerminalService, Disposable { this.terminal!.show(true); } - return this.executeCommand(text); + return this.executeCommand(text, false); } /** @deprecated */ public async sendText(text: string): Promise { @@ -77,9 +78,12 @@ export class TerminalService implements ITerminalService, Disposable { if (!this.options?.hideFromUser) { this.terminal!.show(true); } - this.terminal!.sendText(text); + this.terminal!.sendText(text, false); } - public async executeCommand(commandLine: string): Promise { + public async executeCommand( + commandLine: string, + isPythonShell: boolean, + ): Promise { const terminal = this.terminal!; if (!this.options?.hideFromUser) { terminal.show(true); @@ -102,12 +106,18 @@ export class TerminalService implements ITerminalService, Disposable { }); await promise; } - // python in a shell , exit code is undefined . startCommand event happen, we call end command event - if (terminal.shellIntegration) { + // If shell integration for python is disabled, use sendText inside REPL regardless of upstream shell integration setting. + const config = getConfiguration('python'); + const pythonrcSetting = config.get('terminal.shellIntegration.enabled'); + if (isPythonShell && !pythonrcSetting) { + terminal.sendText(commandLine); + return undefined; + } else if (terminal.shellIntegration) { + // python in a shell , exit code is undefined . startCommand event happen, we call end command event // TODO: Await the python REPL execute promise here. So we know python repl launched for sure before executing other python code. // So we would not be interrupted. - await this.serviceContainer.get(ICodeExecutionService).replActive; + // await this.serviceContainer.get(ICodeExecutionService).replActive; getting undefined const execution = terminal.shellIntegration.executeCommand(commandLine); traceVerbose(`Shell Integration is enabled, executeCommand: ${commandLine}`); diff --git a/src/client/common/terminal/syncTerminalService.ts b/src/client/common/terminal/syncTerminalService.ts index 265635cf18b6..6a74d8ae674d 100644 --- a/src/client/common/terminal/syncTerminalService.ts +++ b/src/client/common/terminal/syncTerminalService.ts @@ -145,8 +145,11 @@ export class SynchronousTerminalService implements ITerminalService, Disposable public sendText(text: string): Promise { return this.terminalService.sendText(text); } - public async executeCommand(commandLine: string): Promise { - return this.terminalService.executeCommand(commandLine); + public async executeCommand( + commandLine: string, + isPythonShell: boolean, + ): Promise { + return this.terminalService.executeCommand(commandLine, isPythonShell); } public show(preserveFocus?: boolean | undefined): Promise { return this.terminalService.show(preserveFocus); diff --git a/src/client/common/terminal/types.ts b/src/client/common/terminal/types.ts index 0a85e3f38545..b782a3b6c79f 100644 --- a/src/client/common/terminal/types.ts +++ b/src/client/common/terminal/types.ts @@ -54,7 +54,7 @@ export interface ITerminalService extends IDisposable { ): Promise; /** @deprecated */ sendText(text: string): Promise; - executeCommand(commandLine: string): Promise; + executeCommand(commandLine: string, isPythonShell: boolean): Promise; show(preserveFocus?: boolean): Promise; } diff --git a/src/client/terminals/codeExecution/terminalCodeExecution.ts b/src/client/terminals/codeExecution/terminalCodeExecution.ts index 09c9669e52a6..55a90b701cc6 100644 --- a/src/client/terminals/codeExecution/terminalCodeExecution.ts +++ b/src/client/terminals/codeExecution/terminalCodeExecution.ts @@ -59,7 +59,7 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService { this.configurationService.updateSetting('REPL.enableREPLSmartSend', false, resource); } } else { - await this.getTerminalService(resource).executeCommand(code); + await this.getTerminalService(resource).executeCommand(code, true); } }