@@ -20,12 +20,10 @@ module ts.formatting {
2020 }
2121
2222 interface DynamicIndentation {
23- getEffectiveIndentation ( tokenLine : number , kind : SyntaxKind ) : number ;
24- getEffectiveCommentIndentation ( commentLine : number ) : number ;
25- getDelta ( ) : number ;
23+ getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind ) : number ;
24+ getIndentationForComment ( owningToken : SyntaxKind ) : number ;
2625 getIndentation ( ) : number ;
27- getCommentIndentation ( ) : number ;
28- increaseCommentIndentation ( delta : number ) : void ;
26+ getDelta ( ) : number ;
2927 recomputeIndentation ( lineAddedByFormatting : boolean ) : void ;
3028 }
3129
@@ -128,42 +126,41 @@ module ts.formatting {
128126 if ( ! errors . length ) {
129127 return rangeHasNoErrors ;
130128 }
131- else {
132- // pick only errors that fall in range
133- var sorted = errors
134- . filter ( d => d . isParseError && ( d . start >= originalRange . pos && d . start + d . length < originalRange . end ) )
135- . sort ( ( e1 , e2 ) => e1 . start - e2 . start ) ;
136-
137- if ( ! sorted . length ) {
138- return rangeHasNoErrors ;
139- }
129+
130+ // pick only errors that fall in range
131+ var sorted = errors
132+ . filter ( d => d . isParseError && ( d . start >= originalRange . pos && d . start + d . length < originalRange . end ) )
133+ . sort ( ( e1 , e2 ) => e1 . start - e2 . start ) ;
140134
141- var index = 0 ;
142- return r => {
143- while ( true ) {
144- if ( index >= sorted . length ) {
145- return false ;
146- }
147- else {
148- var curr = sorted [ index ] ;
149- if ( r . end <= curr . start ) {
150- return false ;
151- }
152- else {
153- var s = Math . max ( r . pos , curr . start ) ;
154- var e = Math . min ( r . end , curr . start + curr . length ) ;
155- if ( s < e ) {
156- return true ;
157- }
158- index ++ ;
159- }
160- }
135+ if ( ! sorted . length ) {
136+ return rangeHasNoErrors ;
137+ }
138+
139+ var index = 0 ;
140+
141+ return r => {
142+ while ( true ) {
143+ if ( index >= sorted . length ) {
144+ return false ;
145+ }
146+
147+ var error = sorted [ index ] ;
148+ if ( r . end <= error . start ) {
149+ return false ;
150+ }
151+
152+ var s = Math . max ( r . pos , error . start ) ;
153+ var e = Math . min ( r . end , error . start + error . length ) ;
154+ if ( s < e ) {
155+ return true ;
161156 }
162- } ;
163157
164- function rangeHasNoErrors ( r : TextRange ) : boolean {
165- return false ;
158+ index ++ ;
166159 }
160+ } ;
161+
162+ function rangeHasNoErrors ( r : TextRange ) : boolean {
163+ return false ;
167164 }
168165 }
169166
@@ -205,16 +202,23 @@ module ts.formatting {
205202
206203 // local functions
207204
208- function getDynamicIndentation ( node : Node , nodeStartLine : number , indentation : number , commentIndentation : number , delta : number ) : DynamicIndentation {
205+ function getDynamicIndentation ( node : Node , nodeStartLine : number , indentation : number , delta : number ) : DynamicIndentation {
209206 return {
210- getEffectiveCommentIndentation : ( line ) => {
211- if ( nodeStartLine !== line ) {
212- return commentIndentation + delta ;
207+ getIndentationForComment : ( kind ) => {
208+ switch ( kind ) {
209+ // preceding comment to the token that closes the indentation scope inherits the indentation from the scope
210+ // .. {
211+ // // comment
212+ // }
213+ case SyntaxKind . CloseBraceToken :
214+ case SyntaxKind . CloseBracketToken :
215+ return indentation + delta ;
213216 }
214- return commentIndentation ;
217+ return indentation ;
215218 } ,
216- getEffectiveIndentation : ( line , kind ) => {
219+ getIndentationForToken : ( line , kind ) => {
217220 switch ( kind ) {
221+ // open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent
218222 case SyntaxKind . OpenBraceToken :
219223 case SyntaxKind . CloseBraceToken :
220224 case SyntaxKind . OpenBracketToken :
@@ -224,21 +228,17 @@ module ts.formatting {
224228 return indentation ;
225229 default :
226230 return nodeStartLine !== line ? indentation + delta : indentation ;
227-
228231 }
229232 } ,
230233 getIndentation : ( ) => indentation ,
231- getCommentIndentation : ( ) => commentIndentation ,
232234 getDelta : ( ) => delta ,
233235 recomputeIndentation : ( lineAdded ) => {
234236 if ( node . parent && SmartIndenter . shouldIndentChildNode ( node . parent , node ) ) {
235237 if ( lineAdded ) {
236238 indentation += options . IndentSize ;
237- commentIndentation += options . IndentSize ;
238239 }
239240 else {
240241 indentation -= options . IndentSize ;
241- commentIndentation -= options . IndentSize ;
242242 }
243243 if ( shouldIndentChildNodes ( node . kind ) ) {
244244 delta = options . IndentSize ;
@@ -248,9 +248,6 @@ module ts.formatting {
248248 }
249249 }
250250 } ,
251- increaseCommentIndentation : ( delta ) => {
252- commentIndentation += delta ;
253- } ,
254251 }
255252 }
256253
@@ -259,7 +256,7 @@ module ts.formatting {
259256 return ;
260257 }
261258
262- var nodeIndentation = getDynamicIndentation ( node , nodeStartLine , indentation , indentation , delta ) ;
259+ var nodeIndentation = getDynamicIndentation ( node , nodeStartLine , indentation , delta ) ;
263260
264261 var childContextNode = contextNode ;
265262 forEachChild (
@@ -283,8 +280,7 @@ module ts.formatting {
283280 else if ( tokenInfo . token . kind === listStartToken ) {
284281 var tokenStartLine = sourceFile . getLineAndCharacterFromPosition ( tokenInfo . token . pos ) . line ;
285282 var indentation = computeIndentation ( tokenInfo . token , tokenStartLine , Indentation . Unknown , node , nodeStartLine ) ;
286- listIndentation = getDynamicIndentation ( node , nodeStartLine , indentation . indentation , indentation . indentation , indentation . delta ) ;
287- // make sure that this token does not belong to the child
283+ listIndentation = getDynamicIndentation ( node , nodeStartLine , indentation . indentation , indentation . delta ) ;
288284 consumeTokenAndAdvanceScanner ( tokenInfo , node , listIndentation ) ;
289285 }
290286 else {
@@ -315,10 +311,6 @@ module ts.formatting {
315311 if ( tokenInfo . token . end > node . end ) {
316312 break ;
317313 }
318-
319- if ( SmartIndenter . nodeContentIsAlwaysIndented ( node . kind ) && node . end === tokenInfo . token . end ) {
320- nodeIndentation . increaseCommentIndentation ( options . IndentSize ) ;
321- }
322314 consumeTokenAndAdvanceScanner ( tokenInfo , node , nodeIndentation ) ;
323315 }
324316
@@ -400,7 +392,7 @@ module ts.formatting {
400392
401393 var delta = 0 ;
402394 if ( indentation === Indentation . Unknown ) {
403- if ( isSomeBlock ( node . kind ) ) {
395+ if ( isSomeBlock ( node . kind ) ) {
404396 delta = options . IndentSize ;
405397 if ( isSomeBlock ( parent . kind ) ||
406398 parent . kind === SyntaxKind . SourceFile ||
@@ -466,34 +458,36 @@ module ts.formatting {
466458 processTrivia ( currentTokenInfo . trailingTrivia , parent , childContextNode , indentation ) ;
467459 }
468460
469-
470461 if ( lastTriviaWasNewLine && indentToken ) {
471462 var indentNextTokenOrTrivia = true ;
472463 if ( currentTokenInfo . leadingTrivia ) {
473464 for ( var i = 0 , len = currentTokenInfo . leadingTrivia . length ; i < len ; ++ i ) {
474465 var triviaItem = currentTokenInfo . leadingTrivia [ i ] ;
466+ if ( ! rangeContainsRange ( originalRange , triviaItem ) ) {
467+ continue ;
468+ }
469+
475470 var triviaStartLine = sourceFile . getLineAndCharacterFromPosition ( triviaItem . pos ) . line ;
476- if ( rangeContainsRange ( originalRange , triviaItem ) ) {
477- switch ( triviaItem . kind ) {
478- case SyntaxKind . MultiLineCommentTrivia :
479- indentMultilineComment ( triviaItem , indentation . getCommentIndentation ( ) , /*firstLineIsIndented*/ ! indentNextTokenOrTrivia ) ;
471+ switch ( triviaItem . kind ) {
472+ case SyntaxKind . MultiLineCommentTrivia :
473+ indentMultilineComment ( triviaItem , indentation . getIndentationForComment ( currentTokenInfo . token . kind ) , /*firstLineIsIndented*/ ! indentNextTokenOrTrivia ) ;
474+ indentNextTokenOrTrivia = false ;
475+ break ;
476+ case SyntaxKind . SingleLineCommentTrivia :
477+ if ( indentNextTokenOrTrivia ) {
478+ insertIndentation ( triviaItem . pos , indentation . getIndentationForComment ( currentTokenInfo . token . kind ) , /*lineAdded*/ false ) ;
480479 indentNextTokenOrTrivia = false ;
481- break ;
482- case SyntaxKind . SingleLineCommentTrivia :
483- if ( indentNextTokenOrTrivia ) {
484- insertIndentation ( triviaItem . pos , indentation . getCommentIndentation ( ) , /*lineAdded*/ false ) ;
485- indentNextTokenOrTrivia = false ;
486- }
487- break ;
488- case SyntaxKind . NewLineTrivia :
489- indentNextTokenOrTrivia = true ;
490- break ;
491- }
480+ }
481+ break ;
482+ case SyntaxKind . NewLineTrivia :
483+ indentNextTokenOrTrivia = true ;
484+ break ;
492485 }
493486 }
494487 }
488+
495489 if ( isTokenInRange && ! rangeContainsError ( currentTokenInfo . token ) ) {
496- insertIndentation ( currentTokenInfo . token . pos , indentation . getEffectiveIndentation ( tokenStart . line , currentTokenInfo . token . kind ) , lineAdded ) ;
490+ insertIndentation ( currentTokenInfo . token . pos , indentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind ) , lineAdded ) ;
497491 }
498492 }
499493
0 commit comments