@@ -34,7 +34,7 @@ namespace ts.formatting {
34
34
* the first token in line so it should be indented
35
35
*/
36
36
interface DynamicIndentation {
37
- getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind , container : Node ) : number ;
37
+ getIndentationForToken ( tokenLine : number , tokenKind : SyntaxKind , container : Node , suppressDelta : boolean ) : number ;
38
38
getIndentationForComment ( owningToken : SyntaxKind , tokenIndentation : number , container : Node ) : number ;
39
39
/**
40
40
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
@@ -398,7 +398,7 @@ namespace ts.formatting {
398
398
let previousRangeStartLine : number ;
399
399
400
400
let lastIndentedLine : number ;
401
- let indentationOnLastIndentedLine : number ;
401
+ let indentationOnLastIndentedLine = Constants . Unknown ;
402
402
403
403
const edits : TextChange [ ] = [ ] ;
404
404
@@ -539,8 +539,18 @@ namespace ts.formatting {
539
539
}
540
540
return tokenIndentation !== Constants . Unknown ? tokenIndentation : indentation ;
541
541
} ,
542
- getIndentationForToken : ( line , kind , container ) =>
543
- shouldAddDelta ( line , kind , container ) ? indentation + getDelta ( container ) : indentation ,
542
+ // if list end token is LessThanToken '>' then its delta should be explicitly suppressed
543
+ // so that LessThanToken as a binary operator can still be indented.
544
+ // foo.then
545
+ // <
546
+ // number,
547
+ // string,
548
+ // >();
549
+ // vs
550
+ // var a = xValue
551
+ // > yValue;
552
+ getIndentationForToken : ( line , kind , container , suppressDelta ) =>
553
+ ! suppressDelta && shouldAddDelta ( line , kind , container ) ? indentation + getDelta ( container ) : indentation ,
544
554
getIndentation : ( ) => indentation ,
545
555
getDelta,
546
556
recomputeIndentation : lineAdded => {
@@ -556,7 +566,6 @@ namespace ts.formatting {
556
566
// open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent
557
567
case SyntaxKind . OpenBraceToken :
558
568
case SyntaxKind . CloseBraceToken :
559
- case SyntaxKind . OpenParenToken :
560
569
case SyntaxKind . CloseParenToken :
561
570
case SyntaxKind . ElseKeyword :
562
571
case SyntaxKind . WhileKeyword :
@@ -737,11 +746,23 @@ namespace ts.formatting {
737
746
else if ( tokenInfo . token . kind === listStartToken ) {
738
747
// consume list start token
739
748
startLine = sourceFile . getLineAndCharacterOfPosition ( tokenInfo . token . pos ) . line ;
740
- const indentation =
741
- computeIndentation ( tokenInfo . token , startLine , Constants . Unknown , parent , parentDynamicIndentation , parentStartLine ) ;
742
749
743
- listDynamicIndentation = getDynamicIndentation ( parent , parentStartLine , indentation . indentation , indentation . delta ) ;
744
- consumeTokenAndAdvanceScanner ( tokenInfo , parent , listDynamicIndentation , parent ) ;
750
+ consumeTokenAndAdvanceScanner ( tokenInfo , parent , parentDynamicIndentation , parent ) ;
751
+
752
+ let indentationOnListStartToken : number ;
753
+ if ( indentationOnLastIndentedLine !== Constants . Unknown ) {
754
+ // scanner just processed list start token so consider last indentation as list indentation
755
+ // function foo(): { // last indentation was 0, list item will be indented based on this value
756
+ // foo: number;
757
+ // }: {};
758
+ indentationOnListStartToken = indentationOnLastIndentedLine ;
759
+ }
760
+ else {
761
+ const startLinePosition = getLineStartPositionForPosition ( tokenInfo . token . pos , sourceFile ) ;
762
+ indentationOnListStartToken = SmartIndenter . findFirstNonWhitespaceColumn ( startLinePosition , tokenInfo . token . pos , sourceFile , options ) ;
763
+ }
764
+
765
+ listDynamicIndentation = getDynamicIndentation ( parent , parentStartLine , indentationOnListStartToken , options . indentSize ! ) ; // TODO: GH#18217
745
766
}
746
767
else {
747
768
// consume any tokens that precede the list as child elements of 'node' using its indentation scope
@@ -770,12 +791,12 @@ namespace ts.formatting {
770
791
// without this check close paren will be interpreted as list end token for function expression which is wrong
771
792
if ( tokenInfo && tokenInfo . token . kind === listEndToken && rangeContainsRange ( parent , tokenInfo . token ) ) {
772
793
// consume list end token
773
- consumeTokenAndAdvanceScanner ( tokenInfo , parent , listDynamicIndentation , parent ) ;
794
+ consumeTokenAndAdvanceScanner ( tokenInfo , parent , listDynamicIndentation , parent , /*isListEndToken*/ true ) ;
774
795
}
775
796
}
776
797
}
777
798
778
- function consumeTokenAndAdvanceScanner ( currentTokenInfo : TokenInfo , parent : Node , dynamicIndentation : DynamicIndentation , container : Node ) : void {
799
+ function consumeTokenAndAdvanceScanner ( currentTokenInfo : TokenInfo , parent : Node , dynamicIndentation : DynamicIndentation , container : Node , isListEndToken ?: boolean ) : void {
779
800
Debug . assert ( rangeContainsRange ( parent , currentTokenInfo . token ) ) ;
780
801
781
802
const lastTriviaWasNewLine = formattingScanner . lastTrailingTriviaWasNewLine ( ) ;
@@ -813,7 +834,7 @@ namespace ts.formatting {
813
834
814
835
if ( indentToken ) {
815
836
const tokenIndentation = ( isTokenInRange && ! rangeContainsError ( currentTokenInfo . token ) ) ?
816
- dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind , container ) :
837
+ dynamicIndentation . getIndentationForToken ( tokenStart . line , currentTokenInfo . token . kind , container , ! ! isListEndToken ) :
817
838
Constants . Unknown ;
818
839
819
840
let indentNextTokenOrTrivia = true ;
@@ -1227,6 +1248,9 @@ namespace ts.formatting {
1227
1248
if ( ( < TypeReferenceNode > node ) . typeArguments === list ) {
1228
1249
return SyntaxKind . LessThanToken ;
1229
1250
}
1251
+ break ;
1252
+ case SyntaxKind . TypeLiteral :
1253
+ return SyntaxKind . OpenBraceToken ;
1230
1254
}
1231
1255
1232
1256
return SyntaxKind . Unknown ;
@@ -1238,6 +1262,8 @@ namespace ts.formatting {
1238
1262
return SyntaxKind . CloseParenToken ;
1239
1263
case SyntaxKind . LessThanToken :
1240
1264
return SyntaxKind . GreaterThanToken ;
1265
+ case SyntaxKind . OpenBraceToken :
1266
+ return SyntaxKind . CloseBraceToken ;
1241
1267
}
1242
1268
1243
1269
return SyntaxKind . Unknown ;
0 commit comments