@@ -32,7 +32,7 @@ namespace ts.formatting {
3232 */
3333 interface DynamicIndentation {
3434 getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind ) : number ;
35- getIndentationForComment ( owningToken : SyntaxKind ) : number ;
35+ getIndentationForComment ( owningToken : SyntaxKind , tokenIndentation : number ) : number ;
3636 /**
3737 * Indentation for open and close tokens of the node if it is block or another node that needs special indentation
3838 * ... {
@@ -455,17 +455,18 @@ namespace ts.formatting {
455455
456456 function getDynamicIndentation ( node : Node , nodeStartLine : number , indentation : number , delta : number ) : DynamicIndentation {
457457 return {
458- getIndentationForComment : kind => {
458+ getIndentationForComment : ( kind , tokenIndentation ) => {
459459 switch ( kind ) {
460460 // preceding comment to the token that closes the indentation scope inherits the indentation from the scope
461461 // .. {
462462 // // comment
463463 // }
464464 case SyntaxKind . CloseBraceToken :
465465 case SyntaxKind . CloseBracketToken :
466+ case SyntaxKind . CloseParenToken :
466467 return indentation + delta ;
467468 }
468- return indentation ;
469+ return tokenIndentation !== Constants . Unknown ? tokenIndentation : indentation ;
469470 } ,
470471 getIndentationForToken : ( line , kind ) => {
471472 if ( nodeStartLine !== line && node . decorators ) {
@@ -716,22 +717,26 @@ namespace ts.formatting {
716717 }
717718
718719 if ( indentToken ) {
719- let indentNextTokenOrTrivia = true ;
720+ let tokenIndentation = ( isTokenInRange && ! rangeContainsError ( currentTokenInfo . token ) ) ?
721+ dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind ) :
722+ Constants . Unknown ;
723+
720724 if ( currentTokenInfo . leadingTrivia ) {
725+ let commentIndentation = dynamicIndentation . getIndentationForComment ( currentTokenInfo . token . kind , tokenIndentation ) ;
726+ let indentNextTokenOrTrivia = true ;
727+
721728 for ( let triviaItem of currentTokenInfo . leadingTrivia ) {
722729 if ( ! rangeContainsRange ( originalRange , triviaItem ) ) {
723730 continue ;
724731 }
725732
726733 switch ( triviaItem . kind ) {
727734 case SyntaxKind . MultiLineCommentTrivia :
728- let commentIndentation = dynamicIndentation . getIndentationForComment ( currentTokenInfo . token . kind ) ;
729735 indentMultilineComment ( triviaItem , commentIndentation , /*firstLineIsIndented*/ ! indentNextTokenOrTrivia ) ;
730736 indentNextTokenOrTrivia = false ;
731737 break ;
732738 case SyntaxKind . SingleLineCommentTrivia :
733739 if ( indentNextTokenOrTrivia ) {
734- let commentIndentation = dynamicIndentation . getIndentationForComment ( currentTokenInfo . token . kind ) ;
735740 insertIndentation ( triviaItem . pos , commentIndentation , /*lineAdded*/ false ) ;
736741 indentNextTokenOrTrivia = false ;
737742 }
@@ -744,8 +749,7 @@ namespace ts.formatting {
744749 }
745750
746751 // indent token only if is it is in target range and does not overlap with any error ranges
747- if ( isTokenInRange && ! rangeContainsError ( currentTokenInfo . token ) ) {
748- let tokenIndentation = dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind ) ;
752+ if ( tokenIndentation !== Constants . Unknown ) {
749753 insertIndentation ( currentTokenInfo . token . pos , tokenIndentation , lineAdded ) ;
750754
751755 lastIndentedLine = tokenStart . line ;
0 commit comments