Skip to content

Re-implement indicator when running registered editor commands #4187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 6 additions & 43 deletions src/features/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { ICheckboxQuickPickItem, showCheckboxQuickPick } from "../controls/check
import { Logger } from "../logging";
import Settings = require("../settings");
import { LanguageClientConsumer } from "../languageClientConsumer";
import { SessionManager } from "../session";

export const EvaluateRequestType = new RequestType<IEvaluateRequestArguments, void, void>("evaluate");
export const OutputNotificationType = new NotificationType<IOutputNotificationBody>("output");
export const ExecutionStatusChangedNotificationType =
new NotificationType<IExecutionStatusDetails>("powerShell/executionStatusChanged");
new NotificationType<ExecutionStatus>("powerShell/executionStatusChanged");

export const ShowChoicePromptRequestType =
new RequestType<IShowChoicePromptRequestArgs,
Expand All @@ -33,12 +34,6 @@ export interface IOutputNotificationBody {
output: string;
}

interface IExecutionStatusDetails {
executionOptions: IExecutionOptions;
executionStatus: ExecutionStatus;
hadErrors: boolean;
}

interface IChoiceDetails {
label: string;
helpMessage: string;
Expand Down Expand Up @@ -75,13 +70,6 @@ enum ExecutionStatus {
Completed,
}

interface IExecutionOptions {
writeOutputToHost: boolean;
writeErrorsToHost: boolean;
addToHistory: boolean;
interruptCommandPrompt: boolean;
}

function showChoicePrompt(
promptDetails: IShowChoicePromptRequestArgs,
client: LanguageClient): Thenable<IShowChoicePromptResponseBody> {
Expand Down Expand Up @@ -196,7 +184,7 @@ export class ConsoleFeature extends LanguageClientConsumer {
private handlers: vscode.Disposable[];
private resolveStatusBarPromise: (value?: {} | PromiseLike<{}>) => void;

constructor(private log: Logger) {
constructor(private log: Logger, private sessionManager: SessionManager) {
super();
this.commands = [
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {
Expand Down Expand Up @@ -236,8 +224,6 @@ export class ConsoleFeature extends LanguageClientConsumer {
}

public dispose() {
// Make sure we cancel any status bar
this.clearStatusBar();
for (const command of this.commands) {
command.dispose();
}
Expand All @@ -257,44 +243,21 @@ export class ConsoleFeature extends LanguageClientConsumer {
ShowInputPromptRequestType,
(promptDetails) => showInputPrompt(promptDetails)),

// TODO: We're not receiving these events from the server any more.
// Set up status bar alerts for when PowerShell is executing a script.
this.languageClient.onNotification(
ExecutionStatusChangedNotificationType,
(executionStatusDetails) => {
switch (executionStatusDetails.executionStatus) {
// If execution has changed to running, make a notification
switch (executionStatusDetails) {
case ExecutionStatus.Running:
this.showExecutionStatus("PowerShell");
this.sessionManager.setSessionBusyStatus();
break;

// If the execution has stopped, destroy the previous notification
case ExecutionStatus.Completed:
case ExecutionStatus.Aborted:
case ExecutionStatus.Failed:
this.clearStatusBar();
this.sessionManager.setSessionRunningStatus();
break;
}
})
]
}

private showExecutionStatus(message: string) {
vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
}, (progress) => {
return new Promise((resolve, _reject) => {
this.clearStatusBar();

this.resolveStatusBarPromise = resolve;
progress.report({ message });
});
});
}

private clearStatusBar() {
if (this.resolveStatusBarPromise) {
this.resolveStatusBarPromise();
}
}
}
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

"use strict";

import path = require("path");
import vscode = require("vscode");
import TelemetryReporter from "@vscode/extension-telemetry";
import { DocumentSelector } from "vscode-languageclient";
Expand Down Expand Up @@ -150,7 +149,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower

// Features and command registrations that require language client
languageClientConsumers = [
new ConsoleFeature(logger),
new ConsoleFeature(logger, sessionManager),
new ExpandAliasFeature(logger),
new GetCommandsFeature(logger),
new ShowHelpFeature(logger),
Expand Down
49 changes: 15 additions & 34 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum SessionStatus {
NotStarted,
Initializing,
Running,
Busy,
Stopping,
Failed,
}
Expand Down Expand Up @@ -76,10 +77,6 @@ export const PowerShellVersionRequestType =
new RequestType0<IPowerShellVersionDetails, void>(
"powerShell/getVersion");

export const RunspaceChangedEventType =
new NotificationType<IRunspaceDetails>(
"powerShell/runspaceChanged");

export class SessionManager implements Middleware {
public HostName: string;
public HostVersion: string;
Expand Down Expand Up @@ -502,21 +499,6 @@ Type 'help' to get help.
}
}

private setStatusBarVersionString(runspaceDetails: IRunspaceDetails) {
const psVersion = runspaceDetails.powerShellVersion;

let versionString =
this.versionDetails.architecture === "x86"
? `${psVersion.displayVersion} (${psVersion.architecture})`
: psVersion.displayVersion;

if (runspaceDetails.runspaceType !== RunspaceType.Local) {
versionString += ` [${runspaceDetails.connectionString}]`;
}

this.setSessionVersion(versionString);
}

private registerCommands(): void {
this.registeredCommands = [
vscode.commands.registerCommand("PowerShell.RestartSession", async () => { await this.restartSession(); }),
Expand Down Expand Up @@ -676,11 +658,6 @@ Type 'help' to get help.
this.languageClient.onNotification(
SendKeyPressNotificationType,
() => { this.languageServerProcess.sendKeyPress(); }),

// TODO: I'm not sure we're still receiving these notifications...
this.languageClient.onNotification(
RunspaceChangedEventType,
(runspaceDetails) => { this.setStatusBarVersionString(runspaceDetails); }),
]

try {
Expand All @@ -691,13 +668,9 @@ Type 'help' to get help.
}

this.versionDetails = await this.languageClient.sendRequest(PowerShellVersionRequestType);

this.setSessionRunningStatus(); // This requires the version details to be set.
this.sendTelemetryEvent("powershellVersionCheck", { powershellVersion: this.versionDetails.version });

this.setSessionVersion(this.versionDetails.architecture === "x86"
? `${this.versionDetails.displayVersion} (${this.versionDetails.architecture})`
: this.versionDetails.displayVersion);

// We haven't "started" until we're done getting the version information.
this.started = true;

Expand Down Expand Up @@ -753,30 +726,38 @@ Type 'help' to get help.
case SessionStatus.NeverStarted:
case SessionStatus.NotStarted:
this.languageStatusItem.busy = false;
// @ts-ignore
this.languageStatusItem.severity = vscode.LanguageStatusSeverity.Information;
break;
case SessionStatus.Busy:
this.languageStatusItem.busy = true;
this.languageStatusItem.severity = vscode.LanguageStatusSeverity.Information;
break;
case SessionStatus.Initializing:
case SessionStatus.Stopping:
this.languageStatusItem.busy = true;
// @ts-ignore
this.languageStatusItem.severity = vscode.LanguageStatusSeverity.Warning;
break;
case SessionStatus.Failed:
this.languageStatusItem.busy = false;
// @ts-ignore
this.languageStatusItem.severity = vscode.LanguageStatusSeverity.Error;
break;
}

}

private setSessionVersion(version: string): void {
// TODO: Accept a VersionDetails object instead of a string.
public setSessionRunningStatus(): void {
const version = this.versionDetails.architecture === "x86"
? `${this.versionDetails.displayVersion} (${this.versionDetails.architecture})`
: this.versionDetails.displayVersion;

this.languageStatusItem.text = "$(terminal-powershell) " + version;
this.setSessionStatus(version, SessionStatus.Running);
}

public setSessionBusyStatus(): void {
this.setSessionStatus("Executing...", SessionStatus.Busy);
}

private async setSessionFailure(message: string, ...additionalMessages: string[]) {
this.setSessionStatus("Initialization Error", SessionStatus.Failed);
await this.log.writeAndShowError(message, ...additionalMessages);
Expand Down