Skip to content

Commit

Permalink
Set focus to the terminal upon creation of a terminal using the Creat…
Browse files Browse the repository at this point in the history
…e Terminal command (#1433)

Fixes #1315
  • Loading branch information
DonJayamanne authored Apr 20, 2018
1 parent 208e603 commit 1465bda
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions news/1 Enhancements/1315.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set focus to the terminal upon creation of a terminal using the `Python: Create Terminal` command.
16 changes: 8 additions & 8 deletions src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { ITerminalHelper, ITerminalService, TerminalShellType } from './types';
@injectable()
export class TerminalService implements ITerminalService, Disposable {
private terminal?: Terminal;
private terminalShellType: TerminalShellType;
private terminalShellType!: TerminalShellType;
private terminalClosed = new EventEmitter<void>();
private terminalManager: ITerminalManager;
private terminalHelper: ITerminalHelper;
public get onDidCloseTerminal(): Event<void> {
return this.terminalClosed.event;
}
constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer,
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer,
private resource?: Uri,
private title: string = 'Python') {

Expand All @@ -44,11 +44,11 @@ export class TerminalService implements ITerminalService, Disposable {
this.terminal!.show(true);
this.terminal!.sendText(text);
}
public async show(): Promise<void> {
await this.ensureTerminal();
this.terminal!.show(true);
public async show(preserveFocus: boolean = true): Promise<void> {
await this.ensureTerminal(preserveFocus);
this.terminal!.show(preserveFocus);
}
private async ensureTerminal(): Promise<void> {
private async ensureTerminal(preserveFocus: boolean = true): Promise<void> {
if (this.terminal) {
return;
}
Expand All @@ -62,7 +62,7 @@ export class TerminalService implements ITerminalService, Disposable {
const activationCommamnds = await this.terminalHelper.getEnvironmentActivationCommands(this.terminalShellType, this.resource);
if (activationCommamnds) {
for (const command of activationCommamnds!) {
this.terminal!.show(true);
this.terminal!.show(preserveFocus);
this.terminal!.sendText(command);

// Give the command some time to complete.
Expand All @@ -71,7 +71,7 @@ export class TerminalService implements ITerminalService, Disposable {
}
}

this.terminal!.show(true);
this.terminal!.show(preserveFocus);
}
private terminalCloseHandler(terminal: Terminal) {
if (terminal === this.terminal) {
Expand Down
3 changes: 1 addition & 2 deletions src/client/common/terminal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT License.

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

export enum TerminalShellType {
powershell = 1,
Expand All @@ -19,7 +18,7 @@ export interface ITerminalService {
readonly onDidCloseTerminal: Event<void>;
sendCommand(command: string, args: string[]): Promise<void>;
sendText(text: string): Promise<void>;
show(): Promise<void>;
show(preserveFocus?: boolean): Promise<void>;
}

export const ITerminalServiceFactory = Symbol('ITerminalServiceFactory');
Expand Down
2 changes: 1 addition & 1 deletion src/client/providers/terminalProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class TerminalProvider implements Disposable {
private async onCreateTerminal() {
const terminalService = this.serviceContainer.get<ITerminalServiceFactory>(ITerminalServiceFactory);
const activeResource = this.getActiveResource();
await terminalService.createTerminalService(activeResource, 'Python').show();
await terminalService.createTerminalService(activeResource, 'Python').show(false);
}
private getActiveResource(): Uri | undefined {
const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
Expand Down
12 changes: 12 additions & 0 deletions src/test/common/terminals/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ suite('Terminal Service', () => {
terminal.verify(t => t.show(TypeMoq.It.isValue(true)), TypeMoq.Times.exactly(2));
});

test('Ensure terminal shown and focus is set to the Terminal', async () => {
terminalHelper.setup(helper => helper.getEnvironmentActivationCommands(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined));
service = new TerminalService(mockServiceContainer.object);
terminalHelper.setup(h => h.getTerminalShellPath()).returns(() => '');
terminalHelper.setup(h => h.identifyTerminalShell(TypeMoq.It.isAny())).returns(() => TerminalShellType.bash);
terminalManager.setup(t => t.createTerminal(TypeMoq.It.isAny())).returns(() => terminal.object);

await service.show(false);

terminal.verify(t => t.show(TypeMoq.It.isValue(false)), TypeMoq.Times.exactly(2));
});

test('Ensure terminal is activated once after creation', async () => {
service = new TerminalService(mockServiceContainer.object);
terminalHelper.setup(h => h.getTerminalShellPath()).returns(() => '');
Expand Down
8 changes: 4 additions & 4 deletions src/test/providers/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ suite('Terminal Provider', () => {
terminalServiceFactory.setup(t => t.createTerminalService(TypeMoq.It.isValue(undefined), TypeMoq.It.isValue('Python'))).returns(() => terminalService.object);

commandHandler!.call(terminalProvider);
terminalService.verify(t => t.show(), TypeMoq.Times.once());
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
});

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

commandHandler!.call(terminalProvider);
terminalService.verify(t => t.show(), TypeMoq.Times.once());
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
});

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

commandHandler!.call(terminalProvider);
terminalService.verify(t => t.show(), TypeMoq.Times.once());
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
});

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

commandHandler!.call(terminalProvider);
terminalService.verify(t => t.show(), TypeMoq.Times.once());
terminalService.verify(t => t.show(false), TypeMoq.Times.once());
});
});

0 comments on commit 1465bda

Please sign in to comment.