Skip to content

Commit 1465bda

Browse files
authored
Set focus to the terminal upon creation of a terminal using the Create Terminal command (#1433)
Fixes #1315
1 parent 208e603 commit 1465bda

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

news/1 Enhancements/1315.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set focus to the terminal upon creation of a terminal using the `Python: Create Terminal` command.

src/client/common/terminal/service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import { ITerminalHelper, ITerminalService, TerminalShellType } from './types';
1111
@injectable()
1212
export class TerminalService implements ITerminalService, Disposable {
1313
private terminal?: Terminal;
14-
private terminalShellType: TerminalShellType;
14+
private terminalShellType!: TerminalShellType;
1515
private terminalClosed = new EventEmitter<void>();
1616
private terminalManager: ITerminalManager;
1717
private terminalHelper: ITerminalHelper;
1818
public get onDidCloseTerminal(): Event<void> {
1919
return this.terminalClosed.event;
2020
}
21-
constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer,
21+
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer,
2222
private resource?: Uri,
2323
private title: string = 'Python') {
2424

@@ -44,11 +44,11 @@ export class TerminalService implements ITerminalService, Disposable {
4444
this.terminal!.show(true);
4545
this.terminal!.sendText(text);
4646
}
47-
public async show(): Promise<void> {
48-
await this.ensureTerminal();
49-
this.terminal!.show(true);
47+
public async show(preserveFocus: boolean = true): Promise<void> {
48+
await this.ensureTerminal(preserveFocus);
49+
this.terminal!.show(preserveFocus);
5050
}
51-
private async ensureTerminal(): Promise<void> {
51+
private async ensureTerminal(preserveFocus: boolean = true): Promise<void> {
5252
if (this.terminal) {
5353
return;
5454
}
@@ -62,7 +62,7 @@ export class TerminalService implements ITerminalService, Disposable {
6262
const activationCommamnds = await this.terminalHelper.getEnvironmentActivationCommands(this.terminalShellType, this.resource);
6363
if (activationCommamnds) {
6464
for (const command of activationCommamnds!) {
65-
this.terminal!.show(true);
65+
this.terminal!.show(preserveFocus);
6666
this.terminal!.sendText(command);
6767

6868
// Give the command some time to complete.
@@ -71,7 +71,7 @@ export class TerminalService implements ITerminalService, Disposable {
7171
}
7272
}
7373

74-
this.terminal!.show(true);
74+
this.terminal!.show(preserveFocus);
7575
}
7676
private terminalCloseHandler(terminal: Terminal) {
7777
if (terminal === this.terminal) {

src/client/common/terminal/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Licensed under the MIT License.
44

55
import { Event, Terminal, Uri } from 'vscode';
6-
import { PythonInterpreter } from '../../interpreter/contracts';
76

87
export enum TerminalShellType {
98
powershell = 1,
@@ -19,7 +18,7 @@ export interface ITerminalService {
1918
readonly onDidCloseTerminal: Event<void>;
2019
sendCommand(command: string, args: string[]): Promise<void>;
2120
sendText(text: string): Promise<void>;
22-
show(): Promise<void>;
21+
show(preserveFocus?: boolean): Promise<void>;
2322
}
2423

2524
export const ITerminalServiceFactory = Symbol('ITerminalServiceFactory');

src/client/providers/terminalProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class TerminalProvider implements Disposable {
2424
private async onCreateTerminal() {
2525
const terminalService = this.serviceContainer.get<ITerminalServiceFactory>(ITerminalServiceFactory);
2626
const activeResource = this.getActiveResource();
27-
await terminalService.createTerminalService(activeResource, 'Python').show();
27+
await terminalService.createTerminalService(activeResource, 'Python').show(false);
2828
}
2929
private getActiveResource(): Uri | undefined {
3030
const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);

src/test/common/terminals/service.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ suite('Terminal Service', () => {
114114
terminal.verify(t => t.show(TypeMoq.It.isValue(true)), TypeMoq.Times.exactly(2));
115115
});
116116

117+
test('Ensure terminal shown and focus is set to the Terminal', async () => {
118+
terminalHelper.setup(helper => helper.getEnvironmentActivationCommands(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined));
119+
service = new TerminalService(mockServiceContainer.object);
120+
terminalHelper.setup(h => h.getTerminalShellPath()).returns(() => '');
121+
terminalHelper.setup(h => h.identifyTerminalShell(TypeMoq.It.isAny())).returns(() => TerminalShellType.bash);
122+
terminalManager.setup(t => t.createTerminal(TypeMoq.It.isAny())).returns(() => terminal.object);
123+
124+
await service.show(false);
125+
126+
terminal.verify(t => t.show(TypeMoq.It.isValue(false)), TypeMoq.Times.exactly(2));
127+
});
128+
117129
test('Ensure terminal is activated once after creation', async () => {
118130
service = new TerminalService(mockServiceContainer.object);
119131
terminalHelper.setup(h => h.getTerminalShellPath()).returns(() => '');

src/test/providers/terminal.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ suite('Terminal Provider', () => {
6868
terminalServiceFactory.setup(t => t.createTerminalService(TypeMoq.It.isValue(undefined), TypeMoq.It.isValue('Python'))).returns(() => terminalService.object);
6969

7070
commandHandler!.call(terminalProvider);
71-
terminalService.verify(t => t.show(), TypeMoq.Times.once());
71+
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
7272
});
7373

7474
test('Ensure terminal creation does not use uri of the active documents which is untitled', () => {
@@ -94,7 +94,7 @@ suite('Terminal Provider', () => {
9494
terminalServiceFactory.setup(t => t.createTerminalService(TypeMoq.It.isValue(undefined), TypeMoq.It.isValue('Python'))).returns(() => terminalService.object);
9595

9696
commandHandler!.call(terminalProvider);
97-
terminalService.verify(t => t.show(), TypeMoq.Times.once());
97+
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
9898
});
9999

100100
test('Ensure terminal creation uses uri of active document', () => {
@@ -122,7 +122,7 @@ suite('Terminal Provider', () => {
122122
terminalServiceFactory.setup(t => t.createTerminalService(TypeMoq.It.isValue(documentUri), TypeMoq.It.isValue('Python'))).returns(() => terminalService.object);
123123

124124
commandHandler!.call(terminalProvider);
125-
terminalService.verify(t => t.show(), TypeMoq.Times.once());
125+
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
126126
});
127127

128128
test('Ensure terminal creation uses uri of active workspace', () => {
@@ -147,6 +147,6 @@ suite('Terminal Provider', () => {
147147
terminalServiceFactory.setup(t => t.createTerminalService(TypeMoq.It.isValue(workspaceUri), TypeMoq.It.isValue('Python'))).returns(() => terminalService.object);
148148

149149
commandHandler!.call(terminalProvider);
150-
terminalService.verify(t => t.show(), TypeMoq.Times.once());
150+
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
151151
});
152152
});

0 commit comments

Comments
 (0)