@@ -82,8 +82,56 @@ function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): nu
8282 }
8383}
8484
85+ class DocumentLocker {
86+ private lockedDocuments : Object ;
87+
88+ constructor ( ) {
89+ this . lockedDocuments = new Object ( ) ;
90+ }
91+
92+ isLocked ( document : TextDocument ) : boolean {
93+ return this . isLockedInternal ( this . getKey ( document ) ) ;
94+ }
95+
96+ lock ( document : TextDocument , unlockWhenDone ?: Thenable < any > ) : void {
97+ this . lockInternal ( this . getKey ( document ) , unlockWhenDone ) ;
98+ }
99+
100+ unlock ( document : TextDocument ) : void {
101+ this . unlockInternal ( this . getKey ( document ) ) ;
102+ }
103+
104+ unlockAll ( ) : void {
105+ Object . keys ( this . lockedDocuments ) . slice ( ) . forEach ( documentKey => this . unlockInternal ( documentKey ) ) ;
106+ }
107+
108+ private getKey ( document : TextDocument ) : string {
109+ return document . uri . toString ( ) ;
110+ }
111+
112+ private lockInternal ( documentKey : string , unlockWhenDone ?: Thenable < any > ) : void {
113+ if ( ! this . isLockedInternal ( documentKey ) ) {
114+ this . lockedDocuments [ documentKey ] = true ;
115+ }
116+
117+ if ( unlockWhenDone !== undefined ) {
118+ unlockWhenDone . then ( ( ) => this . unlockInternal ( documentKey ) ) ;
119+ }
120+ }
121+
122+ private unlockInternal ( documentKey : string ) : void {
123+ if ( this . isLockedInternal ( documentKey ) ) {
124+ delete this . lockedDocuments [ documentKey ] ;
125+ }
126+ }
127+
128+ private isLockedInternal ( documentKey : string ) : boolean {
129+ return this . lockedDocuments . hasOwnProperty ( documentKey ) ;
130+ }
131+ }
132+
85133class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider , DocumentRangeFormattingEditProvider {
86- private static filesBeingFormatted : Object = new Object ;
134+ private static documentLocker = new DocumentLocker ( ) ;
87135 private languageClient : LanguageClient ;
88136
89137 // The order in which the rules will be executed starting from the first element.
@@ -129,15 +177,15 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
129177 return this . emptyPromise ;
130178 }
131179
132- this . lockDocument ( document ) ;
133180 let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( editor , range , options , 0 ) ;
181+ this . lockDocument ( document , textEdits ) ;
134182
135183 // If the session crashes for any reason during formatting
136184 // then the document won't be released and hence we won't
137185 // be able to format it after restarting the session.
138186 // Similar issue with the status - the bar will keep displaying
139187 // the previous status even after restarting the session.
140- this . releaseDocument ( document , textEdits ) ;
188+ // this.releaseDocument(document, textEdits);
141189 AnimatedStatusBar . showAnimatedStatusBarMessage ( "Formatting PowerShell document" , textEdits ) ;
142190 return textEdits ;
143191 }
@@ -147,29 +195,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
147195 }
148196
149197 isDocumentLocked ( document : TextDocument ) : boolean {
150- if ( PSDocumentFormattingEditProvider . filesBeingFormatted . hasOwnProperty ( this . getDocumentKey ( document ) ) ) {
151- return true ;
152- }
153-
154- return false ;
198+ return PSDocumentFormattingEditProvider . documentLocker . isLocked ( document ) ;
155199 }
156200
157- lockDocument ( document : TextDocument ) : void {
158- if ( ! this . isDocumentLocked ( document ) ) {
159- PSDocumentFormattingEditProvider . filesBeingFormatted [ this . getDocumentKey ( document ) ] = true ;
160- }
161- }
162-
163- releaseDocument ( document : TextDocument , releaseWhenDone : Thenable < any > ) : void {
164- if ( this . isDocumentLocked ( document ) ) {
165- releaseWhenDone . then ( ( ) => {
166- delete PSDocumentFormattingEditProvider . filesBeingFormatted [ this . getDocumentKey ( document ) ] ;
167- } ) ;
168- }
169- }
170-
171- getDocumentKey ( document : TextDocument ) : string {
172- return document . uri . toString ( ) ;
201+ lockDocument ( document : TextDocument , unlockWhenDone : Thenable < any > ) : void {
202+ PSDocumentFormattingEditProvider . documentLocker . lock ( document , unlockWhenDone ) ;
173203 }
174204
175205 executeRulesInOrder (
@@ -285,6 +315,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
285315
286316 setLanguageClient ( languageClient : LanguageClient ) : void {
287317 this . languageClient = languageClient ;
318+ PSDocumentFormattingEditProvider . documentLocker . unlockAll ( ) ;
288319 }
289320
290321 getSettings ( rule : string ) : any {
0 commit comments