Skip to content

Commit b85d56b

Browse files
committed
block form indentation
1 parent 28c9ff1 commit b85d56b

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/services/formatting/smartIndenter.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module 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

@@ -110,6 +110,12 @@ module ts.formatting {
110110
if (actualIndentation !== Value.Unknown) {
111111
return actualIndentation + indentationDelta;
112112
}
113+
114+
// check if current node is a block-form item - if yes, take indentation from it
115+
actualIndentation = getActualIndentationForBlockFormItem(current, sourceFile, options);
116+
if (actualIndentation !== Value.Unknown) {
117+
return actualIndentation + indentationDelta;
118+
}
113119
}
114120
parentStart = getParentStart(parent, current, sourceFile);
115121
let parentAndChildShareLine =
@@ -277,6 +283,15 @@ module ts.formatting {
277283
return undefined;
278284
}
279285

286+
function getActualIndentationForBlockFormItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
287+
if (isPassableBlockForm(node.kind)) {
288+
let firstChild = node.getChildAt(0);
289+
let lineAndCharacter = getStartLineAndCharacterForNode(firstChild, sourceFile);
290+
return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options);
291+
}
292+
return Value.Unknown;
293+
}
294+
280295
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
281296
let containingList = getContainingList(node, sourceFile);
282297
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
@@ -400,5 +415,16 @@ module ts.formatting {
400415
return false;
401416
}
402417
}
418+
419+
export function isPassableBlockForm(kind: SyntaxKind): boolean {
420+
switch (kind) {
421+
case SyntaxKind.ArrowFunction:
422+
case SyntaxKind.FunctionExpression:
423+
case SyntaxKind.ArrayLiteralExpression:
424+
case SyntaxKind.ObjectLiteralExpression:
425+
return true;
426+
}
427+
return false;
428+
}
403429
}
404430
}

tests/cases/fourslash/formattingOnChainedCallbacks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ goTo.marker('1');
1717
edit.insertLine('');
1818
goTo.marker('2');
1919
verify.currentLineContentIs(' ""');
20+
edit.insertLine('');
21+
verify.indentationIs(8);
2022
goTo.marker('4');
2123
edit.insertLine('');
2224
goTo.marker('3');

0 commit comments

Comments
 (0)