Skip to content

Commit 1dcc83e

Browse files
author
Andy
authored
Minor cleanup in getDynamicIndentation (#21240)
1 parent ec37651 commit 1dcc83e

File tree

1 file changed

+39
-53
lines changed

1 file changed

+39
-53
lines changed

src/services/formatting/formatting.ts

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -537,70 +537,56 @@ namespace ts.formatting {
537537
case SyntaxKind.CloseBraceToken:
538538
case SyntaxKind.CloseBracketToken:
539539
case SyntaxKind.CloseParenToken:
540-
return indentation + getEffectiveDelta(delta, container);
540+
return indentation + getDelta(container);
541541
}
542542
return tokenIndentation !== Constants.Unknown ? tokenIndentation : indentation;
543543
},
544-
getIndentationForToken: (line, kind, container) => {
545-
if (nodeStartLine !== line && node.decorators) {
546-
if (kind === getFirstNonDecoratorTokenOfNode(node)) {
547-
// if this token is the first token following the list of decorators, we do not need to indent
548-
return indentation;
549-
}
550-
}
551-
switch (kind) {
552-
// open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent
553-
case SyntaxKind.OpenBraceToken:
554-
case SyntaxKind.CloseBraceToken:
555-
case SyntaxKind.OpenParenToken:
556-
case SyntaxKind.CloseParenToken:
557-
case SyntaxKind.ElseKeyword:
558-
case SyntaxKind.WhileKeyword:
559-
case SyntaxKind.AtToken:
560-
return indentation;
561-
case SyntaxKind.SlashToken:
562-
case SyntaxKind.GreaterThanToken: {
563-
if (container.kind === SyntaxKind.JsxOpeningElement ||
564-
container.kind === SyntaxKind.JsxClosingElement ||
565-
container.kind === SyntaxKind.JsxSelfClosingElement
566-
) {
567-
return indentation;
568-
}
569-
break;
570-
}
571-
case SyntaxKind.OpenBracketToken:
572-
case SyntaxKind.CloseBracketToken: {
573-
if (container.kind !== SyntaxKind.MappedType) {
574-
return indentation;
575-
}
576-
break;
577-
}
578-
}
579-
// if token line equals to the line of containing node (this is a first token in the node) - use node indentation
580-
return nodeStartLine !== line ? indentation + getEffectiveDelta(delta, container) : indentation;
581-
},
544+
getIndentationForToken: (line, kind, container) =>
545+
shouldAddDelta(line, kind, container) ? indentation + getDelta(container) : indentation,
582546
getIndentation: () => indentation,
583-
getDelta: child => getEffectiveDelta(delta, child),
547+
getDelta,
584548
recomputeIndentation: lineAdded => {
585549
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node)) {
586-
if (lineAdded) {
587-
indentation += options.indentSize;
588-
}
589-
else {
590-
indentation -= options.indentSize;
591-
}
550+
indentation += lineAdded ? options.indentSize : -options.indentSize;
551+
delta = SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0;
552+
}
553+
}
554+
};
592555

593-
if (SmartIndenter.shouldIndentChildNode(node)) {
594-
delta = options.indentSize;
556+
function shouldAddDelta(line: number, kind: SyntaxKind, container: Node): boolean {
557+
switch (kind) {
558+
// open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent
559+
case SyntaxKind.OpenBraceToken:
560+
case SyntaxKind.CloseBraceToken:
561+
case SyntaxKind.OpenParenToken:
562+
case SyntaxKind.CloseParenToken:
563+
case SyntaxKind.ElseKeyword:
564+
case SyntaxKind.WhileKeyword:
565+
case SyntaxKind.AtToken:
566+
return false;
567+
case SyntaxKind.SlashToken:
568+
case SyntaxKind.GreaterThanToken:
569+
switch (container.kind) {
570+
case SyntaxKind.JsxOpeningElement:
571+
case SyntaxKind.JsxClosingElement:
572+
case SyntaxKind.JsxSelfClosingElement:
573+
return false;
595574
}
596-
else {
597-
delta = 0;
575+
break;
576+
case SyntaxKind.OpenBracketToken:
577+
case SyntaxKind.CloseBracketToken:
578+
if (container.kind !== SyntaxKind.MappedType) {
579+
return false;
598580
}
599-
}
581+
break;
600582
}
601-
};
583+
// if token line equals to the line of containing node (this is a first token in the node) - use node indentation
584+
return nodeStartLine !== line
585+
// if this token is the first token following the list of decorators, we do not need to indent
586+
&& !(node.decorators && kind === getFirstNonDecoratorTokenOfNode(node));
587+
}
602588

603-
function getEffectiveDelta(delta: number, child: TextRangeWithKind) {
589+
function getDelta(child: TextRangeWithKind) {
604590
// Delta value should be zero when the node explicitly prevents indentation of the child node
605591
return SmartIndenter.nodeWillIndentChild(node, child, /*indentByDefault*/ true) ? delta : 0;
606592
}

0 commit comments

Comments
 (0)