Skip to content

Commit e9227d6

Browse files
committed
added support for smart indentation in the middle of list items, updated test baselines
1 parent 9a3462a commit e9227d6

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module ts.formatting {
5151
var indentation: number;
5252

5353
while (current) {
54-
if (!isToken(current) && isPositionBelongToNode(current, position, sourceFile)) {
54+
if (isPositionBelongToNode(current, position, sourceFile)) {
5555

5656
currentStartLine = getStartLineForNode(current, sourceFile);
5757

@@ -68,6 +68,10 @@ module ts.formatting {
6868

6969
break;
7070
}
71+
var customIndentation = getCustomIndentationForListItem(current, sourceFile);
72+
if (customIndentation !== -1) {
73+
return customIndentation;
74+
}
7175

7276
previous = current;
7377
current = current.parent;
@@ -85,6 +89,11 @@ module ts.formatting {
8589
// walk upwards and collect indentations for pairs of parent-child nodes
8690
// indentation is not added if parent and child nodes start on the same line or if parent is IfStatement and child starts on the same line with 'else clause'
8791
while (parent) {
92+
var customIndentation = getCustomIndentationForListItem(current, sourceFile);
93+
if (customIndentation !== -1) {
94+
return customIndentation + indentation;
95+
}
96+
8897
parentStartLine = sourceFile.getLineAndCharacterFromPosition(parent.getStart(sourceFile)).line;
8998
var increaseIndentation =
9099
isNodeContentIndented(parent, current) &&
@@ -166,45 +175,34 @@ module ts.formatting {
166175
}
167176
}
168177

169-
// preserve indentation for list items
170-
// - first list item is either on the same line with the parent: foo(a... . (in this case it is not indented) or on the different line (then it is indented with base level + delta)
171-
// - subsequent list items inherit indentation for its sibling on the left when these siblings are also on the new line.
172-
// 1. foo(a, b
173-
// $ - indentation = base level + delta
174-
// 2. foo (a,
175-
// b, c, d,
176-
// $ - same indentation with first child node on the previous line
177-
// NOTE: indentation for list items spans from the beginning of the line to the first non-whitespace character
178-
// /*test*/ x,
179-
// $ <-- indentation for a new item will be here
180-
function getCustomIndentationForListItem(leftSibling: Node, sourceFile: SourceFile): number {
181-
if (leftSibling.parent) {
182-
switch (leftSibling.parent.kind) {
178+
function getCustomIndentationForListItem(node: Node, sourceFile: SourceFile): number {
179+
if (node.parent) {
180+
switch (node.parent.kind) {
183181
case SyntaxKind.ObjectLiteral:
184-
return getCustomIndentationFromList((<ObjectLiteral>leftSibling.parent).properties);
182+
return getCustomIndentationFromList((<ObjectLiteral>node.parent).properties);
185183
case SyntaxKind.TypeLiteral:
186-
return getCustomIndentationFromList((<TypeLiteralNode>leftSibling.parent).members);
184+
return getCustomIndentationFromList((<TypeLiteralNode>node.parent).members);
187185
case SyntaxKind.ArrayLiteral:
188-
return getCustomIndentationFromList((<ArrayLiteral>leftSibling.parent).elements);
186+
return getCustomIndentationFromList((<ArrayLiteral>node.parent).elements);
189187
case SyntaxKind.FunctionDeclaration:
190188
case SyntaxKind.FunctionExpression:
191189
case SyntaxKind.ArrowFunction:
192190
case SyntaxKind.Method:
193191
case SyntaxKind.CallSignature:
194192
case SyntaxKind.ConstructSignature:
195-
if ((<SignatureDeclaration>leftSibling.parent).typeParameters && leftSibling.end < (<SignatureDeclaration>leftSibling.parent).typeParameters.end) {
196-
return getCustomIndentationFromList((<SignatureDeclaration>leftSibling.parent).typeParameters);
193+
if ((<SignatureDeclaration>node.parent).typeParameters && node.end < (<SignatureDeclaration>node.parent).typeParameters.end) {
194+
return getCustomIndentationFromList((<SignatureDeclaration>node.parent).typeParameters);
197195
}
198196
else {
199-
return getCustomIndentationFromList((<SignatureDeclaration>leftSibling.parent).parameters);
197+
return getCustomIndentationFromList((<SignatureDeclaration>node.parent).parameters);
200198
}
201199
case SyntaxKind.NewExpression:
202200
case SyntaxKind.CallExpression:
203-
if ((<CallExpression>leftSibling.parent).typeArguments && leftSibling.end < (<CallExpression>leftSibling.parent).typeArguments.end) {
204-
return getCustomIndentationFromList((<CallExpression>leftSibling.parent).typeArguments);
201+
if ((<CallExpression>node.parent).typeArguments && node.end < (<CallExpression>node.parent).typeArguments.end) {
202+
return getCustomIndentationFromList((<CallExpression>node.parent).typeArguments);
205203
}
206204
else {
207-
return getCustomIndentationFromList((<CallExpression>leftSibling.parent).arguments);
205+
return getCustomIndentationFromList((<CallExpression>node.parent).arguments);
208206
}
209207

210208
break;
@@ -214,9 +212,9 @@ module ts.formatting {
214212
return -1;
215213

216214
function getCustomIndentationFromList(list: Node[]): number {
217-
var index = indexOf(list, leftSibling);
215+
var index = indexOf(list, node);
218216
if (index !== -1) {
219-
var lineAndCol = sourceFile.getLineAndCharacterFromPosition(leftSibling.getStart(sourceFile));
217+
var lineAndCol = sourceFile.getLineAndCharacterFromPosition(node.getStart(sourceFile));
220218
for (var i = index - 1; i >= 0; --i) {
221219
var prevLineAndCol = sourceFile.getLineAndCharacterFromPosition(list[i].getStart(sourceFile));
222220
if (lineAndCol.line !== prevLineAndCol.line) {
@@ -227,7 +225,7 @@ module ts.formatting {
227225
return i;
228226
}
229227
}
230-
// code is unreachable because the rance that we check above includes at least one non-whitespace character at the very end
228+
// code is unreachable because the range that we check above includes at least one non-whitespace character at the very end
231229
Debug.fail("Unreachable code")
232230

233231
}

tests/cases/fourslash/chainedFunctionFunctionArgIndent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//// declare var $: any;
33
//// $(".contentDiv").each(function (index, element) {/**/
44
//// // <-- ensure cursor is here after return on above
5-
//// }); // Ensure indent is 0 after LBrace
5+
//// });
66

77
goTo.marker();
88
edit.insert("\n");
99
verify.indentationIs(4);
1010
edit.insert("}");
11-
verify.indentationIs(0);
11+
verify.indentationIs(4); // keep arguments indented

0 commit comments

Comments
 (0)