@@ -176,9 +176,14 @@ module ts.formatting {
176176 var formattingContext = new FormattingContext ( sourceFile , requestKind ) ;
177177
178178 var enclosingNode = findEnclosingNode ( originalRange , sourceFile ) ;
179- var initialIndentation = SmartIndenter . getIndentationForNode ( enclosingNode , originalRange , sourceFile , options ) ;
180-
181- var formattingScanner = getFormattingScanner ( sourceFile , enclosingNode . pos , originalRange . end ) ;
179+ if ( enclosingNode . kind === SyntaxKind . SourceFile ) {
180+ var formattingScanner = getFormattingScanner ( sourceFile , originalRange . pos , originalRange . end ) ;
181+ var initialIndentation = 0 ;
182+ }
183+ else {
184+ var formattingScanner = getFormattingScanner ( sourceFile , enclosingNode . pos , originalRange . end ) ;
185+ var initialIndentation = SmartIndenter . getIndentationForNode ( enclosingNode , originalRange , sourceFile , options ) ;
186+ }
182187
183188 var previousRangeHasError : boolean ;
184189 var previousRange : TextRangeWithKind ;
@@ -321,11 +326,26 @@ module ts.formatting {
321326 effectiveParentStartLine : number ,
322327 isListItem : boolean ) : number {
323328
329+ var childStartPos = child . getStart ( sourceFile ) ;
330+
331+ var childStart = sourceFile . getLineAndCharacterFromPosition ( childStartPos ) ;
332+ var childIndentationAmount =
333+ isListItem
334+ ? tryComputeIndentationForListItem ( childStartPos , child . end , effectiveParentStartLine , originalRange , inheritedIndentation )
335+ : Indentation . Unknown ;
336+
337+ if ( isListItem && childIndentationAmount !== Indentation . Unknown ) {
338+ inheritedIndentation = childIndentationAmount ;
339+ }
340+
341+ if ( ! rangeOverlapsWithStartEnd ( originalRange , child . pos , child . end ) ) {
342+ return inheritedIndentation ;
343+ }
344+
324345 if ( child . kind === SyntaxKind . Missing ) {
325346 return inheritedIndentation ;
326347 }
327348
328- var childStartPos = child . getStart ( sourceFile ) ;
329349 while ( formattingScanner . isOnToken ( ) ) {
330350 var tokenInfo = formattingScanner . readTokenInfo ( node ) ;
331351 if ( tokenInfo . token . end > childStartPos ) {
@@ -339,16 +359,6 @@ module ts.formatting {
339359 return inheritedIndentation ;
340360 }
341361
342- var childStart = sourceFile . getLineAndCharacterFromPosition ( childStartPos ) ;
343- var childIndentationAmount =
344- isListItem
345- ? tryComputeIndentationForListItem ( childStartPos , child . end , effectiveParentStartLine , originalRange , inheritedIndentation )
346- : Indentation . Unknown ;
347-
348- if ( isListItem && childIndentationAmount !== Indentation . Unknown ) {
349- inheritedIndentation = childIndentationAmount ;
350- }
351-
352362 if ( isToken ( child ) ) {
353363 var tokenInfo = formattingScanner . readTokenInfo ( node ) ;
354364 Debug . assert ( tokenInfo . token . end === child . end ) ;
0 commit comments