Skip to content

Commit 1ced600

Browse files
committed
pass in sourceFile and revert child to TextRangeOfKind
1 parent 6786d74 commit 1ced600

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/services/formatting/formatting.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ namespace ts.formatting {
328328
break;
329329
}
330330

331-
if (SmartIndenter.shouldIndentChildNode(n, child)) {
331+
if (SmartIndenter.shouldIndentChildNode(n, child, sourceFile)) {
332332
return options.indentSize;
333333
}
334334

@@ -514,7 +514,7 @@ namespace ts.formatting {
514514
if ((<MethodDeclaration>node).asteriskToken) {
515515
return SyntaxKind.AsteriskToken;
516516
}
517-
// falls through
517+
// falls through
518518
case SyntaxKind.PropertyDeclaration:
519519
case SyntaxKind.Parameter:
520520
return getNameOfDeclaration(<Declaration>node).kind;
@@ -541,7 +541,7 @@ namespace ts.formatting {
541541
getIndentation: () => indentation,
542542
getDelta,
543543
recomputeIndentation: lineAdded => {
544-
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node)) {
544+
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node, sourceFile)) {
545545
indentation += lineAdded ? options.indentSize : -options.indentSize;
546546
delta = SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0;
547547
}
@@ -583,7 +583,7 @@ namespace ts.formatting {
583583

584584
function getDelta(child: Node) {
585585
// Delta value should be zero when the node explicitly prevents indentation of the child node
586-
return SmartIndenter.nodeWillIndentChild(node, child, /*indentByDefault*/ true) ? delta : 0;
586+
return SmartIndenter.nodeWillIndentChild(node, child, sourceFile, /*indentByDefault*/ true) ? delta : 0;
587587
}
588588
}
589589

src/services/formatting/smartIndenter.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace ts.formatting {
112112
let previous: Node | undefined;
113113
let current = precedingToken;
114114
while (current) {
115-
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous, /*isNextChild*/ true)) {
115+
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous, sourceFile, /*isNextChild*/ true)) {
116116
const currentStart = getStartLineAndCharacterForNode(current, sourceFile);
117117
const nextTokenKind = nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile);
118118
const indentationDelta = nextTokenKind !== NextTokenKind.Unknown
@@ -193,7 +193,7 @@ namespace ts.formatting {
193193
}
194194

195195
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
196-
if (shouldIndentChildNode(parent, current, isNextChild) && !parentAndChildShareLine) {
196+
if (shouldIndentChildNode(parent, current, sourceFile, isNextChild) && !parentAndChildShareLine) {
197197
indentationDelta += options.indentSize;
198198
}
199199

@@ -531,21 +531,18 @@ namespace ts.formatting {
531531
return false;
532532
}
533533

534-
export function nodeWillIndentChild(parent: TextRangeWithKind, child: Node | undefined, indentByDefault: boolean): boolean {
534+
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind | undefined, sourceFile: SourceFileLike | undefined, indentByDefault: boolean): boolean {
535535
const childKind = child ? child.kind : SyntaxKind.Unknown;
536536

537537
switch (parent.kind) {
538538
case SyntaxKind.VariableDeclaration:
539539
case SyntaxKind.PropertyAssignment:
540540
case SyntaxKind.ObjectLiteralExpression:
541-
if (childKind === SyntaxKind.ObjectLiteralExpression) {
542-
const sourceFile = child.getSourceFile();
543-
if (sourceFile) {
544-
// May not be defined for synthesized nodes.
545-
const startLine = sourceFile.getLineAndCharacterOfPosition(child.getStart()).line;
546-
const endLine = sourceFile.getLineAndCharacterOfPosition(child.getEnd()).line;
547-
return startLine === endLine;
548-
}
541+
if (sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) {
542+
const childStart = skipTrivia(sourceFile.text, child.pos);
543+
const startLine = sourceFile.getLineAndCharacterOfPosition(childStart).line;
544+
const endLine = sourceFile.getLineAndCharacterOfPosition(child.end).line;
545+
return startLine === endLine;
549546
}
550547
break;
551548
case SyntaxKind.DoStatement:
@@ -599,8 +596,8 @@ namespace ts.formatting {
599596
* True when the parent node should indent the given child by an explicit rule.
600597
* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child.
601598
*/
602-
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: Node, isNextChild = false): boolean {
603-
return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, /*indentByDefault*/ false))
599+
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: Node, sourceFile?: SourceFileLike, isNextChild = false): boolean {
600+
return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, sourceFile, /*indentByDefault*/ false))
604601
&& !(isNextChild && child && isControlFlowEndingStatement(child.kind, parent));
605602
}
606603
}

0 commit comments

Comments
 (0)