From 1465bda342c4d1ed15f709f3e555257e70f524fd Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 20 Apr 2018 16:01:34 -0700 Subject: [PATCH] Set focus to the terminal upon creation of a terminal using the Create Terminal command (#1433) Fixes #1315 --- news/1 Enhancements/1315.md | 1 + src/client/common/terminal/service.ts | 16 ++++++++-------- src/client/common/terminal/types.ts | 3 +-- src/client/providers/terminalProvider.ts | 2 +- src/test/common/terminals/service.test.ts | 12 ++++++++++++ src/test/providers/terminal.test.ts | 8 ++++---- 6 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 news/1 Enhancements/1315.md diff --git a/news/1 Enhancements/1315.md b/news/1 Enhancements/1315.md new file mode 100644 index 000000000000..ff67ed87e629 --- /dev/null +++ b/news/1 Enhancements/1315.md @@ -0,0 +1 @@ +Set focus to the terminal upon creation of a terminal using the `Python: Create Terminal` command. diff --git a/src/client/common/terminal/service.ts b/src/client/common/terminal/service.ts index 5ec43970a75a..7e54a853b1dc 100644 --- a/src/client/common/terminal/service.ts +++ b/src/client/common/terminal/service.ts @@ -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(); private terminalManager: ITerminalManager; private terminalHelper: ITerminalHelper; public get onDidCloseTerminal(): Event { return this.terminalClosed.event; } - constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer, + constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer, private resource?: Uri, private title: string = 'Python') { @@ -44,11 +44,11 @@ export class TerminalService implements ITerminalService, Disposable { this.terminal!.show(true); this.terminal!.sendText(text); } - public async show(): Promise { - await this.ensureTerminal(); - this.terminal!.show(true); + public async show(preserveFocus: boolean = true): Promise { + await this.ensureTerminal(preserveFocus); + this.terminal!.show(preserveFocus); } - private async ensureTerminal(): Promise { + private async ensureTerminal(preserveFocus: boolean = true): Promise { if (this.terminal) { return; } @@ -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. @@ -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) { diff --git a/src/client/common/terminal/types.ts b/src/client/common/terminal/types.ts index 5a2914fd2ab0..56cdb305c077 100644 --- a/src/client/common/terminal/types.ts +++ b/src/client/common/terminal/types.ts @@ -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, @@ -19,7 +18,7 @@ export interface ITerminalService { readonly onDidCloseTerminal: Event; sendCommand(command: string, args: string[]): Promise; sendText(text: string): Promise; - show(): Promise; + show(preserveFocus?: boolean): Promise; } export const ITerminalServiceFactory = Symbol('ITerminalServiceFactory'); diff --git a/src/client/providers/terminalProvider.ts b/src/client/providers/terminalProvider.ts index 7bfb5bb4d08f..2d84986ebb07 100644 --- a/src/client/providers/terminalProvider.ts +++ b/src/client/providers/terminalProvider.ts @@ -24,7 +24,7 @@ export class TerminalProvider implements Disposable { private async onCreateTerminal() { const terminalService = this.serviceContainer.get(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); diff --git a/src/test/common/terminals/service.test.ts b/src/test/common/terminals/service.test.ts index f18b3738b7af..1f71218b8094 100644 --- a/src/test/common/terminals/service.test.ts +++ b/src/test/common/terminals/service.test.ts @@ -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(() => ''); diff --git a/src/test/providers/terminal.test.ts b/src/test/providers/terminal.test.ts index 7b9097120d7e..6573ef9d231a 100644 --- a/src/test/providers/terminal.test.ts +++ b/src/test/providers/terminal.test.ts @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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()); }); });