From a9eba62b68611bfb4aa75446ad6b147086dbe541 Mon Sep 17 00:00:00 2001 From: SaschaNaz Date: Sat, 16 May 2015 16:24:50 +0900 Subject: [PATCH] getLineIndentation --- src/services/formatting/smartIndenter.ts | 62 +++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 35bc015d66b..77e70a1ceaf 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -110,12 +110,6 @@ module ts.formatting { if (actualIndentation !== Value.Unknown) { return actualIndentation + indentationDelta; } - - // check if current node is a block-form item - if yes, take indentation from it - actualIndentation = getActualIndentationForBlockFormItem(current, sourceFile, options); - if (actualIndentation !== Value.Unknown) { - return actualIndentation + indentationDelta; - } } parentStart = getParentStart(parent, current, sourceFile); let parentAndChildShareLine = @@ -283,23 +277,48 @@ module ts.formatting { return undefined; } - function getActualIndentationForBlockFormItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number { - if (isPassableBlockForm(node.kind)) { - let firstChild = node.getChildAt(0); - let lineAndCharacter = getStartLineAndCharacterForNode(firstChild, sourceFile); - return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options); - } - return Value.Unknown; - } - function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number { let containingList = getContainingList(node, sourceFile); - return containingList ? getActualIndentationFromList(containingList) : Value.Unknown; + + if (containingList) { + let lineIndentation = getLineIndentationWhenExpressionIsInMultiLine(); + if (lineIndentation !== Value.Unknown) + return lineIndentation; + return getActualIndentationFromList(containingList); + } + return Value.Unknown; function getActualIndentationFromList(list: Node[]): number { let index = indexOf(list, node); return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : Value.Unknown; } + + function getLineIndentationWhenExpressionIsInMultiLine() { + if (node.parent.kind === SyntaxKind.CallExpression) { + let parentExpression = (node.parent).expression; + let startingExpression = getStartingExpression(parentExpression); + + if (parentExpression === startingExpression) { + return Value.Unknown; + } + + let parentExpressionEnd = sourceFile.getLineAndCharacterOfPosition(parentExpression.end); + let startingExpressionEnd = sourceFile.getLineAndCharacterOfPosition(startingExpression.end); + + if (parentExpressionEnd.line === startingExpressionEnd.line) { + return Value.Unknown; + } + + return findColumnForFirstNonWhitespaceCharacterInLine(parentExpressionEnd, sourceFile, options); + } + return Value.Unknown; + } + + function getStartingExpression(expression: CallExpression) { + while (expression.expression) + expression = expression.expression; + return expression; + } } function deriveActualIndentationFromList(list: Node[], index: number, sourceFile: SourceFile, options: EditorOptions): number { @@ -415,16 +434,5 @@ module ts.formatting { return false; } } - - export function isPassableBlockForm(kind: SyntaxKind): boolean { - switch (kind) { - case SyntaxKind.ArrowFunction: - case SyntaxKind.FunctionExpression: - case SyntaxKind.ArrayLiteralExpression: - case SyntaxKind.ObjectLiteralExpression: - return true; - } - return false; - } } } \ No newline at end of file