From 27d5c0e2d7a1e877e094d9a6fc65c5071b797636 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 27 Jun 2016 16:22:42 -0700 Subject: [PATCH] Prevent multiple terminals from being created via createNew Fixes an exception was being caused because the panel was not initialized when calling into createNew. This also fixes multiple terminal instances being spawned if the panel WAS initialized but contained 0 processes. Fixes #8209 --- .../electron-browser/terminalService.ts | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts index 9183945670d..589fee73f77 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalService.ts @@ -58,7 +58,7 @@ export class TerminalService implements ITerminalService { public setActiveTerminal(index: number): TPromise { return this.focus().then(() => { - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + return this.showAndGetTerminalPanel().then((terminalPanel) => { this.activeTerminalIndex = index; terminalPanel.setActiveTerminal(this.activeTerminalIndex); terminalPanel.focus(); @@ -73,7 +73,7 @@ export class TerminalService implements ITerminalService { public focusNext(): TPromise { return this.focus().then(() => { - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + return this.showAndGetTerminalPanel().then((terminalPanel) => { if (this.terminalProcesses.length <= 1) { return; } @@ -90,7 +90,7 @@ export class TerminalService implements ITerminalService { public focusPrevious(): TPromise { return this.focus().then(() => { - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + return this.showAndGetTerminalPanel().then((terminalPanel) => { if (this.terminalProcesses.length <= 1) { return; } @@ -106,7 +106,7 @@ export class TerminalService implements ITerminalService { } public runSelectedText(): TPromise { - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + return this.showAndGetTerminalPanel().then((terminalPanel) => { let editor = this.codeEditorService.getFocusedCodeEditor(); let selection = editor.getModel().getValueInRange(editor.getSelection()); // Add a new line if one doesn't already exist so the text is executed @@ -126,6 +126,10 @@ export class TerminalService implements ITerminalService { return TPromise.as(null); } + return this.show(); + } + + public show(): TPromise { return this.panelService.openPanel(TERMINAL_PANEL_ID, true); } @@ -139,7 +143,21 @@ export class TerminalService implements ITerminalService { public createNew(): TPromise { let self = this; - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + let processCount = this.terminalProcesses.length; + + return this.showAndGetTerminalPanel().then((terminalPanel) => { + // terminalPanel will be null if createNew is called from the command before the + // TerminalPanel has been initialized. In this case, skip creating the terminal here + // data rely on TerminalPanel's constructor creating the new instance. + if (!terminalPanel) { + return; + } + + // Only create a new process if none have been created since toggling the terminal panel + if (processCount !== this.terminalProcesses.length) { + return; + } + self.initConfigHelper(terminalPanel.getContainer()); terminalPanel.createNewTerminalInstance(self.createTerminalProcess()); self._onInstancesChanged.fire(); @@ -147,16 +165,16 @@ export class TerminalService implements ITerminalService { } public close(): TPromise { - return this.toggleAndGetTerminalPanel().then((terminalPanel) => { + return this.showAndGetTerminalPanel().then((terminalPanel) => { terminalPanel.closeActiveTerminal(); }); } - private toggleAndGetTerminalPanel(): TPromise { + private showAndGetTerminalPanel(): TPromise { return new TPromise((complete) => { let panel = this.panelService.getActivePanel(); if (!panel || panel.getId() !== TERMINAL_PANEL_ID) { - this.toggle().then(() => { + this.show().then(() => { panel = this.panelService.getActivePanel(); complete(panel); }); -- GitLab