From e9227d656d4b0358f2b894b1ad09731bba4c1e98 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 11 Sep 2014 17:26:05 -0700 Subject: [PATCH] added support for smart indentation in the middle of list items, updated test baselines --- src/services/formatting/smartIndenter.ts | 52 +++++++++---------- .../chainedFunctionFunctionArgIndent.ts | 4 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index f6958ebbc57..4eb3aaaa55f 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -51,7 +51,7 @@ module ts.formatting { var indentation: number; while (current) { - if (!isToken(current) && isPositionBelongToNode(current, position, sourceFile)) { + if (isPositionBelongToNode(current, position, sourceFile)) { currentStartLine = getStartLineForNode(current, sourceFile); @@ -68,6 +68,10 @@ module ts.formatting { break; } + var customIndentation = getCustomIndentationForListItem(current, sourceFile); + if (customIndentation !== -1) { + return customIndentation; + } previous = current; current = current.parent; @@ -85,6 +89,11 @@ module ts.formatting { // walk upwards and collect indentations for pairs of parent-child nodes // indentation is not added if parent and child nodes start on the same line or if parent is IfStatement and child starts on the same line with 'else clause' while (parent) { + var customIndentation = getCustomIndentationForListItem(current, sourceFile); + if (customIndentation !== -1) { + return customIndentation + indentation; + } + parentStartLine = sourceFile.getLineAndCharacterFromPosition(parent.getStart(sourceFile)).line; var increaseIndentation = isNodeContentIndented(parent, current) && @@ -166,45 +175,34 @@ module ts.formatting { } } - // preserve indentation for list items - // - first list item is either on the same line with the parent: foo(a... . (in this case it is not indented) or on the different line (then it is indented with base level + delta) - // - subsequent list items inherit indentation for its sibling on the left when these siblings are also on the new line. - // 1. foo(a, b - // $ - indentation = base level + delta - // 2. foo (a, - // b, c, d, - // $ - same indentation with first child node on the previous line - // NOTE: indentation for list items spans from the beginning of the line to the first non-whitespace character - // /*test*/ x, - // $ <-- indentation for a new item will be here - function getCustomIndentationForListItem(leftSibling: Node, sourceFile: SourceFile): number { - if (leftSibling.parent) { - switch (leftSibling.parent.kind) { + function getCustomIndentationForListItem(node: Node, sourceFile: SourceFile): number { + if (node.parent) { + switch (node.parent.kind) { case SyntaxKind.ObjectLiteral: - return getCustomIndentationFromList((leftSibling.parent).properties); + return getCustomIndentationFromList((node.parent).properties); case SyntaxKind.TypeLiteral: - return getCustomIndentationFromList((leftSibling.parent).members); + return getCustomIndentationFromList((node.parent).members); case SyntaxKind.ArrayLiteral: - return getCustomIndentationFromList((leftSibling.parent).elements); + return getCustomIndentationFromList((node.parent).elements); case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: case SyntaxKind.Method: case SyntaxKind.CallSignature: case SyntaxKind.ConstructSignature: - if ((leftSibling.parent).typeParameters && leftSibling.end < (leftSibling.parent).typeParameters.end) { - return getCustomIndentationFromList((leftSibling.parent).typeParameters); + if ((node.parent).typeParameters && node.end < (node.parent).typeParameters.end) { + return getCustomIndentationFromList((node.parent).typeParameters); } else { - return getCustomIndentationFromList((leftSibling.parent).parameters); + return getCustomIndentationFromList((node.parent).parameters); } case SyntaxKind.NewExpression: case SyntaxKind.CallExpression: - if ((leftSibling.parent).typeArguments && leftSibling.end < (leftSibling.parent).typeArguments.end) { - return getCustomIndentationFromList((leftSibling.parent).typeArguments); + if ((node.parent).typeArguments && node.end < (node.parent).typeArguments.end) { + return getCustomIndentationFromList((node.parent).typeArguments); } else { - return getCustomIndentationFromList((leftSibling.parent).arguments); + return getCustomIndentationFromList((node.parent).arguments); } break; @@ -214,9 +212,9 @@ module ts.formatting { return -1; function getCustomIndentationFromList(list: Node[]): number { - var index = indexOf(list, leftSibling); + var index = indexOf(list, node); if (index !== -1) { - var lineAndCol = sourceFile.getLineAndCharacterFromPosition(leftSibling.getStart(sourceFile)); + var lineAndCol = sourceFile.getLineAndCharacterFromPosition(node.getStart(sourceFile)); for (var i = index - 1; i >= 0; --i) { var prevLineAndCol = sourceFile.getLineAndCharacterFromPosition(list[i].getStart(sourceFile)); if (lineAndCol.line !== prevLineAndCol.line) { @@ -227,7 +225,7 @@ module ts.formatting { return i; } } - // code is unreachable because the rance that we check above includes at least one non-whitespace character at the very end + // code is unreachable because the range that we check above includes at least one non-whitespace character at the very end Debug.fail("Unreachable code") } diff --git a/tests/cases/fourslash/chainedFunctionFunctionArgIndent.ts b/tests/cases/fourslash/chainedFunctionFunctionArgIndent.ts index 8b9a54de52f..ac5b74e83ce 100644 --- a/tests/cases/fourslash/chainedFunctionFunctionArgIndent.ts +++ b/tests/cases/fourslash/chainedFunctionFunctionArgIndent.ts @@ -2,10 +2,10 @@ //// declare var $: any; //// $(".contentDiv").each(function (index, element) {/**/ //// // <-- ensure cursor is here after return on above -//// }); // Ensure indent is 0 after LBrace +//// }); goTo.marker(); edit.insert("\n"); verify.indentationIs(4); edit.insert("}"); -verify.indentationIs(0); +verify.indentationIs(4); // keep arguments indented