Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3fc480c

Browse filesBrowse files
committed
cSharp-style formatting for multi-line object literals
1 parent e5f6ed0 commit 3fc480c
Copy full SHA for 3fc480c

4 files changed

+77-3Lines changed: 77 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/services/formatting/formatting.ts‎

Copy file name to clipboardExpand all lines: src/services/formatting/formatting.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ namespace ts.formatting {
581581
&& !(node.decorators && kind === getFirstNonDecoratorTokenOfNode(node));
582582
}
583583

584-
function getDelta(child: TextRangeWithKind) {
584+
function getDelta(child: Node) {
585585
// Delta value should be zero when the node explicitly prevents indentation of the child node
586586
return SmartIndenter.nodeWillIndentChild(node, child, /*indentByDefault*/ true) ? delta : 0;
587587
}
Collapse file

‎src/services/formatting/smartIndenter.ts‎

Copy file name to clipboardExpand all lines: src/services/formatting/smartIndenter.ts
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,23 @@ namespace ts.formatting {
531531
return false;
532532
}
533533

534-
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind | undefined, indentByDefault: boolean): boolean {
534+
export function nodeWillIndentChild(parent: TextRangeWithKind, child: Node | undefined, indentByDefault: boolean): boolean {
535535
const childKind = child ? child.kind : SyntaxKind.Unknown;
536+
536537
switch (parent.kind) {
538+
case SyntaxKind.VariableDeclaration:
539+
case SyntaxKind.PropertyAssignment:
540+
case SyntaxKind.ObjectLiteralExpression:
541+
if (childKind === SyntaxKind.ObjectLiteralExpression) {
542+
const sourceFile = child.getSourceFile();
543+
if (sourceFile) {
544+
// May not be defined for synthesized nodes.
545+
const startLine = sourceFile.getLineAndCharacterOfPosition(child.getStart()).line;
546+
const endLine = sourceFile.getLineAndCharacterOfPosition(child.getEnd()).line;
547+
return startLine === endLine;
548+
}
549+
}
550+
break;
537551
case SyntaxKind.DoStatement:
538552
case SyntaxKind.WhileStatement:
539553
case SyntaxKind.ForInStatement:
@@ -585,7 +599,7 @@ namespace ts.formatting {
585599
* True when the parent node should indent the given child by an explicit rule.
586600
* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child.
587601
*/
588-
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: TextRangeWithKind, isNextChild = false): boolean {
602+
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: Node, isNextChild = false): boolean {
589603
return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, /*indentByDefault*/ false))
590604
&& !(isNextChild && child && isControlFlowEndingStatement(child.kind, parent));
591605
}
Collapse file
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////
4+
//// var clear =
5+
//// {
6+
//// outerKey:
7+
//// {
8+
//// innerKey: 1
9+
//// }
10+
//// };
11+
////
12+
13+
format.document();
14+
verify.currentFileContentIs(
15+
`
16+
var clear =
17+
{
18+
outerKey:
19+
{
20+
innerKey: 1
21+
}
22+
};
23+
`
24+
);
Collapse file
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////
4+
//// var varName =/**/
5+
////
6+
7+
goTo.marker();
8+
edit.insert("\n{");
9+
verify.currentFileContentIs(
10+
`
11+
var varName =
12+
{
13+
`
14+
);
15+
16+
edit.insert("\na: 1");
17+
format.document();
18+
verify.currentFileContentIs(
19+
`
20+
var varName =
21+
{
22+
a: 1
23+
`
24+
);
25+
26+
edit.insert("\n};");
27+
28+
format.document();
29+
verify.currentFileContentIs(
30+
`
31+
var varName =
32+
{
33+
a: 1
34+
};
35+
`
36+
);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.