do not descend into child nodes if child does not overlap with target span

This commit is contained in:
Vladimir Matveev
2014-11-08 00:10:30 -08:00
parent aafc54ed20
commit 07469fb306
2 changed files with 25 additions and 15 deletions

View File

@@ -176,9 +176,14 @@ module ts.formatting {
var formattingContext = new FormattingContext(sourceFile, requestKind);
var enclosingNode = findEnclosingNode(originalRange, sourceFile);
var initialIndentation = SmartIndenter.getIndentationForNode(enclosingNode, originalRange, sourceFile, options);
var formattingScanner = getFormattingScanner(sourceFile, enclosingNode.pos, originalRange.end);
if (enclosingNode.kind === SyntaxKind.SourceFile) {
var formattingScanner = getFormattingScanner(sourceFile, originalRange.pos, originalRange.end);
var initialIndentation = 0;
}
else {
var formattingScanner = getFormattingScanner(sourceFile, enclosingNode.pos, originalRange.end);
var initialIndentation = SmartIndenter.getIndentationForNode(enclosingNode, originalRange, sourceFile, options);
}
var previousRangeHasError: boolean;
var previousRange: TextRangeWithKind;
@@ -321,11 +326,26 @@ module ts.formatting {
effectiveParentStartLine: number,
isListItem: boolean): number {
var childStartPos = child.getStart(sourceFile);
var childStart = sourceFile.getLineAndCharacterFromPosition(childStartPos);
var childIndentationAmount =
isListItem
? tryComputeIndentationForListItem(childStartPos, child.end, effectiveParentStartLine, originalRange, inheritedIndentation)
: Indentation.Unknown;
if (isListItem && childIndentationAmount !== Indentation.Unknown) {
inheritedIndentation = childIndentationAmount;
}
if (!rangeOverlapsWithStartEnd(originalRange, child.pos, child.end)) {
return inheritedIndentation;
}
if (child.kind === SyntaxKind.Missing) {
return inheritedIndentation;
}
var childStartPos = child.getStart(sourceFile);
while (formattingScanner.isOnToken()) {
var tokenInfo = formattingScanner.readTokenInfo(node);
if (tokenInfo.token.end > childStartPos) {
@@ -339,16 +359,6 @@ module ts.formatting {
return inheritedIndentation;
}
var childStart = sourceFile.getLineAndCharacterFromPosition(childStartPos);
var childIndentationAmount =
isListItem
? tryComputeIndentationForListItem(childStartPos, child.end, effectiveParentStartLine, originalRange, inheritedIndentation)
: Indentation.Unknown;
if (isListItem && childIndentationAmount !== Indentation.Unknown) {
inheritedIndentation = childIndentationAmount;
}
if (isToken(child)) {
var tokenInfo = formattingScanner.readTokenInfo(node);
Debug.assert(tokenInfo.token.end === child.end);

View File

@@ -197,7 +197,7 @@ module ts.formatting {
return candidate.end > position || !isCompletedNode(candidate, sourceFile);
}
export function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: Node, childStartLine: number, sourceFile: SourceFile): boolean {
export function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFile): boolean {
if (parent.kind === SyntaxKind.IfStatement && (<IfStatement>parent).elseStatement === child) {
var elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile);
Debug.assert(elseKeyword !== undefined);