diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 01c09a4989c..ed6b71efb75 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -490,6 +490,9 @@ namespace ts.formatting { else if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { return { indentation: parentDynamicIndentation.getIndentation(), delta }; } + else if (SmartIndenter.argumentStartsOnSameLineAsPreviousArgument(parent, node, startLine, sourceFile)) { + return { indentation: parentDynamicIndentation.getIndentation(), delta }; + } else { return { indentation: parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta(node), delta }; } diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 0c9e7b186f7..7a8bfb5f5d5 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -322,6 +322,25 @@ namespace ts.formatting { return false; } + export function argumentStartsOnSameLineAsPreviousArgument(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean { + if (isCallOrNewExpression(parent)) { + if (!parent.arguments) return false; + + const currentNode = Debug.assertDefined(find(parent.arguments, arg => arg.pos === child.pos)); + const currentIndex = parent.arguments.indexOf(currentNode); + if (currentIndex === 0) return false; // Can't look at previous node if first + + const previousNode = parent.arguments[currentIndex - 1]; + const lineOfPreviousNode = getLineAndCharacterOfPosition(sourceFile, previousNode.getEnd()).line; + + if (childStartLine === lineOfPreviousNode) { + return true; + } + } + + return false; + } + export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray | undefined { return node.parent && getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile); } diff --git a/tests/cases/fourslash/consistenceOnIndentionsOfObjectsInAListAfterFormatting.ts b/tests/cases/fourslash/consistenceOnIndentionsOfObjectsInAListAfterFormatting.ts index 14ada206026..bbcdaec9cad 100644 --- a/tests/cases/fourslash/consistenceOnIndentionsOfObjectsInAListAfterFormatting.ts +++ b/tests/cases/fourslash/consistenceOnIndentionsOfObjectsInAListAfterFormatting.ts @@ -8,4 +8,4 @@ format.document(); goTo.marker("1"); verify.currentLineContentIs("}, {"); goTo.marker("2"); -verify.currentLineContentIs(" });"); \ No newline at end of file +verify.currentLineContentIs("});"); diff --git a/tests/cases/fourslash/formatMultipleFunctionArguments.ts b/tests/cases/fourslash/formatMultipleFunctionArguments.ts new file mode 100644 index 00000000000..32e6d0eea89 --- /dev/null +++ b/tests/cases/fourslash/formatMultipleFunctionArguments.ts @@ -0,0 +1,44 @@ +/// + +//// +//// someRandomFunction({ +//// prop1: 1, +//// prop2: 2 +//// }, { +//// prop3: 3, +//// prop4: 4 +//// }, { +//// prop5: 5, +//// prop6: 6 +//// }); +//// +//// someRandomFunction( +//// { prop7: 1, prop8: 2 }, +//// { prop9: 3, prop10: 4 }, +//// { +//// prop11: 5, +//// prop2: 6 +//// } +//// ); + +format.document(); +verify.currentFileContentIs(` +someRandomFunction({ + prop1: 1, + prop2: 2 +}, { + prop3: 3, + prop4: 4 +}, { + prop5: 5, + prop6: 6 +}); + +someRandomFunction( + { prop7: 1, prop8: 2 }, + { prop9: 3, prop10: 4 }, + { + prop11: 5, + prop2: 6 + } +);`);