@@ -29,7 +29,7 @@ module ts.formatting {
29
29
var listStartLine = getStartLineForNode ( precedingListItem . parent , sourceFile ) ;
30
30
31
31
if ( precedingListItemStartLineAndChar . line !== listStartLine ) {
32
-
32
+ return findFirstNonWhitespaceCharacterInLine ( precedingListItemStartLineAndChar . line , precedingListItemStartLineAndChar . character , sourceFile ) ;
33
33
// previous list item starts on the different line with list, find first non-whitespace character in this line and use its position as indentation
34
34
var lineStartPosition = sourceFile . getPositionFromLineAndCharacter ( precedingListItemStartLineAndChar . line , 1 ) ;
35
35
for ( var i = 0 ; i < precedingListItemStartLineAndChar . character ; ++ i ) {
@@ -52,18 +52,13 @@ module ts.formatting {
52
52
53
53
while ( current ) {
54
54
if ( isPositionBelongToNode ( current , position , sourceFile ) ) {
55
-
56
55
currentStartLine = getStartLineForNode ( current , sourceFile ) ;
57
56
58
57
if ( discardInitialIndentationIfNextTokenIsOpenOrCloseBrace ( precedingToken , current , lineAtPosition , sourceFile ) ) {
59
58
indentation = 0 ;
60
59
}
61
60
else {
62
- indentation =
63
- isNodeContentIndented ( current , previous ) &&
64
- lineAtPosition !== currentStartLine
65
- ? options . indentSpaces
66
- : 0 ;
61
+ indentation = isNodeContentIndented ( current , previous ) && lineAtPosition !== currentStartLine ? options . indentSpaces : 0 ;
67
62
}
68
63
69
64
break ;
@@ -73,6 +68,12 @@ module ts.formatting {
73
68
return customIndentation ;
74
69
}
75
70
71
+ // check if current node is a list item - if yes, take indentation from it
72
+ var customIndentation = getCustomIndentationForListItem ( current , sourceFile ) ;
73
+ if ( customIndentation !== - 1 ) {
74
+ return customIndentation ;
75
+ }
76
+
76
77
previous = current ;
77
78
current = current . parent ;
78
79
}
@@ -89,12 +90,15 @@ module ts.formatting {
89
90
// walk upwards and collect indentations for pairs of parent-child nodes
90
91
// indentation is not added if parent and child nodes start on the same line or if parent is IfStatement and child starts on the same line with 'else clause'
91
92
while ( parent ) {
93
+
94
+ // check if current node is a list item - if yes, take indentation from it
92
95
var customIndentation = getCustomIndentationForListItem ( current , sourceFile ) ;
93
96
if ( customIndentation !== - 1 ) {
94
97
return customIndentation + indentation ;
95
98
}
96
99
97
100
parentStartLine = sourceFile . getLineAndCharacterFromPosition ( parent . getStart ( sourceFile ) ) . line ;
101
+ // increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
98
102
var increaseIndentation =
99
103
isNodeContentIndented ( parent , current ) &&
100
104
parentStartLine !== currentStartLine &&
@@ -218,16 +222,7 @@ module ts.formatting {
218
222
for ( var i = index - 1 ; i >= 0 ; -- i ) {
219
223
var prevLineAndCol = sourceFile . getLineAndCharacterFromPosition ( list [ i ] . getStart ( sourceFile ) ) ;
220
224
if ( lineAndCol . line !== prevLineAndCol . line ) {
221
- // find the line start position
222
- var lineStart = sourceFile . getPositionFromLineAndCharacter ( lineAndCol . line , 1 ) ;
223
- for ( var i = 0 ; i <= lineAndCol . character ; ++ i ) {
224
- if ( ! isWhiteSpace ( sourceFile . text . charCodeAt ( lineStart + i ) ) ) {
225
- return i ;
226
- }
227
- }
228
- // code is unreachable because the range that we check above includes at least one non-whitespace character at the very end
229
- Debug . fail ( "Unreachable code" )
230
-
225
+ return findFirstNonWhitespaceCharacterInLine ( lineAndCol . line , lineAndCol . character , sourceFile ) ;
231
226
}
232
227
lineAndCol = prevLineAndCol ;
233
228
}
@@ -236,6 +231,17 @@ module ts.formatting {
236
231
}
237
232
}
238
233
234
+ function findFirstNonWhitespaceCharacterInLine ( line : number , maxCharacter : number , sourceFile : SourceFile ) : number {
235
+ var lineStart = sourceFile . getPositionFromLineAndCharacter ( line , 1 ) ;
236
+ for ( var i = 0 ; i < maxCharacter ; ++ i ) {
237
+ if ( ! isWhiteSpace ( sourceFile . text . charCodeAt ( lineStart + i ) ) ) {
238
+ return i ;
239
+ }
240
+ }
241
+
242
+ return maxCharacter ;
243
+ }
244
+
239
245
function findNextToken ( previousToken : Node , parent : Node ) : Node {
240
246
return find ( parent ) ;
241
247
0 commit comments