Skip to content

Commit e7803be

Browse files
committed
Revert "Open separate Python terminals when running different Python files (microsoft#21202)"
This reverts commit 17daae4.
1 parent 6601c8d commit e7803be

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

src/client/common/terminal/factory.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { inject, injectable } from 'inversify';
55
import { Uri } from 'vscode';
6-
import * as path from 'path';
76
import { IInterpreterService } from '../../interpreter/contracts';
87
import { IServiceContainer } from '../../ioc/types';
98
import { PythonEnvironment } from '../../pythonEnvironments/info';
@@ -27,14 +26,10 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
2726
public getTerminalService(options: TerminalCreationOptions): ITerminalService {
2827
const resource = options?.resource;
2928
const title = options?.title;
30-
let terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
29+
const terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
3130
const interpreter = options?.interpreter;
3231
const id = this.getTerminalId(terminalTitle, resource, interpreter);
3332
if (!this.terminalServices.has(id)) {
34-
if (this.terminalServices.size >= 1 && resource) {
35-
terminalTitle = `${terminalTitle}: ${path.basename(resource.fsPath).replace('.py', '')}`;
36-
}
37-
options.title = terminalTitle;
3833
const terminalService = new TerminalService(this.serviceContainer, options);
3934
this.terminalServices.set(id, terminalService);
4035
}
@@ -58,6 +53,6 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
5853
const workspaceFolder = this.serviceContainer
5954
.get<IWorkspaceService>(IWorkspaceService)
6055
.getWorkspaceFolder(resource || undefined);
61-
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}:${resource?.fsPath || ''}`;
56+
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}`;
6257
}
6358
}

src/client/terminals/codeExecution/terminalCodeExecution.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { ICodeExecutionService } from '../../terminals/types';
1919
export class TerminalCodeExecutionProvider implements ICodeExecutionService {
2020
private hasRanOutsideCurrentDrive = false;
2121
protected terminalTitle!: string;
22-
private replActive = new Map<string, Promise<boolean>>();
22+
private _terminalService!: ITerminalService;
23+
private replActive?: Promise<boolean>;
2324
constructor(
2425
@inject(ITerminalServiceFactory) protected readonly terminalServiceFactory: ITerminalServiceFactory,
2526
@inject(IConfigurationService) protected readonly configurationService: IConfigurationService,
@@ -47,27 +48,19 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
4748
await this.getTerminalService(resource).sendText(code);
4849
}
4950
public async initializeRepl(resource?: Uri) {
50-
const terminalService = this.getTerminalService(resource);
51-
let replActive = this.replActive.get(resource?.fsPath || '');
52-
if (replActive && (await replActive)) {
53-
await terminalService.show();
51+
if (this.replActive && (await this.replActive)) {
52+
await this._terminalService.show();
5453
return;
5554
}
56-
replActive = new Promise<boolean>(async (resolve) => {
55+
this.replActive = new Promise<boolean>(async (resolve) => {
5756
const replCommandArgs = await this.getExecutableInfo(resource);
58-
terminalService.sendCommand(replCommandArgs.command, replCommandArgs.args);
57+
await this.getTerminalService(resource).sendCommand(replCommandArgs.command, replCommandArgs.args);
5958

6059
// Give python repl time to start before we start sending text.
6160
setTimeout(() => resolve(true), 1000);
6261
});
63-
this.replActive.set(resource?.fsPath || '', replActive);
64-
this.disposables.push(
65-
terminalService.onDidCloseTerminal(() => {
66-
this.replActive.delete(resource?.fsPath || '');
67-
}),
68-
);
6962

70-
await replActive;
63+
await this.replActive;
7164
}
7265

7366
public async getExecutableInfo(resource?: Uri, args: string[] = []): Promise<PythonExecInfo> {
@@ -84,10 +77,18 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
8477
return this.getExecutableInfo(resource, executeArgs);
8578
}
8679
private getTerminalService(resource?: Uri): ITerminalService {
87-
return this.terminalServiceFactory.getTerminalService({
88-
resource,
89-
title: this.terminalTitle,
90-
});
80+
if (!this._terminalService) {
81+
this._terminalService = this.terminalServiceFactory.getTerminalService({
82+
resource,
83+
title: this.terminalTitle,
84+
});
85+
this.disposables.push(
86+
this._terminalService.onDidCloseTerminal(() => {
87+
this.replActive = undefined;
88+
}),
89+
);
90+
}
91+
return this._terminalService;
9192
}
9293
private async setCwdForFileExecution(file: Uri) {
9394
const pythonSettings = this.configurationService.getSettings(file);

src/test/common/terminals/factory.unit.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ suite('Terminal Service Factory', () => {
105105
expect(notSameAsThirdInstance).to.not.equal(true, 'Instances are the same');
106106
});
107107

108-
test('Ensure different terminal is returned when using different resources from the same workspace', () => {
108+
test('Ensure same terminal is returned when using resources from the same workspace', () => {
109109
const file1A = Uri.file('1a');
110110
const file2A = Uri.file('2a');
111111
const fileB = Uri.file('b');
@@ -131,7 +131,7 @@ suite('Terminal Service Factory', () => {
131131
const terminalForFileB = factory.getTerminalService({ resource: fileB }) as SynchronousTerminalService;
132132

133133
const terminalsAreSameForWorkspaceA = terminalForFile1A.terminalService === terminalForFile2A.terminalService;
134-
expect(terminalsAreSameForWorkspaceA).to.equal(false, 'Instances are the same for Workspace A');
134+
expect(terminalsAreSameForWorkspaceA).to.equal(true, 'Instances are not the same for Workspace A');
135135

136136
const terminalsForWorkspaceABAreDifferent =
137137
terminalForFile1A.terminalService === terminalForFileB.terminalService;

0 commit comments

Comments
 (0)