From 1146c690f83176bacb37aa39090d7ebe0c12df9b Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Fri, 18 May 2018 16:33:55 -0700 Subject: [PATCH 1/3] Fix issue with formatting object literal csharp style --- src/services/formatting/smartIndenter.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index a75781b8da0..f71c6108d37 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -505,7 +505,6 @@ namespace ts.formatting { case SyntaxKind.CallExpression: case SyntaxKind.NewExpression: case SyntaxKind.VariableStatement: - case SyntaxKind.VariableDeclaration: case SyntaxKind.ExportAssignment: case SyntaxKind.ReturnStatement: case SyntaxKind.ConditionalExpression: @@ -528,7 +527,6 @@ namespace ts.formatting { case SyntaxKind.NamedImports: case SyntaxKind.ExportSpecifier: case SyntaxKind.ImportSpecifier: - case SyntaxKind.PropertyAssignment: case SyntaxKind.PropertyDeclaration: return true; } @@ -541,7 +539,6 @@ namespace ts.formatting { switch (parent.kind) { case SyntaxKind.VariableDeclaration: case SyntaxKind.PropertyAssignment: - case SyntaxKind.ObjectLiteralExpression: if (!settings.indentMultiLineObjectLiteralBeginningOnBlankLine && sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) { return rangeIsOnOneLine(sourceFile, child); } From 0f7f9783dc41ee4af2e0cb431d0d08f9c7f781f9 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Fri, 18 May 2018 17:07:58 -0700 Subject: [PATCH 2/3] Fixed test regression, removed nodeContentIsAlwaysIndented --- src/services/formatting/smartIndenter.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index f71c6108d37..48b1e3a66a7 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -482,8 +482,10 @@ namespace ts.formatting { return findFirstNonWhitespaceCharacterAndColumn(startPos, endPos, sourceFile, options).column; } - function nodeContentIsAlwaysIndented(kind: SyntaxKind): boolean { - switch (kind) { + export function nodeWillIndentChild(settings: FormatCodeSettings | undefined, parent: TextRangeWithKind, child: TextRangeWithKind | undefined, sourceFile: SourceFileLike | undefined, indentByDefault: boolean): boolean { + const childKind = child ? child.kind : SyntaxKind.Unknown; + + switch (parent.kind) { case SyntaxKind.ExpressionStatement: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: @@ -529,20 +531,12 @@ namespace ts.formatting { case SyntaxKind.ImportSpecifier: case SyntaxKind.PropertyDeclaration: return true; - } - return false; - } - - export function nodeWillIndentChild(settings: FormatCodeSettings | undefined, parent: TextRangeWithKind, child: TextRangeWithKind | undefined, sourceFile: SourceFileLike | undefined, indentByDefault: boolean): boolean { - const childKind = child ? child.kind : SyntaxKind.Unknown; - - switch (parent.kind) { case SyntaxKind.VariableDeclaration: case SyntaxKind.PropertyAssignment: if (!settings.indentMultiLineObjectLiteralBeginningOnBlankLine && sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) { return rangeIsOnOneLine(sourceFile, child); } - break; + return true; case SyntaxKind.DoStatement: case SyntaxKind.WhileStatement: case SyntaxKind.ForInStatement: @@ -595,7 +589,7 @@ namespace ts.formatting { * @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child. */ export function shouldIndentChildNode(settings: FormatCodeSettings | undefined, parent: TextRangeWithKind, child?: Node, sourceFile?: SourceFileLike, isNextChild = false): boolean { - return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(settings, parent, child, sourceFile, /*indentByDefault*/ false)) + return nodeWillIndentChild(settings, parent, child, sourceFile, /*indentByDefault*/ false) && !(isNextChild && child && isControlFlowEndingStatement(child.kind, parent)); } From 0b18bdf5908ac022df61b80767abca5881ad87a8 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Mon, 21 May 2018 18:22:13 -0700 Subject: [PATCH 3/3] Added tests --- ...artIndentObjectLiteralOpenBracketNewLine.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/cases/fourslash/smartIndentObjectLiteralOpenBracketNewLine.ts diff --git a/tests/cases/fourslash/smartIndentObjectLiteralOpenBracketNewLine.ts b/tests/cases/fourslash/smartIndentObjectLiteralOpenBracketNewLine.ts new file mode 100644 index 00000000000..d20d7593e54 --- /dev/null +++ b/tests/cases/fourslash/smartIndentObjectLiteralOpenBracketNewLine.ts @@ -0,0 +1,18 @@ +/// + +//// var a = +//// {/*1*/} +//// +//// var b = { +//// outer: +//// {/*2*/} +//// } + +function verifyIndentationAfterNewLine(marker: string, indentation: number): void { + goTo.marker(marker); + edit.insert("\n"); + verify.indentationIs(indentation); +} + +verifyIndentationAfterNewLine("1", 0); +verifyIndentationAfterNewLine("2", 4); \ No newline at end of file