@@ -51,7 +51,7 @@ module ts.formatting {
51
51
var indentation : number ;
52
52
53
53
while ( current ) {
54
- if ( ! isToken ( current ) && isPositionBelongToNode ( current , position , sourceFile ) ) {
54
+ if ( isPositionBelongToNode ( current , position , sourceFile ) ) {
55
55
56
56
currentStartLine = getStartLineForNode ( current , sourceFile ) ;
57
57
@@ -68,6 +68,10 @@ module ts.formatting {
68
68
69
69
break ;
70
70
}
71
+ var customIndentation = getCustomIndentationForListItem ( current , sourceFile ) ;
72
+ if ( customIndentation !== - 1 ) {
73
+ return customIndentation ;
74
+ }
71
75
72
76
previous = current ;
73
77
current = current . parent ;
@@ -85,6 +89,11 @@ module ts.formatting {
85
89
// walk upwards and collect indentations for pairs of parent-child nodes
86
90
// 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'
87
91
while ( parent ) {
92
+ var customIndentation = getCustomIndentationForListItem ( current , sourceFile ) ;
93
+ if ( customIndentation !== - 1 ) {
94
+ return customIndentation + indentation ;
95
+ }
96
+
88
97
parentStartLine = sourceFile . getLineAndCharacterFromPosition ( parent . getStart ( sourceFile ) ) . line ;
89
98
var increaseIndentation =
90
99
isNodeContentIndented ( parent , current ) &&
@@ -166,45 +175,34 @@ module ts.formatting {
166
175
}
167
176
}
168
177
169
- // preserve indentation for list items
170
- // - first list item is either on the same line with the parent: foo(a... . (in this case it is not indented) or on the different line (then it is indented with base level + delta)
171
- // - subsequent list items inherit indentation for its sibling on the left when these siblings are also on the new line.
172
- // 1. foo(a, b
173
- // $ - indentation = base level + delta
174
- // 2. foo (a,
175
- // b, c, d,
176
- // $ - same indentation with first child node on the previous line
177
- // NOTE: indentation for list items spans from the beginning of the line to the first non-whitespace character
178
- // /*test*/ x,
179
- // $ <-- indentation for a new item will be here
180
- function getCustomIndentationForListItem ( leftSibling : Node , sourceFile : SourceFile ) : number {
181
- if ( leftSibling . parent ) {
182
- switch ( leftSibling . parent . kind ) {
178
+ function getCustomIndentationForListItem ( node : Node , sourceFile : SourceFile ) : number {
179
+ if ( node . parent ) {
180
+ switch ( node . parent . kind ) {
183
181
case SyntaxKind . ObjectLiteral :
184
- return getCustomIndentationFromList ( ( < ObjectLiteral > leftSibling . parent ) . properties ) ;
182
+ return getCustomIndentationFromList ( ( < ObjectLiteral > node . parent ) . properties ) ;
185
183
case SyntaxKind . TypeLiteral :
186
- return getCustomIndentationFromList ( ( < TypeLiteralNode > leftSibling . parent ) . members ) ;
184
+ return getCustomIndentationFromList ( ( < TypeLiteralNode > node . parent ) . members ) ;
187
185
case SyntaxKind . ArrayLiteral :
188
- return getCustomIndentationFromList ( ( < ArrayLiteral > leftSibling . parent ) . elements ) ;
186
+ return getCustomIndentationFromList ( ( < ArrayLiteral > node . parent ) . elements ) ;
189
187
case SyntaxKind . FunctionDeclaration :
190
188
case SyntaxKind . FunctionExpression :
191
189
case SyntaxKind . ArrowFunction :
192
190
case SyntaxKind . Method :
193
191
case SyntaxKind . CallSignature :
194
192
case SyntaxKind . ConstructSignature :
195
- if ( ( < SignatureDeclaration > leftSibling . parent ) . typeParameters && leftSibling . end < ( < SignatureDeclaration > leftSibling . parent ) . typeParameters . end ) {
196
- return getCustomIndentationFromList ( ( < SignatureDeclaration > leftSibling . parent ) . typeParameters ) ;
193
+ if ( ( < SignatureDeclaration > node . parent ) . typeParameters && node . end < ( < SignatureDeclaration > node . parent ) . typeParameters . end ) {
194
+ return getCustomIndentationFromList ( ( < SignatureDeclaration > node . parent ) . typeParameters ) ;
197
195
}
198
196
else {
199
- return getCustomIndentationFromList ( ( < SignatureDeclaration > leftSibling . parent ) . parameters ) ;
197
+ return getCustomIndentationFromList ( ( < SignatureDeclaration > node . parent ) . parameters ) ;
200
198
}
201
199
case SyntaxKind . NewExpression :
202
200
case SyntaxKind . CallExpression :
203
- if ( ( < CallExpression > leftSibling . parent ) . typeArguments && leftSibling . end < ( < CallExpression > leftSibling . parent ) . typeArguments . end ) {
204
- return getCustomIndentationFromList ( ( < CallExpression > leftSibling . parent ) . typeArguments ) ;
201
+ if ( ( < CallExpression > node . parent ) . typeArguments && node . end < ( < CallExpression > node . parent ) . typeArguments . end ) {
202
+ return getCustomIndentationFromList ( ( < CallExpression > node . parent ) . typeArguments ) ;
205
203
}
206
204
else {
207
- return getCustomIndentationFromList ( ( < CallExpression > leftSibling . parent ) . arguments ) ;
205
+ return getCustomIndentationFromList ( ( < CallExpression > node . parent ) . arguments ) ;
208
206
}
209
207
210
208
break ;
@@ -214,9 +212,9 @@ module ts.formatting {
214
212
return - 1 ;
215
213
216
214
function getCustomIndentationFromList ( list : Node [ ] ) : number {
217
- var index = indexOf ( list , leftSibling ) ;
215
+ var index = indexOf ( list , node ) ;
218
216
if ( index !== - 1 ) {
219
- var lineAndCol = sourceFile . getLineAndCharacterFromPosition ( leftSibling . getStart ( sourceFile ) ) ;
217
+ var lineAndCol = sourceFile . getLineAndCharacterFromPosition ( node . getStart ( sourceFile ) ) ;
220
218
for ( var i = index - 1 ; i >= 0 ; -- i ) {
221
219
var prevLineAndCol = sourceFile . getLineAndCharacterFromPosition ( list [ i ] . getStart ( sourceFile ) ) ;
222
220
if ( lineAndCol . line !== prevLineAndCol . line ) {
@@ -227,7 +225,7 @@ module ts.formatting {
227
225
return i ;
228
226
}
229
227
}
230
- // code is unreachable because the rance that we check above includes at least one non-whitespace character at the very end
228
+ // code is unreachable because the range that we check above includes at least one non-whitespace character at the very end
231
229
Debug . fail ( "Unreachable code" )
232
230
233
231
}
0 commit comments