@@ -43,7 +43,7 @@ module ts.formatting {
43
43
var indentation : number ;
44
44
45
45
while ( current ) {
46
- if ( isPositionBelongToNode ( current , position , sourceFile ) && isNodeContentIndented ( current , previous ) ) {
46
+ if ( positionBelongsToNode ( current , position , sourceFile ) && nodeContentIsIndented ( current , previous ) ) {
47
47
currentStart = getStartLineAndCharacterForNode ( current , sourceFile ) ;
48
48
49
49
if ( discardInitialIndentationIfNextTokenIsOpenOrCloseBrace ( precedingToken , current , lineAtPosition , sourceFile ) ) {
@@ -88,7 +88,7 @@ module ts.formatting {
88
88
parentStart = sourceFile . getLineAndCharacterFromPosition ( parent . getStart ( sourceFile ) ) ;
89
89
var parentAndChildShareLine =
90
90
parentStart . line === currentStart . line ||
91
- isChildStartsOnTheSameLineWithElseInIfStatement ( parent , current , currentStart . line , sourceFile ) ;
91
+ childStartsOnTheSameLineWithElseInIfStatement ( parent , current , currentStart . line , sourceFile ) ;
92
92
93
93
// try to fetch actual indentation for current node from source text
94
94
var actualIndentation = getActualIndentationForNode ( current , parent , currentStart , parentAndChildShareLine , sourceFile , options ) ;
@@ -97,7 +97,7 @@ module ts.formatting {
97
97
}
98
98
99
99
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
100
- var increaseIndentation = isNodeContentIndented ( parent , current ) && ! parentAndChildShareLine ;
100
+ var increaseIndentation = nodeContentIsIndented ( parent , current ) && ! parentAndChildShareLine ;
101
101
102
102
if ( increaseIndentation ) {
103
103
indentation += options . indentSpaces ;
@@ -153,6 +153,7 @@ module ts.formatting {
153
153
}
154
154
}
155
155
156
+ // function returns -1 if indentation cannot be determined
156
157
function getActualIndentationForListItemBeforeComma ( commaToken : Node , sourceFile : SourceFile , options : TypeScript . FormattingOptions ) : number {
157
158
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
158
159
var precedingListItem = findPrecedingListItem ( commaToken ) ;
@@ -166,6 +167,7 @@ module ts.formatting {
166
167
return - 1 ;
167
168
}
168
169
170
+ // function returns -1 if actual indentation for node should not be used (i.e because node is nested expression)
169
171
function getActualIndentationForNode ( current : Node , parent : Node , currentLineAndChar : LineAndCharacter , parentAndChildShareLine : boolean , sourceFile : SourceFile , options : TypeScript . FormattingOptions ) : number {
170
172
var useActualIndentation =
171
173
( isDeclaration ( current ) || isStatement ( current ) ) &&
@@ -227,11 +229,11 @@ module ts.formatting {
227
229
return children [ commaIndex - 1 ] ;
228
230
}
229
231
230
- function isPositionBelongToNode ( candidate : Node , position : number , sourceFile : SourceFile ) : boolean {
232
+ function positionBelongsToNode ( candidate : Node , position : number , sourceFile : SourceFile ) : boolean {
231
233
return candidate . end > position || ! isCompletedNode ( candidate , sourceFile ) ;
232
234
}
233
235
234
- function isChildStartsOnTheSameLineWithElseInIfStatement ( parent : Node , child : Node , childStartLine : number , sourceFile : SourceFile ) : boolean {
236
+ function childStartsOnTheSameLineWithElseInIfStatement ( parent : Node , child : Node , childStartLine : number , sourceFile : SourceFile ) : boolean {
235
237
if ( parent . kind === SyntaxKind . IfStatement && ( < IfStatement > parent ) . elseStatement === child ) {
236
238
var elseKeyword = forEach ( parent . getChildren ( ) , c => c . kind === SyntaxKind . ElseKeyword && c ) ;
237
239
Debug . assert ( elseKeyword ) ;
@@ -404,10 +406,10 @@ module ts.formatting {
404
406
}
405
407
406
408
function isToken ( n : Node ) : boolean {
407
- return n . kind < SyntaxKind . Missing ;
409
+ return n . kind >= SyntaxKind . FirstToken && n . kind <= SyntaxKind . LastToken ;
408
410
}
409
411
410
- function isNodeContentIndented ( parent : Node , child : Node ) : boolean {
412
+ function nodeContentIsIndented ( parent : Node , child : Node ) : boolean {
411
413
switch ( parent . kind ) {
412
414
case SyntaxKind . ClassDeclaration :
413
415
case SyntaxKind . InterfaceDeclaration :
@@ -456,7 +458,7 @@ module ts.formatting {
456
458
457
459
/// checks if node ends with 'expectedLastToken'.
458
460
/// If child at position 'length - 1' is 'SemicolonToken' it is skipped and 'expectedLastToken' is compared with child at position 'length - 2'.
459
- function isNodeEndWith ( n : Node , expectedLastToken : SyntaxKind , sourceFile : SourceFile ) : boolean {
461
+ function nodeEndsWith ( n : Node , expectedLastToken : SyntaxKind , sourceFile : SourceFile ) : boolean {
460
462
var children = n . getChildren ( sourceFile ) ;
461
463
if ( children . length ) {
462
464
var last = children [ children . length - 1 ] ;
@@ -470,7 +472,7 @@ module ts.formatting {
470
472
return false ;
471
473
}
472
474
473
- // this function is alwasy called when position of the cursor is located after the node
475
+ // this function is always called when position of the cursor is located after the node
474
476
function isCompletedNode ( n : Node , sourceFile : SourceFile ) : boolean {
475
477
switch ( n . kind ) {
476
478
case SyntaxKind . ClassDeclaration :
@@ -483,11 +485,11 @@ module ts.formatting {
483
485
case SyntaxKind . FunctionBlock :
484
486
case SyntaxKind . ModuleBlock :
485
487
case SyntaxKind . SwitchStatement :
486
- return isNodeEndWith ( n , SyntaxKind . CloseBraceToken , sourceFile ) ;
488
+ return nodeEndsWith ( n , SyntaxKind . CloseBraceToken , sourceFile ) ;
487
489
case SyntaxKind . ParenExpression :
488
490
case SyntaxKind . CallSignature :
489
491
case SyntaxKind . CallExpression :
490
- return isNodeEndWith ( n , SyntaxKind . CloseParenToken , sourceFile ) ;
492
+ return nodeEndsWith ( n , SyntaxKind . CloseParenToken , sourceFile ) ;
491
493
case SyntaxKind . FunctionDeclaration :
492
494
case SyntaxKind . FunctionExpression :
493
495
case SyntaxKind . Method :
@@ -503,7 +505,7 @@ module ts.formatting {
503
505
case SyntaxKind . ExpressionStatement :
504
506
return isCompletedNode ( ( < ExpressionStatement > n ) . expression , sourceFile ) ;
505
507
case SyntaxKind . ArrayLiteral :
506
- return isNodeEndWith ( n , SyntaxKind . CloseBracketToken , sourceFile ) ;
508
+ return nodeEndsWith ( n , SyntaxKind . CloseBracketToken , sourceFile ) ;
507
509
case SyntaxKind . Missing :
508
510
return false ;
509
511
case SyntaxKind . CaseClause :
@@ -516,8 +518,8 @@ module ts.formatting {
516
518
// rough approximation: if DoStatement has While keyword - then if node is completed is checking the presence of ')';
517
519
var hasWhileKeyword = forEach ( n . getChildren ( ) , c => c . kind === SyntaxKind . WhileKeyword && c ) ;
518
520
if ( hasWhileKeyword ) {
519
- return isNodeEndWith ( n , SyntaxKind . CloseParenToken , sourceFile ) ;
520
- }
521
+ return nodeEndsWith ( n , SyntaxKind . CloseParenToken , sourceFile ) ;
522
+ }
521
523
return isCompletedNode ( ( < DoStatement > n ) . statement , sourceFile ) ;
522
524
default :
523
525
return true ;
0 commit comments