@@ -82,8 +82,56 @@ function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): nu
82
82
}
83
83
}
84
84
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
+
85
133
class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider , DocumentRangeFormattingEditProvider {
86
- private static filesBeingFormatted : Object = new Object ;
134
+ private static documentLocker = new DocumentLocker ( ) ;
87
135
private languageClient : LanguageClient ;
88
136
89
137
// The order in which the rules will be executed starting from the first element.
@@ -129,15 +177,15 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
129
177
return this . emptyPromise ;
130
178
}
131
179
132
- this . lockDocument ( document ) ;
133
180
let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( editor , range , options , 0 ) ;
181
+ this . lockDocument ( document , textEdits ) ;
134
182
135
183
// If the session crashes for any reason during formatting
136
184
// then the document won't be released and hence we won't
137
185
// be able to format it after restarting the session.
138
186
// Similar issue with the status - the bar will keep displaying
139
187
// the previous status even after restarting the session.
140
- this . releaseDocument ( document , textEdits ) ;
188
+ // this.releaseDocument(document, textEdits);
141
189
AnimatedStatusBar . showAnimatedStatusBarMessage ( "Formatting PowerShell document" , textEdits ) ;
142
190
return textEdits ;
143
191
}
@@ -147,29 +195,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
147
195
}
148
196
149
197
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 ) ;
155
199
}
156
200
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 ) ;
173
203
}
174
204
175
205
executeRulesInOrder (
@@ -285,6 +315,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
285
315
286
316
setLanguageClient ( languageClient : LanguageClient ) : void {
287
317
this . languageClient = languageClient ;
318
+ PSDocumentFormattingEditProvider . documentLocker . unlockAll ( ) ;
288
319
}
289
320
290
321
getSettings ( rule : string ) : any {
0 commit comments