Skip to content

Commit f5f4af2

Browse files
committed
Merge branch 'blockFormParameterIndentation' of https://github.com/SaschaNaz/TypeScript into SaschaNaz-blockFormParameterIndentation
2 parents 30657d4 + 4d91fff commit f5f4af2

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace ts.formatting {
2626
precedingToken.kind === SyntaxKind.TemplateHead ||
2727
precedingToken.kind === SyntaxKind.TemplateMiddle ||
2828
precedingToken.kind === SyntaxKind.TemplateTail;
29-
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
29+
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
3030
return 0;
3131
}
3232

@@ -66,6 +66,10 @@ namespace ts.formatting {
6666
if (actualIndentation !== Value.Unknown) {
6767
return actualIndentation;
6868
}
69+
actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options);
70+
if (actualIndentation !== Value.Unknown) {
71+
return actualIndentation + options.IndentSize;
72+
}
6973

7074
previous = current;
7175
current = current.parent;
@@ -122,6 +126,10 @@ namespace ts.formatting {
122126
if (actualIndentation !== Value.Unknown) {
123127
return actualIndentation + indentationDelta;
124128
}
129+
actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options);
130+
if (actualIndentation !== Value.Unknown) {
131+
return actualIndentation + indentationDelta;
132+
}
125133
}
126134

127135
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
@@ -287,6 +295,41 @@ namespace ts.formatting {
287295
}
288296
}
289297

298+
function getLineIndentationWhenExpressionIsInMultiLine(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
299+
// actual indentation should not be used when:
300+
// - node is close parenthesis - this is the end of the expression
301+
// - node is property access expression
302+
if (node.kind !== SyntaxKind.CloseParenToken &&
303+
node.kind !== SyntaxKind.PropertyAccessExpression &&
304+
node.parent && (
305+
node.parent.kind === SyntaxKind.CallExpression ||
306+
node.parent.kind === SyntaxKind.NewExpression)) {
307+
308+
let parentExpression = (<CallExpression | NewExpression>node.parent).expression;
309+
let startingExpression = getStartingExpression(<PropertyAccessExpression | CallExpression | ElementAccessExpression>parentExpression);
310+
311+
if (parentExpression === startingExpression) {
312+
return Value.Unknown;
313+
}
314+
315+
let parentExpressionEnd = sourceFile.getLineAndCharacterOfPosition(parentExpression.end);
316+
let startingExpressionEnd = sourceFile.getLineAndCharacterOfPosition(startingExpression.end);
317+
318+
if (parentExpressionEnd.line === startingExpressionEnd.line) {
319+
return Value.Unknown;
320+
}
321+
322+
return findColumnForFirstNonWhitespaceCharacterInLine(parentExpressionEnd, sourceFile, options);
323+
}
324+
return Value.Unknown;
325+
326+
function getStartingExpression(expression: PropertyAccessExpression | CallExpression | ElementAccessExpression) {
327+
while (expression.expression)
328+
expression = <PropertyAccessExpression | CallExpression | ElementAccessExpression>expression.expression;
329+
return expression;
330+
}
331+
}
332+
290333
function deriveActualIndentationFromList(list: Node[], index: number, sourceFile: SourceFile, options: EditorOptions): number {
291334
Debug.assert(index >= 0 && index < list.length);
292335
let node = list[index];

tests/cases/fourslash/consistenceOnIndentionsOfChainedFunctionCalls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ goTo.marker("1");
1818
edit.insert("\r\n");
1919
goTo.marker("0");
2020
// Won't-fixed: Smart indent during chained function calls
21-
verify.indentationIs(4);
21+
verify.indentationIs(8);

tests/cases/fourslash/formattingOnChainedCallbacks.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@
1313
//// })/*b*/
1414
////}
1515

16+
////Promise
17+
//// .then(
18+
//// /*n1*/
19+
//// )
20+
//// /*n2*/
21+
//// .then();
22+
23+
1624
goTo.marker('1');
1725
edit.insertLine('');
1826
goTo.marker('2');
1927
verify.currentLineContentIs(' ""');
28+
edit.insertLine('');
29+
verify.indentationIs(8);
2030
goTo.marker('4');
2131
edit.insertLine('');
2232
goTo.marker('3');
@@ -34,4 +44,9 @@ edit.insert(';');
3444
verify.currentLineContentIs(' "";');
3545
goTo.marker('b');
3646
edit.insert(';');
37-
verify.currentLineContentIs(' });');
47+
verify.currentLineContentIs(' });');
48+
49+
goTo.marker('n1');
50+
verify.indentationIs(8);
51+
goTo.marker('n2');
52+
verify.indentationIs(4);

0 commit comments

Comments
 (0)