Skip to content

Show formatting status in status bar #461

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 7 commits into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
123 changes: 123 additions & 0 deletions src/animatedStatusBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import {
StatusBarItem,
StatusBarAlignment,
Disposable,
window} from "vscode";

export function setAnimatedStatusBarMessage(text: string, hideWhenDone: Thenable<any>): Disposable {
let animatedStatusBarItem: AnimatedStatuBarItem = new AnimatedStatuBarItem(text);
animatedStatusBarItem.show(hideWhenDone);
return animatedStatusBarItem;
}

class AnimatedStatuBarItem implements StatusBarItem {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight typo, should be AnimatedStatusBarItem

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

private readonly animationRate: number;
private statusBarItem: StatusBarItem;
private maxCount: number;
private counter: number;
private baseText: string;
private timerInterval: number;
private elapsedTime: number;
private intervalId: NodeJS.Timer;
private suffixStates: string[];

get alignment(): StatusBarAlignment {
return this.statusBarItem.alignment;
}

get priority(): number {
return this.statusBarItem.priority;
}

get text(): string {
return this.statusBarItem.text;
}

set text(value: string) {
this.statusBarItem.text = value;
}

get tooltip(): string {
return this.statusBarItem.tooltip;
}

set tooltip(value: string) {
this.statusBarItem.tooltip = value;
}

get color(): string {
return this.statusBarItem.color;
}

set color(value: string) {
this.statusBarItem.color = value;
}

get command(): string {
return this.statusBarItem.command;
}

set command(value: string) {
this.statusBarItem.command = value;
}

constructor(baseText: string, alignment?: StatusBarAlignment, priority?: number) {
this.animationRate = 1;
this.statusBarItem = window.createStatusBarItem(alignment, priority);
this.baseText = baseText;
this.counter = 0;
this.suffixStates = [" ", ". ", ".. ", "..."];
this.maxCount = this.suffixStates.length;
this.timerInterval = ((1/this.maxCount) * 1000) / this.animationRate;
this.elapsedTime = 0;
}

show(hideWhenDone?: Thenable<any>): void {
this.statusBarItem.show();
this._start();
if (hideWhenDone !== undefined) {
hideWhenDone.then(() => this.hide());
}
}

hide(): void {
this._stop();
this.statusBarItem.hide();
}

dispose(): void {
this.statusBarItem.dispose();
}

_updateCounter(): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript has 'private' visibility, you should use that instead of the underscore naming convention

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

this.counter = (this.counter + 1) % this.maxCount;
this.elapsedTime = this.elapsedTime + this.timerInterval;
}

_updateText(): void {
this.text = this.baseText + this.suffixStates[this.counter];
}

_update(): void {
this._updateCounter();
this._updateText();
}

_reset(): void {
this.counter = 0;
this._updateText();
}

_start(): void {
this._reset();
this.intervalId = setInterval(() => this._update(), this.timerInterval);
}

_stop(): void {
clearInterval(this.intervalId);
}
}
10 changes: 7 additions & 3 deletions src/features/DocumentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Window = vscode.window;
import { IFeature } from '../feature';
import * as Settings from '../settings';
import * as Utils from '../utils';
import * as AnimatedStatusBar from '../animatedStatusBar';

export namespace ScriptFileMarkersRequest {
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptFileMarkers"; } };
Expand Down Expand Up @@ -110,14 +111,17 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
range: Range,
options: FormattingOptions,
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
return this.executeRulesInOrder(document, range, options, 0);

let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(document, range, options, 0);
AnimatedStatusBar.setAnimatedStatusBarMessage("formatting", textEdits);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend a more obvious string like "PowerShell code formatting in progress"

return textEdits;
}

executeRulesInOrder(
document: TextDocument,
range: Range,
options: FormattingOptions,
index: number): Thenable<TextEdit[]> | TextEdit[] {
index: number): Thenable<TextEdit[]> {
if (this.languageClient !== null && index < this.ruleOrder.length) {
let rule = this.ruleOrder[index];
let uniqueEdits: ScriptRegion[] = [];
Expand Down Expand Up @@ -172,7 +176,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
return this.executeRulesInOrder(document, range, options, index + 1);
});
} else {
return TextEdit[0];
return Promise.resolve(TextEdit[0]);
}
}

Expand Down