22 * Copyright (C) Microsoft Corporation. All rights reserved.
33 *--------------------------------------------------------*/
44
5+ import * as path from "path" ;
56import vscode = require( 'vscode' ) ;
67import {
78 TextDocument ,
@@ -11,6 +12,7 @@ import {
1112 DocumentFormattingEditProvider ,
1213 DocumentRangeFormattingEditProvider ,
1314 Range ,
15+ TextEditor
1416} from 'vscode' ;
1517import { LanguageClient , RequestType } from 'vscode-languageclient' ;
1618import Window = vscode . window ;
@@ -95,6 +97,10 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
9597 // hence we keep this as an option but set it true by default.
9698 private aggregateUndoStop : boolean ;
9799
100+ private get emptyPromise ( ) : Promise < TextEdit [ ] > {
101+ return Promise . resolve ( TextEdit [ 0 ] ) ;
102+ }
103+
98104 constructor ( aggregateUndoStop = true ) {
99105 this . aggregateUndoStop = aggregateUndoStop ;
100106 }
@@ -112,17 +118,28 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
112118 options : FormattingOptions ,
113119 token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
114120
121+ let editor : TextEditor = this . getEditor ( document ) ;
122+ if ( editor === undefined ) {
123+ Window . showWarningMessage ( `Something went wrong. Cannot format ${ path . basename ( document . fileName ) } ` ) ;
124+ return this . emptyPromise ;
125+ }
126+
115127 if ( this . isDocumentLocked ( document ) ) {
116- return ;
128+ Window . showWarningMessage ( `Formatting ${ path . basename ( document . fileName ) } . Please wait.` ) ;
129+ return this . emptyPromise ;
117130 }
118131
119132 this . lockDocument ( document ) ;
120- let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( document , range , options , 0 ) ;
133+ let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( editor , range , options , 0 ) ;
121134 this . releaseDocument ( document , textEdits ) ;
122135 AnimatedStatusBar . showAnimatedStatusBarMessage ( "Formatting PowerShell document" , textEdits ) ;
123136 return textEdits ;
124137 }
125138
139+ getEditor ( document : TextDocument ) : TextEditor {
140+ return Window . visibleTextEditors . find ( ( e , n , obj ) => { return e . document === document ; } ) ;
141+ }
142+
126143 isDocumentLocked ( document : TextDocument ) : boolean {
127144 if ( PSDocumentFormattingEditProvider . filesBeingFormatted . hasOwnProperty ( this . getDocumentKey ( document ) ) ) {
128145 return true ;
@@ -150,13 +167,14 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
150167 }
151168
152169 executeRulesInOrder (
153- document : TextDocument ,
170+ editor : TextEditor ,
154171 range : Range ,
155172 options : FormattingOptions ,
156173 index : number ) : Thenable < TextEdit [ ] > {
157174 if ( this . languageClient !== null && index < this . ruleOrder . length ) {
158- let rule = this . ruleOrder [ index ] ;
175+ let rule : string = this . ruleOrder [ index ] ;
159176 let uniqueEdits : ScriptRegion [ ] = [ ] ;
177+ let document : TextDocument = editor . document ;
160178 let edits : ScriptRegion [ ] ;
161179
162180 return this . languageClient . sendRequest (
@@ -197,22 +215,29 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
197215 // we do not return a valid array because our text edits
198216 // need to be executed in a particular order and it is
199217 // easier if we perform the edits ourselves
200- return this . applyEdit ( uniqueEdits , range , 0 , index ) ;
218+ return this . applyEdit ( editor , uniqueEdits , range , 0 , index ) ;
201219 } )
202220 . then ( ( ) => {
203221 // execute the same rule again if we left out violations
204222 // on the same line
223+ let newIndex : number = index + 1 ;
205224 if ( uniqueEdits . length !== edits . length ) {
206- return this . executeRulesInOrder ( document , range , options , index ) ;
225+ newIndex = index ;
207226 }
208- return this . executeRulesInOrder ( document , range , options , index + 1 ) ;
227+
228+ return this . executeRulesInOrder ( editor , range , options , newIndex ) ;
209229 } ) ;
210230 } else {
211- return Promise . resolve ( TextEdit [ 0 ] ) ;
231+ return this . emptyPromise ;
212232 }
213233 }
214234
215- applyEdit ( edits : ScriptRegion [ ] , range : Range , markerIndex : number , ruleIndex : number ) : Thenable < void > {
235+ applyEdit (
236+ editor : TextEditor ,
237+ edits : ScriptRegion [ ] ,
238+ range : Range ,
239+ markerIndex : number ,
240+ ruleIndex : number ) : Thenable < void > {
216241 if ( markerIndex >= edits . length ) {
217242 return ;
218243 }
@@ -226,7 +251,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
226251 edit . endLineNumber - 1 ,
227252 edit . endColumnNumber - 1 ) ;
228253 if ( range === null || range . contains ( editRange ) ) {
229- return Window . activeTextEditor . edit ( ( editBuilder ) => {
254+ return editor . edit ( ( editBuilder ) => {
230255 editBuilder . replace (
231256 editRange ,
232257 edit . text ) ;
@@ -235,11 +260,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
235260 undoStopAfter : undoStopAfter ,
236261 undoStopBefore : undoStopBefore
237262 } ) . then ( ( isEditApplied ) => {
238- return this . applyEdit ( edits , range , markerIndex + 1 , ruleIndex ) ;
263+ return this . applyEdit ( editor , edits , range , markerIndex + 1 , ruleIndex ) ;
239264 } ) ; // TODO handle rejection
240265 }
241266 else {
242- return this . applyEdit ( edits , range , markerIndex + 1 , ruleIndex ) ;
267+ return this . applyEdit ( editor , edits , range , markerIndex + 1 , ruleIndex ) ;
243268 }
244269 }
245270
0 commit comments