From f1e8e611ab9dc3d6af5cbcfe303e82dedd8bc752 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 17 Mar 2015 13:43:29 -0700 Subject: [PATCH] Updated formatting for decorators --- src/services/formatting/formatting.ts | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 24ddabc2b2..02c247bbc3 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -419,6 +419,29 @@ module ts.formatting { } } + function getFirstNonDecoratorTokenOfNode(node: Node) { + if (node.modifiers && node.modifiers.length) { + return node.modifiers[0].kind; + } + switch (node.kind) { + case SyntaxKind.ClassDeclaration: return SyntaxKind.ClassKeyword; + case SyntaxKind.InterfaceDeclaration: return SyntaxKind.InterfaceKeyword; + case SyntaxKind.FunctionDeclaration: return SyntaxKind.FunctionKeyword; + case SyntaxKind.EnumDeclaration: return SyntaxKind.EnumDeclaration; + case SyntaxKind.GetAccessor: return SyntaxKind.GetKeyword; + case SyntaxKind.SetAccessor: return SyntaxKind.SetKeyword; + case SyntaxKind.MethodDeclaration: + if ((node).asteriskToken) { + return SyntaxKind.AsteriskToken; + } + // fall-through + + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.Parameter: + return (node).name.kind; + } + } + function getDynamicIndentation(node: Node, nodeStartLine: number, indentation: number, delta: number): DynamicIndentation { return { getIndentationForComment: kind => { @@ -434,6 +457,12 @@ module ts.formatting { return indentation; }, getIndentationForToken: (line, kind) => { + if (nodeStartLine !== line && node.decorators) { + if (kind === getFirstNonDecoratorTokenOfNode(node)) { + // if this token is the first token following the list of decorators, we do not need to indent + return indentation; + } + } switch (kind) { // open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent case SyntaxKind.OpenBraceToken: @@ -442,6 +471,7 @@ module ts.formatting { case SyntaxKind.CloseBracketToken: case SyntaxKind.ElseKeyword: case SyntaxKind.WhileKeyword: + case SyntaxKind.AtToken: return indentation; default: // if token line equals to the line of containing node (this is a first token in the node) - use node indentation