@@ -31,8 +31,8 @@ namespace ts.formatting {
3131 * the first token in line so it should be indented
3232 */
3333 interface DynamicIndentation {
34- getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind ) : number ;
35- getIndentationForComment ( owningToken : SyntaxKind , tokenIndentation : number ) : number ;
34+ getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind , container : Node ) : number ;
35+ getIndentationForComment ( owningToken : SyntaxKind , tokenIndentation : number , container : Node ) : number ;
3636 /**
3737 * Indentation for open and close tokens of the node if it is block or another node that needs special indentation
3838 * ... {
@@ -60,11 +60,6 @@ namespace ts.formatting {
6060 * so indentation scope can adjust values of indentation and delta.
6161 */
6262 recomputeIndentation ( lineAddedByFormatting : boolean ) : void ;
63-
64- /**
65- * Returns DynamicIndentation object that includes modified delta value for specific child node.
66- */
67- getCopyForSpecificChild ( child : Node ) : DynamicIndentation ;
6863 }
6964
7065 interface Indentation {
@@ -330,7 +325,7 @@ namespace ts.formatting {
330325
331326 let lastIndentedLine : number ;
332327 let indentationOnLastIndentedLine : number ;
333-
328+
334329 let edits : TextChange [ ] = [ ] ;
335330
336331 formattingScanner . advance ( ) ;
@@ -359,12 +354,12 @@ namespace ts.formatting {
359354 * If list element is in the range - its indentation will be equal
360355 * to inherited indentation from its predecessors.
361356 */
362- function tryComputeIndentationForListItem ( startPos : number ,
363- endPos : number ,
364- parentStartLine : number ,
365- range : TextRange ,
357+ function tryComputeIndentationForListItem ( startPos : number ,
358+ endPos : number ,
359+ parentStartLine : number ,
360+ range : TextRange ,
366361 inheritedIndentation : number ) : number {
367-
362+
368363 if ( rangeOverlapsWithStartEnd ( range , startPos , endPos ) ) {
369364 if ( inheritedIndentation !== Constants . Unknown ) {
370365 return inheritedIndentation ;
@@ -381,7 +376,7 @@ namespace ts.formatting {
381376
382377 return Constants . Unknown ;
383378 }
384-
379+
385380 function computeIndentation (
386381 node : TextRangeWithKind ,
387382 startLine : number ,
@@ -397,8 +392,8 @@ namespace ts.formatting {
397392 // if node is located on the same line with the parent
398393 // - inherit indentation from the parent
399394 // - push children if either parent of node itself has non-zero delta
400- indentation = startLine === lastIndentedLine
401- ? indentationOnLastIndentedLine
395+ indentation = startLine === lastIndentedLine
396+ ? indentationOnLastIndentedLine
402397 : parentDynamicIndentation . getIndentation ( ) ;
403398 delta = Math . min ( options . IndentSize , parentDynamicIndentation . getDelta ( node ) + delta ) ;
404399 }
@@ -442,7 +437,7 @@ namespace ts.formatting {
442437
443438 function getDynamicIndentation ( node : Node , nodeStartLine : number , indentation : number , delta : number ) : DynamicIndentation {
444439 return {
445- getIndentationForComment : ( kind , tokenIndentation ) => {
440+ getIndentationForComment : ( kind , tokenIndentation , container ) => {
446441 switch ( kind ) {
447442 // preceding comment to the token that closes the indentation scope inherits the indentation from the scope
448443 // .. {
@@ -451,11 +446,11 @@ namespace ts.formatting {
451446 case SyntaxKind . CloseBraceToken :
452447 case SyntaxKind . CloseBracketToken :
453448 case SyntaxKind . CloseParenToken :
454- return indentation + delta ;
449+ return indentation + getEffectiveDelta ( delta , container ) ;
455450 }
456451 return tokenIndentation !== Constants . Unknown ? tokenIndentation : indentation ;
457452 } ,
458- getIndentationForToken : ( line , kind ) => {
453+ getIndentationForToken : ( line , kind , container ) => {
459454 if ( nodeStartLine !== line && node . decorators ) {
460455 if ( kind === getFirstNonDecoratorTokenOfNode ( node ) ) {
461456 // if this token is the first token following the list of decorators, we do not need to indent
@@ -476,11 +471,11 @@ namespace ts.formatting {
476471 return indentation ;
477472 default :
478473 // if token line equals to the line of containing node (this is a first token in the node) - use node indentation
479- return nodeStartLine !== line ? indentation + delta : indentation ;
474+ return nodeStartLine !== line ? indentation + getEffectiveDelta ( delta , container ) : indentation ;
480475 }
481476 } ,
482477 getIndentation : ( ) => indentation ,
483- getDelta : ( child ) => ( delta && SmartIndenter . shouldInheritParentIndentation ( node , child ) ) ? 0 : delta ,
478+ getDelta : ( child ) => getEffectiveDelta ( delta , child ) ,
484479 recomputeIndentation : lineAdded => {
485480 if ( node . parent && SmartIndenter . shouldIndentChildNode ( node . parent , node ) ) {
486481 if ( lineAdded ) {
@@ -497,15 +492,12 @@ namespace ts.formatting {
497492 delta = 0 ;
498493 }
499494 }
500- } ,
501- getCopyForSpecificChild ( child ) {
502- if ( ! delta ) {
503- // delta value is already 0, so do not copy
504- return this ;
505- }
506- return getDynamicIndentation ( node , nodeStartLine , indentation , ( < DynamicIndentation > this ) . getDelta ( child ) ) ;
507495 }
508496 }
497+
498+ function getEffectiveDelta ( delta : number , child : TextRangeWithKind ) {
499+ return SmartIndenter . shouldInheritParentIndentation ( node , child ) ? 0 : delta ;
500+ }
509501 }
510502
511503 function processNode ( node : Node , contextNode : Node , nodeStartLine : number , undecoratedNodeStartLine : number , indentation : number , delta : number ) {
@@ -580,7 +572,7 @@ namespace ts.formatting {
580572 if ( ! rangeOverlapsWithStartEnd ( originalRange , child . pos , child . end ) ) {
581573 return inheritedIndentation ;
582574 }
583-
575+
584576 if ( child . getFullWidth ( ) === 0 ) {
585577 return inheritedIndentation ;
586578 }
@@ -604,7 +596,7 @@ namespace ts.formatting {
604596 // if child node is a token, it does not impact indentation, proceed it using parent indentation scope rules
605597 let tokenInfo = formattingScanner . readTokenInfo ( child ) ;
606598 Debug . assert ( tokenInfo . token . end === child . end ) ;
607- consumeTokenAndAdvanceScanner ( tokenInfo , node , parentDynamicIndentation . getCopyForSpecificChild ( child ) ) ;
599+ consumeTokenAndAdvanceScanner ( tokenInfo , node , parentDynamicIndentation , child ) ;
608600 return inheritedIndentation ;
609601 }
610602
@@ -618,8 +610,8 @@ namespace ts.formatting {
618610 return inheritedIndentation ;
619611 }
620612
621- function processChildNodes ( nodes : NodeArray < Node > ,
622- parent : Node ,
613+ function processChildNodes ( nodes : NodeArray < Node > ,
614+ parent : Node ,
623615 parentStartLine : number ,
624616 parentDynamicIndentation : DynamicIndentation ) : void {
625617
@@ -673,7 +665,7 @@ namespace ts.formatting {
673665 }
674666 }
675667
676- function consumeTokenAndAdvanceScanner ( currentTokenInfo : TokenInfo , parent : Node , dynamicIndentation : DynamicIndentation ) : void {
668+ function consumeTokenAndAdvanceScanner ( currentTokenInfo : TokenInfo , parent : Node , dynamicIndentation : DynamicIndentation , container ?: Node ) : void {
677669 Debug . assert ( rangeContainsRange ( parent , currentTokenInfo . token ) ) ;
678670
679671 let lastTriviaWasNewLine = formattingScanner . lastTrailingTriviaWasNewLine ( ) ;
@@ -712,11 +704,11 @@ namespace ts.formatting {
712704
713705 if ( indentToken ) {
714706 let tokenIndentation = ( isTokenInRange && ! rangeContainsError ( currentTokenInfo . token ) ) ?
715- dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind ) :
707+ dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind , container ) :
716708 Constants . Unknown ;
717709
718710 if ( currentTokenInfo . leadingTrivia ) {
719- let commentIndentation = dynamicIndentation . getIndentationForComment ( currentTokenInfo . token . kind , tokenIndentation ) ;
711+ let commentIndentation = dynamicIndentation . getIndentationForComment ( currentTokenInfo . token . kind , tokenIndentation , container ) ;
720712 let indentNextTokenOrTrivia = true ;
721713
722714 for ( let triviaItem of currentTokenInfo . leadingTrivia ) {
@@ -745,7 +737,7 @@ namespace ts.formatting {
745737 // indent token only if is it is in target range and does not overlap with any error ranges
746738 if ( tokenIndentation !== Constants . Unknown ) {
747739 insertIndentation ( currentTokenInfo . token . pos , tokenIndentation , lineAdded ) ;
748-
740+
749741 lastIndentedLine = tokenStart . line ;
750742 indentationOnLastIndentedLine = tokenIndentation ;
751743 }
@@ -766,12 +758,12 @@ namespace ts.formatting {
766758 }
767759 }
768760
769- function processRange ( range : TextRangeWithKind ,
770- rangeStart : LineAndCharacter ,
771- parent : Node ,
772- contextNode : Node ,
761+ function processRange ( range : TextRangeWithKind ,
762+ rangeStart : LineAndCharacter ,
763+ parent : Node ,
764+ contextNode : Node ,
773765 dynamicIndentation : DynamicIndentation ) : boolean {
774-
766+
775767 let rangeHasError = rangeContainsError ( range ) ;
776768 let lineAdded : boolean ;
777769 if ( ! rangeHasError && ! previousRangeHasError ) {
@@ -781,7 +773,7 @@ namespace ts.formatting {
781773 trimTrailingWhitespacesForLines ( originalStart . line , rangeStart . line ) ;
782774 }
783775 else {
784- lineAdded =
776+ lineAdded =
785777 processPair ( range , rangeStart . line , parent , previousRange , previousRangeStartLine , previousParent , contextNode , dynamicIndentation )
786778 }
787779 }
0 commit comments