From 33fbce1ff76c8c1577f424263fb91fd22f1e1271 Mon Sep 17 00:00:00 2001 From: SaschaNaz Date: Sat, 12 Sep 2015 17:05:12 +0900 Subject: [PATCH] nodeWillIndentChild from #4609 --- src/services/formatting/formatting.ts | 36 +++++++----------------- src/services/formatting/smartIndenter.ts | 36 +++++++++++++++++------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index fad3ebe6e2b..868683bb6af 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -282,19 +282,19 @@ namespace ts.formatting { */ function getOwnOrInheritedDelta(n: Node, options: FormatCodeOptions, sourceFile: SourceFile): number { let previousLine = Constants.Unknown; - let childKind = SyntaxKind.Unknown; + let child: Node = null; while (n) { let line = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)).line; if (previousLine !== Constants.Unknown && line !== previousLine) { break; } - if (SmartIndenter.shouldIndentChildNode(n.kind, childKind)) { + if (SmartIndenter.shouldIndentChildNode(n, child)) { return options.IndentSize; } previousLine = line; - childKind = n.kind; + child = n; n = n.parent; } return 0; @@ -387,33 +387,17 @@ namespace ts.formatting { let indentation = inheritedIndentation; if (indentation === Constants.Unknown) { - if (isSomeBlock(node.kind)) { - // blocks should be indented in - // - other blocks - // - source file - // - switch\default clauses - if (isSomeBlock(parent.kind) || - parent.kind === SyntaxKind.SourceFile || - parent.kind === SyntaxKind.CaseClause || - parent.kind === SyntaxKind.DefaultClause) { + if (SmartIndenter.shouldInheritParentIndentation(parent, node) || + SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { - indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(); - } - else { - indentation = parentDynamicIndentation.getIndentation(); - } + indentation = parentDynamicIndentation.getIndentation(); } else { - if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { - indentation = parentDynamicIndentation.getIndentation(); - } - else { - indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(); - } + indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(); } } - var delta = SmartIndenter.shouldIndentChildNode(node.kind, SyntaxKind.Unknown) ? options.IndentSize : 0; + var delta = SmartIndenter.shouldIndentChildNode(node, null) ? options.IndentSize : 0; if (effectiveParentStartLine === startLine) { // if node is located on the same line with the parent @@ -495,7 +479,7 @@ namespace ts.formatting { getIndentation: () => indentation, getDelta: () => delta, recomputeIndentation: lineAdded => { - if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent.kind, node.kind)) { + if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node)) { if (lineAdded) { indentation += options.IndentSize; } @@ -503,7 +487,7 @@ namespace ts.formatting { indentation -= options.IndentSize; } - if (SmartIndenter.shouldIndentChildNode(node.kind, SyntaxKind.Unknown)) { + if (SmartIndenter.shouldIndentChildNode(node, null)) { delta = options.IndentSize; } else { diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 8355fac03f5..ab23f3da939 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -48,7 +48,7 @@ namespace ts.formatting { let indentationDelta: number; while (current) { - if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current.kind, previous ? previous.kind : SyntaxKind.Unknown)) { + if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous)) { currentStart = getStartLineAndCharacterForNode(current, sourceFile); if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) { @@ -133,7 +133,7 @@ namespace ts.formatting { } // increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line - if (shouldIndentChildNode(parent.kind, current.kind) && !parentAndChildShareLine) { + if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { indentationDelta += options.IndentSize; } @@ -446,11 +446,12 @@ namespace ts.formatting { return false; } - export function shouldIndentChildNode(parent: SyntaxKind, child: SyntaxKind): boolean { - if (nodeContentIsAlwaysIndented(parent)) { - return true; - } - switch (parent) { + /** + * Function returns true when a node with conditional indentation rule will indent certain child node. + */ + function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean) { + let childKind = child ? child.kind : SyntaxKind.Unknown; + switch (parent.kind) { case SyntaxKind.DoStatement: case SyntaxKind.WhileStatement: case SyntaxKind.ForInStatement: @@ -464,10 +465,25 @@ namespace ts.formatting { case SyntaxKind.Constructor: case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: - return child !== SyntaxKind.Block; - default: - return false; + return childKind !== SyntaxKind.Block; } + // No explicit rule for selected nodes, so result will follow the default value argument. + return indentByDefault; + } + + export function shouldIndentChildNode(parent: TextRangeWithKind, child: TextRangeWithKind): boolean { + if (nodeContentIsAlwaysIndented(parent.kind)) { + return true; + } + return nodeWillIndentChild(parent, child, false); + } + + /** + * Function returns true if a node should not get additional indentation in its parent node. + */ + export function shouldInheritParentIndentation(parent: TextRangeWithKind, child: TextRangeWithKind): boolean { + // Check if + return !nodeWillIndentChild(parent, child, true); } } } \ No newline at end of file