Skip to content

Commit

Permalink
figure out if we are inside Python REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykim1 committed Nov 11, 2024
1 parent c7a1504 commit 2044f83
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
TerminalShellType,
} from './types';
import { traceVerbose } from '../../logging';
import { getConfiguration } from '../vscodeApis/workspaceApis';

@injectable()
export class TerminalService implements ITerminalService, Disposable {
Expand Down Expand Up @@ -69,17 +70,20 @@ 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<void> {
await this.ensureTerminal();
if (!this.options?.hideFromUser) {
this.terminal!.show(true);
}
this.terminal!.sendText(text);
this.terminal!.sendText(text, false);
}
public async executeCommand(commandLine: string): Promise<IExecuteCommandResult | undefined> {
public async executeCommand(
commandLine: string,
isPythonShell: boolean,
): Promise<IExecuteCommandResult | undefined> {
const terminal = this.terminal!;
if (!this.options?.hideFromUser) {
terminal.show(true);
Expand All @@ -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<boolean>('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>(ICodeExecutionService).replActive;
// await this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService).replActive; getting undefined

const execution = terminal.shellIntegration.executeCommand(commandLine);
traceVerbose(`Shell Integration is enabled, executeCommand: ${commandLine}`);
Expand Down
7 changes: 5 additions & 2 deletions src/client/common/terminal/syncTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ export class SynchronousTerminalService implements ITerminalService, Disposable
public sendText(text: string): Promise<void> {
return this.terminalService.sendText(text);
}
public async executeCommand(commandLine: string): Promise<IExecuteCommandResult | undefined> {
return this.terminalService.executeCommand(commandLine);
public async executeCommand(
commandLine: string,
isPythonShell: boolean,
): Promise<IExecuteCommandResult | undefined> {
return this.terminalService.executeCommand(commandLine, isPythonShell);
}
public show(preserveFocus?: boolean | undefined): Promise<void> {
return this.terminalService.show(preserveFocus);
Expand Down
2 changes: 1 addition & 1 deletion src/client/common/terminal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface ITerminalService extends IDisposable {
): Promise<IExecuteCommandResult | undefined>;
/** @deprecated */
sendText(text: string): Promise<void>;
executeCommand(commandLine: string): Promise<IExecuteCommandResult | undefined>;
executeCommand(commandLine: string, isPythonShell: boolean): Promise<IExecuteCommandResult | undefined>;
show(preserveFocus?: boolean): Promise<void>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 2044f83

Please sign in to comment.