@@ -247,6 +247,41 @@ module ts.formatting {
247
247
return precedingToken ? precedingToken . end : enclosingNode . pos ;
248
248
}
249
249
250
+ /*
251
+ * For cases like
252
+ * if (a ||
253
+ * b ||$
254
+ * c) {...}
255
+ * If we hit Enter at $ we want line ' b ||' to be indented.
256
+ * Formatting will be applied to the last two lines.
257
+ * Node that fully encloses these lines is binary expression 'a ||...'.
258
+ * Initial indentation for this node will be 0.
259
+ * Binary expressions don't introduce new indentation scopes, however it is possible
260
+ * that some parent node on the same line does - like if statement in this case.
261
+ * Note that we are considering parents only from the same line with initial node -
262
+ * if parent is on the different line - its delta was already contributed
263
+ * to the initial indentation.
264
+ */
265
+ function getOwnOrInheritedDelta ( n : Node , options : FormatCodeOptions , sourceFile : SourceFile ) : number {
266
+ var previousLine = Constants . Unknown ;
267
+ var childKind = SyntaxKind . Unknown ;
268
+ while ( n ) {
269
+ var line = sourceFile . getLineAndCharacterFromPosition ( n . getStart ( sourceFile ) ) . line ;
270
+ if ( previousLine !== Constants . Unknown && line !== previousLine ) {
271
+ break ;
272
+ }
273
+
274
+ if ( SmartIndenter . shouldIndentChildNode ( n . kind , childKind ) ) {
275
+ return options . IndentSize ;
276
+ }
277
+
278
+ previousLine = line ;
279
+ childKind = n . kind ;
280
+ n = n . parent ;
281
+ }
282
+ return 0 ;
283
+ }
284
+
250
285
function formatSpan ( originalRange : TextRange ,
251
286
sourceFile : SourceFile ,
252
287
options : FormatCodeOptions ,
@@ -276,7 +311,7 @@ module ts.formatting {
276
311
277
312
if ( formattingScanner . isOnToken ( ) ) {
278
313
var startLine = sourceFile . getLineAndCharacterFromPosition ( enclosingNode . getStart ( sourceFile ) ) . line ;
279
- var delta = SmartIndenter . shouldIndentChildNode ( enclosingNode . kind , SyntaxKind . Unknown ) ? options . IndentSize : 0 ;
314
+ var delta = getOwnOrInheritedDelta ( enclosingNode , options , sourceFile ) ;
280
315
processNode ( enclosingNode , enclosingNode , startLine , initialIndentation , delta ) ;
281
316
}
282
317
0 commit comments