Skip to content

Commit 07469fb

Browse files
committed
do not descend into child nodes if child does not overlap with target span
1 parent aafc54e commit 07469fb

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

src/services/format.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/services/smartIndenter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ module ts.formatting {
197197
return candidate.end > position || !isCompletedNode(candidate, sourceFile);
198198
}
199199

200-
export function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: Node, childStartLine: number, sourceFile: SourceFile): boolean {
200+
export function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFile): boolean {
201201
if (parent.kind === SyntaxKind.IfStatement && (<IfStatement>parent).elseStatement === child) {
202202
var elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile);
203203
Debug.assert(elseKeyword !== undefined);

0 commit comments

Comments
 (0)