From b85d56b8570ce6bdb4ec0ee4dd5e8694dc0cabb9 Mon Sep 17 00:00:00 2001 From: SaschaNaz Date: Fri, 15 May 2015 21:00:32 +0900 Subject: [PATCH] block form indentation --- src/services/formatting/smartIndenter.ts | 28 ++++++++++++++++++- .../fourslash/formattingOnChainedCallbacks.ts | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 9cd28a40a54..35bc015d66b 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -26,7 +26,7 @@ module ts.formatting { precedingToken.kind === SyntaxKind.TemplateHead || precedingToken.kind === SyntaxKind.TemplateMiddle || precedingToken.kind === SyntaxKind.TemplateTail; - if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) { + if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) { return 0; } @@ -110,6 +110,12 @@ 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 = @@ -277,6 +283,15 @@ 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; @@ -400,5 +415,16 @@ 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 diff --git a/tests/cases/fourslash/formattingOnChainedCallbacks.ts b/tests/cases/fourslash/formattingOnChainedCallbacks.ts index 37fa6ff03f1..98f91de56fb 100644 --- a/tests/cases/fourslash/formattingOnChainedCallbacks.ts +++ b/tests/cases/fourslash/formattingOnChainedCallbacks.ts @@ -17,6 +17,8 @@ goTo.marker('1'); edit.insertLine(''); goTo.marker('2'); verify.currentLineContentIs(' ""'); +edit.insertLine(''); +verify.indentationIs(8); goTo.marker('4'); edit.insertLine(''); goTo.marker('3');