Skip to content

Commit

Permalink
Unify Terminal REPL triggers (#23641)
Browse files Browse the repository at this point in the history
Resolves: microsoft#22242

Attempting to keep instance of REPL. Did not end up using shell
integration for this, but shell integration and exit code will come in
handy when we have to keep track of 'opened' state of REPL instance for
cases when user wants to enter 'exit()'
  • Loading branch information
anthonykim1 authored and DonJayamanne committed Jun 24, 2024
1 parent 42b8eac commit 10f9613
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class TerminalService implements ITerminalService, Disposable {
if (!this.options?.hideFromUser) {
this.terminal!.show(true);
}

this.terminal!.sendText(text, true);
}
public async sendText(text: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/client/providers/replProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ReplProvider implements Disposable {
.then(noop, noop);
return;
}
const replProvider = this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService, 'repl');
const replProvider = this.serviceContainer.get<ICodeExecutionService>(ICodeExecutionService, 'standard');
await replProvider.initializeRepl(resource);
}
}
7 changes: 6 additions & 1 deletion src/client/terminals/codeExecution/terminalCodeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import { IInterpreterService } from '../../interpreter/contracts';
import { traceInfo } from '../../logging';
import { buildPythonExecInfo, PythonExecInfo } from '../../pythonEnvironments/exec';
import { ICodeExecutionService } from '../../terminals/types';

@injectable()
export class TerminalCodeExecutionProvider implements ICodeExecutionService {
private hasRanOutsideCurrentDrive = false;
protected terminalTitle!: string;
private replActive?: Promise<boolean>;

constructor(
@inject(ITerminalServiceFactory) protected readonly terminalServiceFactory: ITerminalServiceFactory,
@inject(IConfigurationService) protected readonly configurationService: IConfigurationService,
Expand Down Expand Up @@ -58,12 +60,14 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
await this.getTerminalService(resource).sendText(code);
}
}

public async initializeRepl(resource: Resource) {
const terminalService = this.getTerminalService(resource);
if (this.replActive && (await this.replActive)) {
await terminalService.show();
return;
}

this.replActive = new Promise<boolean>(async (resolve) => {
const replCommandArgs = await this.getExecutableInfo(resource);
let listener: IDisposable;
Expand Down Expand Up @@ -93,7 +97,8 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
}
resolve(true);
});
terminalService.sendCommand(replCommandArgs.command, replCommandArgs.args);

await terminalService.sendCommand(replCommandArgs.command, replCommandArgs.args);
});
this.disposables.push(
terminalService.onDidCloseTerminal(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/test/providers/repl.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ suite('REPL Provider', () => {
serviceContainer.setup((c) => c.get(ICommandManager)).returns(() => commandManager.object);
serviceContainer.setup((c) => c.get(IWorkspaceService)).returns(() => workspace.object);
serviceContainer
.setup((c) => c.get(ICodeExecutionService, TypeMoq.It.isValue('repl')))
.setup((s) => s.get(TypeMoq.It.isValue(ICodeExecutionService), TypeMoq.It.isValue('standard')))
.returns(() => codeExecutionService.object);
serviceContainer.setup((c) => c.get(IDocumentManager)).returns(() => documentManager.object);
serviceContainer.setup((c) => c.get(IActiveResourceService)).returns(() => activeResourceService.object);
Expand Down Expand Up @@ -80,6 +80,7 @@ suite('REPL Provider', () => {
const resource = Uri.parse('a');
const disposable = TypeMoq.Mock.ofType<Disposable>();
let commandHandler: undefined | (() => Promise<void>);

commandManager
.setup((c) =>
c.registerCommand(TypeMoq.It.isValue(Commands.Start_REPL), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
Expand All @@ -98,7 +99,7 @@ suite('REPL Provider', () => {
await commandHandler!.call(replProvider);

serviceContainer.verify(
(c) => c.get(TypeMoq.It.isValue(ICodeExecutionService), TypeMoq.It.isValue('repl')),
(c) => c.get(TypeMoq.It.isValue(ICodeExecutionService), TypeMoq.It.isValue('standard')),
TypeMoq.Times.once(),
);
codeExecutionService.verify((c) => c.initializeRepl(TypeMoq.It.isValue(resource)), TypeMoq.Times.once());
Expand Down

0 comments on commit 10f9613

Please sign in to comment.