Skip to content

Commit 0197b50

Browse files
authored
Merge pull request PowerShell#461 from PowerShell/kapilmb/formatting-status
Show formatting status in status bar
2 parents 8f4a2bd + 99a17cc commit 0197b50

File tree

2 files changed

+130
-3
lines changed

2 files changed

+130
-3
lines changed

src/animatedStatusBar.ts

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import {
6+
StatusBarItem,
7+
StatusBarAlignment,
8+
Disposable,
9+
window} from "vscode";
10+
11+
export function setAnimatedStatusBarMessage(text: string, hideWhenDone: Thenable<any>): Disposable {
12+
let animatedStatusBarItem: AnimatedStatusBarItem = new AnimatedStatusBarItem(text);
13+
animatedStatusBarItem.show(hideWhenDone);
14+
return animatedStatusBarItem;
15+
}
16+
17+
class AnimatedStatusBarItem implements StatusBarItem {
18+
private readonly animationRate: number;
19+
private statusBarItem: StatusBarItem;
20+
private maxCount: number;
21+
private counter: number;
22+
private baseText: string;
23+
private timerInterval: number;
24+
private elapsedTime: number;
25+
private intervalId: NodeJS.Timer;
26+
private suffixStates: string[];
27+
28+
public get alignment(): StatusBarAlignment {
29+
return this.statusBarItem.alignment;
30+
}
31+
32+
public get priority(): number {
33+
return this.statusBarItem.priority;
34+
}
35+
36+
public get text(): string {
37+
return this.statusBarItem.text;
38+
}
39+
40+
public set text(value: string) {
41+
this.statusBarItem.text = value;
42+
}
43+
44+
public get tooltip(): string {
45+
return this.statusBarItem.tooltip;
46+
}
47+
48+
public set tooltip(value: string) {
49+
this.statusBarItem.tooltip = value;
50+
}
51+
52+
public get color(): string {
53+
return this.statusBarItem.color;
54+
}
55+
56+
public set color(value: string) {
57+
this.statusBarItem.color = value;
58+
}
59+
60+
public get command(): string {
61+
return this.statusBarItem.command;
62+
}
63+
64+
public set command(value: string) {
65+
this.statusBarItem.command = value;
66+
}
67+
68+
constructor(baseText: string, alignment?: StatusBarAlignment, priority?: number) {
69+
this.animationRate = 1;
70+
this.statusBarItem = window.createStatusBarItem(alignment, priority);
71+
this.baseText = baseText;
72+
this.counter = 0;
73+
this.suffixStates = [" ", ". ", ".. ", "..."];
74+
this.maxCount = this.suffixStates.length;
75+
this.timerInterval = ((1/this.maxCount) * 1000) / this.animationRate;
76+
this.elapsedTime = 0;
77+
}
78+
79+
public show(hideWhenDone?: Thenable<any>): void {
80+
this.statusBarItem.show();
81+
this.start();
82+
if (hideWhenDone !== undefined) {
83+
hideWhenDone.then(() => this.hide());
84+
}
85+
}
86+
87+
public hide(): void {
88+
this.stop();
89+
this.statusBarItem.hide();
90+
}
91+
92+
public dispose(): void {
93+
this.statusBarItem.dispose();
94+
}
95+
96+
private updateCounter(): void {
97+
this.counter = (this.counter + 1) % this.maxCount;
98+
this.elapsedTime = this.elapsedTime + this.timerInterval;
99+
}
100+
101+
private updateText(): void {
102+
this.text = this.baseText + this.suffixStates[this.counter];
103+
}
104+
105+
private update(): void {
106+
this.updateCounter();
107+
this.updateText();
108+
}
109+
110+
private reset(): void {
111+
this.counter = 0;
112+
this.updateText();
113+
}
114+
115+
private start(): void {
116+
this.reset();
117+
this.intervalId = setInterval(() => this.update(), this.timerInterval);
118+
}
119+
120+
private stop(): void {
121+
clearInterval(this.intervalId);
122+
}
123+
}

src/features/DocumentFormatter.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Window = vscode.window;
1818
import { IFeature } from '../feature';
1919
import * as Settings from '../settings';
2020
import * as Utils from '../utils';
21+
import * as AnimatedStatusBar from '../animatedStatusBar';
2122

2223
export namespace ScriptFileMarkersRequest {
2324
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptFileMarkers"; } };
@@ -110,14 +111,17 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
110111
range: Range,
111112
options: FormattingOptions,
112113
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
113-
return this.executeRulesInOrder(document, range, options, 0);
114+
115+
let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(document, range, options, 0);
116+
AnimatedStatusBar.setAnimatedStatusBarMessage("formatting", textEdits);
117+
return textEdits;
114118
}
115119

116120
executeRulesInOrder(
117121
document: TextDocument,
118122
range: Range,
119123
options: FormattingOptions,
120-
index: number): Thenable<TextEdit[]> | TextEdit[] {
124+
index: number): Thenable<TextEdit[]> {
121125
if (this.languageClient !== null && index < this.ruleOrder.length) {
122126
let rule = this.ruleOrder[index];
123127
let uniqueEdits: ScriptRegion[] = [];
@@ -172,7 +176,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
172176
return this.executeRulesInOrder(document, range, options, index + 1);
173177
});
174178
} else {
175-
return TextEdit[0];
179+
return Promise.resolve(TextEdit[0]);
176180
}
177181
}
178182

0 commit comments

Comments
 (0)