diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 3808cb789404b..6eaa7f5dcafaa 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -709,7 +709,20 @@ namespace ts.formatting { return inheritedIndentation; } - const effectiveParentStartLine = child.kind === SyntaxKind.Decorator ? childStartLine : undecoratedParentStartLine; + let effectiveParentStartLine = child.kind === SyntaxKind.Decorator ? childStartLine : undecoratedParentStartLine; + + // variable declarations with multiline initializers + if (node.kind === SyntaxKind.VariableDeclaration) { + const variableDeclaration = node as VariableDeclaration; + if (variableDeclaration.initializer === child) { + if (variableDeclaration.name.kind & (SyntaxKind.ObjectBindingPattern | SyntaxKind.ArrayBindingPattern)) { + const pos = sourceFile.getLineAndCharacterOfPosition(variableDeclaration.name.pos); + const end = sourceFile.getLineAndCharacterOfPosition(variableDeclaration.name.end); + effectiveParentStartLine += end.line - pos.line; + } + } + } + const childIndentation = computeIndentation(child, childStartLine, childIndentationAmount, node, parentDynamicIndentation, effectiveParentStartLine); processNode(child, childContextNode, childStartLine, undecoratedChildStartLine, childIndentation.indentation, childIndentation.delta); diff --git a/tests/cases/fourslash/formattingInDestructuring5.ts b/tests/cases/fourslash/formattingInDestructuring5.ts new file mode 100644 index 0000000000000..4d5579bbbf231 --- /dev/null +++ b/tests/cases/fourslash/formattingInDestructuring5.ts @@ -0,0 +1,23 @@ +/// + +/////*1*/const { +/////*2*/ a, +/////*3*/ b, +/////*4*/} = parseInt( +/////*5*/ '12' +/////*6*/); + +format.document(); + +goTo.marker("1"); +verify.currentLineContentIs("const {"); +goTo.marker("2"); +verify.currentLineContentIs(" a,"); +goTo.marker("3"); +verify.currentLineContentIs(" b,"); +goTo.marker("4"); +verify.currentLineContentIs("} = parseInt("); +goTo.marker("5"); +verify.currentLineContentIs(" '12'"); +goTo.marker("6"); +verify.currentLineContentIs(");"); diff --git a/tests/cases/fourslash/formattingInDestructuring6.ts b/tests/cases/fourslash/formattingInDestructuring6.ts new file mode 100644 index 0000000000000..0619cdc4fb0e6 --- /dev/null +++ b/tests/cases/fourslash/formattingInDestructuring6.ts @@ -0,0 +1,23 @@ +/// + +/////*1*/const [ +/////*2*/ a, +/////*3*/ b, +/////*4*/] = [ +/////*5*/ 1, 2 +/////*6*/]; + +format.document(); + +goTo.marker("1"); +verify.currentLineContentIs("const ["); +goTo.marker("2"); +verify.currentLineContentIs(" a,"); +goTo.marker("3"); +verify.currentLineContentIs(" b,"); +goTo.marker("4"); +verify.currentLineContentIs("] = ["); +goTo.marker("5"); +verify.currentLineContentIs(" 1, 2"); +goTo.marker("6"); +verify.currentLineContentIs("];");