From 1110b902dc88cf93cac92e3a0aa4dffb3e72c311 Mon Sep 17 00:00:00 2001 From: SaschaNaz Date: Fri, 11 Sep 2015 13:43:35 +0900 Subject: [PATCH] nodeWillIndentChild --- src/services/formatting/formatting.ts | 31 +++++----------- src/services/formatting/smartIndenter.ts | 46 ++++++++++++------------ 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 4f21f3ae2a7..a47d5c85712 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; @@ -386,24 +386,9 @@ namespace ts.formatting { effectiveParentStartLine: number): Indentation { 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) { - indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(); - } - else { - indentation = parentDynamicIndentation.getIndentation(); - } - } - else if (SmartIndenter.shouldInheritParentIndentation(node) || + if (indentation === Constants.Unknown) { + if (SmartIndenter.shouldInheritParentIndentation(parent, node) || SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { indentation = parentDynamicIndentation.getIndentation(); @@ -413,7 +398,7 @@ namespace ts.formatting { } } - 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 +480,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 +488,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 aabd9af2544..e30c83151a7 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,9 +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) && - !shouldInheritParentIndentation(current) && !parentAndChildShareLine) { - + if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { indentationDelta += options.IndentSize; } @@ -442,7 +440,6 @@ namespace ts.formatting { case SyntaxKind.ParenthesizedType: case SyntaxKind.TaggedTemplateExpression: case SyntaxKind.AwaitExpression: - case SyntaxKind.ImportDeclaration: case SyntaxKind.NamedExports: case SyntaxKind.NamedImports: case SyntaxKind.ExportSpecifier: @@ -451,12 +448,10 @@ namespace ts.formatting { } return false; } - - export function shouldIndentChildNode(parent: SyntaxKind, child: SyntaxKind): boolean { - if (nodeContentIsAlwaysIndented(parent)) { - return true; - } - switch (parent) { + + 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: @@ -470,26 +465,29 @@ namespace ts.formatting { case SyntaxKind.Constructor: case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: - return child !== SyntaxKind.Block; + return childKind !== SyntaxKind.Block; case SyntaxKind.ExportDeclaration: - return child !== SyntaxKind.NamedExports; - default: - return false; + return childKind !== SyntaxKind.NamedExports; + case SyntaxKind.ImportDeclaration: + return childKind !== SyntaxKind.ImportClause || + (child).namedBindings.kind !== SyntaxKind.NamedImports; } + // 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(node: TextRangeWithKind) { - switch (node.kind) { - case SyntaxKind.NamedExports: - return true; - case SyntaxKind.ImportClause: - // NamedImports has its own braces as Block does - return (node).namedBindings.kind === SyntaxKind.NamedImports; - } - return false; + export function shouldInheritParentIndentation(parent: TextRangeWithKind, child: TextRangeWithKind) { + return !nodeWillIndentChild(parent, child, true); } } } \ No newline at end of file