From fa8ff74e549eb2f155cd177747a73f30479d229d Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 3 Mar 2022 14:50:47 -0800 Subject: [PATCH] Use new `isTransient` API to prevent duplicate integrated consoles This new API available today in VS Code 1.65.0 prevents Code from saving and reloading the integrated console when the extension is reloaded. This is necessary because the extension always spawns a new terminal to host PowerShell Editor Services when it starts, and Code's newish feature to save and restore terminals caused these to be duplicated. When we update the VS Code engine, we can remove the cast to `any`. --- src/process.ts | 26 +++++++++++++++----------- src/session.ts | 12 ++---------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/process.ts b/src/process.ts index 507e195773..f64eb45843 100644 --- a/src/process.ts +++ b/src/process.ts @@ -2,9 +2,7 @@ // Licensed under the MIT License. import cp = require("child_process"); -import fs = require("fs"); -import net = require("net"); -import os = require("os"); +import * as semver from "semver"; import path = require("path"); import vscode = require("vscode"); import { Logger } from "./logging"; @@ -104,14 +102,20 @@ export class PowerShellProcess { utils.deleteSessionFile(this.sessionFilePath); // Launch PowerShell in the integrated terminal - this.consoleTerminal = - vscode.window.createTerminal({ - name: this.title, - shellPath: this.exePath, - shellArgs: powerShellArgs, - hideFromUser: !this.sessionSettings.integratedConsole.showOnStartup, - cwd: this.sessionSettings.cwd - }); + const terminalOptions: vscode.TerminalOptions = { + name: this.title, + shellPath: this.exePath, + shellArgs: powerShellArgs, + hideFromUser: !this.sessionSettings.integratedConsole.showOnStartup, + cwd: this.sessionSettings.cwd, + }; + + // This API is available only in newer versions of VS Code. + if (semver.gte(vscode.version, "1.65.0")) { + (terminalOptions as any).isTransient = true; + } + + this.consoleTerminal = vscode.window.createTerminal(terminalOptions); const pwshName = path.basename(this.exePath); this.log.write(`${pwshName} started.`); diff --git a/src/session.ts b/src/session.ts index 81803deb98..1e00f44771 100644 --- a/src/session.ts +++ b/src/session.ts @@ -660,7 +660,6 @@ export class SessionManager implements Middleware { case SessionStatus.Running: case SessionStatus.NeverStarted: case SessionStatus.NotStarted: - // This icon is available since 1.56, now our current engine version. this.statusBarItem.text = "$(terminal-powershell)"; // These have to be reset because this function mutates state. this.statusBarItem.color = undefined; @@ -669,18 +668,11 @@ export class SessionManager implements Middleware { case SessionStatus.Initializing: case SessionStatus.Stopping: this.statusBarItem.text = "$(sync)"; - // The warning colors were added later than our current engine version. - // https://code.visualstudio.com/api/references/theme-color#status-bar-colors - this.statusBarItem.color = (semver.gte(vscode.version, "1.59.0")) - ? new vscode.ThemeColor("statusBarItem.warningForeground") - : new vscode.ThemeColor("statusBarItem.errorForeground"); - this.statusBarItem.backgroundColor = (semver.gte(vscode.version, "1.59.0")) - ? new vscode.ThemeColor("statusBarItem.warningBackground") - : new vscode.ThemeColor("statusBarItem.errorBackground"); + this.statusBarItem.color = new vscode.ThemeColor("statusBarItem.warningForeground"); + this.statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.warningBackground"); break; case SessionStatus.Failed: this.statusBarItem.text = "$(alert)"; - // The error colors have been available since 1.53. this.statusBarItem.color = new vscode.ThemeColor("statusBarItem.errorForeground"); this.statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground"); break;