@@ -26,6 +26,7 @@ import {
26
26
} from 'vscode-languageclient' ;
27
27
import { TextDocumentIdentifier } from "vscode-languageserver-types" ;
28
28
import Window = vscode . window ;
29
+ import { Logger } from '../logging' ;
29
30
import { IFeature } from '../feature' ;
30
31
import * as Settings from '../settings' ;
31
32
import * as Utils from '../utils' ;
@@ -130,22 +131,60 @@ class PSDocumentFormattingEditProvider implements
130
131
return Promise . resolve ( TextEdit [ 0 ] ) ;
131
132
}
132
133
133
- constructor ( ) {
134
+ constructor ( private logger : Logger ) {
134
135
}
135
136
136
137
provideDocumentFormattingEdits (
137
138
document : TextDocument ,
138
139
options : FormattingOptions ,
139
140
token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
140
- return this . provideDocumentRangeFormattingEdits ( document , null , options , token ) ;
141
- }
141
+
142
+ this . logger . writeVerbose ( `Formatting entire document - ${ document . uri } ...` )
143
+ return this . sendDocumentFormatRequest ( document , null , options , token ) ;
144
+ }
142
145
143
146
provideDocumentRangeFormattingEdits (
144
147
document : TextDocument ,
145
148
range : Range ,
146
149
options : FormattingOptions ,
147
150
token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
148
151
152
+ this . logger . writeVerbose ( `Formatting document range ${ JSON . stringify ( range ) } - ${ document . uri } ...` )
153
+ return this . sendDocumentFormatRequest ( document , range , options , token ) ;
154
+ }
155
+
156
+ provideOnTypeFormattingEdits (
157
+ document : TextDocument ,
158
+ position : Position ,
159
+ ch : string ,
160
+ options : FormattingOptions ,
161
+ token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
162
+
163
+ this . logger . writeVerbose ( `Formatting on type at position ${ JSON . stringify ( position ) } - ${ document . uri } ...` )
164
+
165
+ return this . getScriptRegion ( document , position , ch ) . then ( scriptRegion => {
166
+ if ( scriptRegion === null ) {
167
+ this . logger . writeVerbose ( "No formattable range returned." ) ;
168
+ return this . emptyPromise ;
169
+ }
170
+
171
+ return this . sendDocumentFormatRequest (
172
+ document ,
173
+ toRange ( scriptRegion ) ,
174
+ options ,
175
+ token ) ;
176
+ } ,
177
+ ( err ) => {
178
+ this . logger . writeVerbose ( `Error while requesting script region for formatting: ${ err } ` )
179
+ } ) ;
180
+ }
181
+
182
+ private sendDocumentFormatRequest (
183
+ document : TextDocument ,
184
+ range : Range ,
185
+ options : FormattingOptions ,
186
+ token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
187
+
149
188
let editor : TextEditor = this . getEditor ( document ) ;
150
189
if ( editor === undefined ) {
151
190
return this . emptyPromise ;
@@ -157,7 +196,6 @@ class PSDocumentFormattingEditProvider implements
157
196
return this . emptyPromise ;
158
197
}
159
198
160
-
161
199
// somehow range object gets serialized to an array of Position objects,
162
200
// so we need to use the object literal syntax to initialize it.
163
201
let rangeParam = null ;
@@ -180,31 +218,25 @@ class PSDocumentFormattingEditProvider implements
180
218
options : this . getEditorSettings ( )
181
219
} ;
182
220
221
+ let formattingStartTime = new Date ( ) . valueOf ( ) ;
222
+ function getFormattingDuration ( ) {
223
+ return ( ( new Date ( ) . valueOf ( ) ) - formattingStartTime ) / 1000 ;
224
+ }
225
+
183
226
let textEdits = this . languageClient . sendRequest (
184
227
DocumentRangeFormattingRequest . type ,
185
228
requestParams ) ;
186
229
this . lockDocument ( document , textEdits ) ;
187
230
PSDocumentFormattingEditProvider . showStatusBar ( document , textEdits ) ;
188
- return textEdits ;
189
- }
190
-
191
- provideOnTypeFormattingEdits (
192
- document : TextDocument ,
193
- position : Position ,
194
- ch : string ,
195
- options : FormattingOptions ,
196
- token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
197
- return this . getScriptRegion ( document , position , ch ) . then ( scriptRegion => {
198
- if ( scriptRegion === null ) {
199
- return this . emptyPromise ;
200
- }
201
231
202
- return this . provideDocumentRangeFormattingEdits (
203
- document ,
204
- toRange ( scriptRegion ) ,
205
- options ,
206
- token ) ;
207
- } ) ;
232
+ return textEdits . then (
233
+ ( edits ) => {
234
+ this . logger . writeVerbose ( `Document formatting finished in ${ getFormattingDuration ( ) } s` ) ;
235
+ return edits ;
236
+ } ,
237
+ ( err ) => {
238
+ this . logger . writeVerbose ( `Document formatting failed in ${ getFormattingDuration ( ) } : ${ err } ` ) ;
239
+ } ) ;
208
240
}
209
241
210
242
setLanguageClient ( languageClient : LanguageClient ) : void {
@@ -284,8 +316,8 @@ export class DocumentFormatterFeature implements IFeature {
284
316
private languageClient : LanguageClient ;
285
317
private documentFormattingEditProvider : PSDocumentFormattingEditProvider ;
286
318
287
- constructor ( ) {
288
- this . documentFormattingEditProvider = new PSDocumentFormattingEditProvider ( ) ;
319
+ constructor ( private logger : Logger ) {
320
+ this . documentFormattingEditProvider = new PSDocumentFormattingEditProvider ( logger ) ;
289
321
this . formattingEditProvider = vscode . languages . registerDocumentFormattingEditProvider (
290
322
"powershell" ,
291
323
this . documentFormattingEditProvider ) ;
0 commit comments