Skip to content

Commit 0d35540

Browse files
author
Kapil Borle
committed
Clean up format status during session restart
1 parent 0e9df49 commit 0d35540

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/features/DocumentFormatter.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class DocumentLocker {
132132

133133
class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
134134
private static documentLocker = new DocumentLocker();
135+
private static statusBarTracker = new Object();
135136
private languageClient: LanguageClient;
136137

137138
// The order in which the rules will be executed starting from the first element.
@@ -180,13 +181,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
180181
let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(editor, range, options, 0);
181182
this.lockDocument(document, textEdits);
182183

183-
// If the session crashes for any reason during formatting
184-
// then the document won't be released and hence we won't
185-
// be able to format it after restarting the session.
186-
// Similar issue with the status - the bar will keep displaying
187-
// the previous status even after restarting the session.
188-
// this.releaseDocument(document, textEdits);
189-
AnimatedStatusBar.showAnimatedStatusBarMessage("Formatting PowerShell document", textEdits);
184+
// FIXME If the session crashes for any reason during formatting
185+
// then the bar will keep displaying the previous status even after restarting the session.
186+
// A fix is to restart the window
187+
// AnimatedStatusBar.showAnimatedStatusBarMessage("Formatting PowerShell document", textEdits);
188+
PSDocumentFormattingEditProvider.showStatusBar(document, textEdits);
190189
return textEdits;
191190
}
192191

@@ -315,7 +314,12 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
315314

316315
setLanguageClient(languageClient: LanguageClient): void {
317316
this.languageClient = languageClient;
317+
318+
// setLanguageClient is called while restarting a session,
319+
// so this makes sure we clean up the document locker and
320+
// any residual status bars
318321
PSDocumentFormattingEditProvider.documentLocker.unlockAll();
322+
PSDocumentFormattingEditProvider.disposeAllStatusBars();
319323
}
320324

321325
getSettings(rule: string): any {
@@ -341,6 +345,27 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
341345
settings[rule] = ruleSettings;
342346
return settings;
343347
}
348+
349+
private static showStatusBar(document: TextDocument, hideWhenDone: Thenable<any>): void {
350+
let statusBar = AnimatedStatusBar.showAnimatedStatusBarMessage("Formatting PowerShell document", hideWhenDone);
351+
this.statusBarTracker[document.uri.toString()] = statusBar;
352+
hideWhenDone.then(() => {
353+
this.disposeStatusBar(document.uri.toString());
354+
});
355+
}
356+
357+
private static disposeStatusBar(documentUri: string) {
358+
if (this.statusBarTracker.hasOwnProperty(documentUri)) {
359+
this.statusBarTracker[documentUri].dispose();
360+
delete this.statusBarTracker[documentUri];
361+
}
362+
}
363+
364+
private static disposeAllStatusBars() {
365+
Object.keys(this.statusBarTracker).slice().forEach((key) => this.disposeStatusBar(key));
366+
}
367+
368+
344369
}
345370

346371
export class DocumentFormatterFeature implements IFeature {

0 commit comments

Comments
 (0)