@@ -132,6 +132,7 @@ class DocumentLocker {
132
132
133
133
class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider , DocumentRangeFormattingEditProvider {
134
134
private static documentLocker = new DocumentLocker ( ) ;
135
+ private static statusBarTracker = new Object ( ) ;
135
136
private languageClient : LanguageClient ;
136
137
137
138
// The order in which the rules will be executed starting from the first element.
@@ -180,13 +181,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
180
181
let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( editor , range , options , 0 ) ;
181
182
this . lockDocument ( document , textEdits ) ;
182
183
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 ) ;
190
189
return textEdits ;
191
190
}
192
191
@@ -315,7 +314,12 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
315
314
316
315
setLanguageClient ( languageClient : LanguageClient ) : void {
317
316
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
318
321
PSDocumentFormattingEditProvider . documentLocker . unlockAll ( ) ;
322
+ PSDocumentFormattingEditProvider . disposeAllStatusBars ( ) ;
319
323
}
320
324
321
325
getSettings ( rule : string ) : any {
@@ -341,6 +345,27 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
341
345
settings [ rule ] = ruleSettings ;
342
346
return settings ;
343
347
}
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
+
344
369
}
345
370
346
371
export class DocumentFormatterFeature implements IFeature {
0 commit comments