Skip to content

Commit 85780f9

Browse files
committed
indent using list start position
1 parent 43a4526 commit 85780f9

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ namespace ts.formatting {
101101
}
102102

103103
// check if current node is a list item - if yes, take indentation from it
104-
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
104+
// do not consider parent-child line sharing yet:
105+
// function foo(a
106+
// | preceding node 'a' does share line with its parent but indentation is expected
107+
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options, /*listIndentsChild*/ true);
105108
if (actualIndentation !== Value.Unknown) {
106109
return actualIndentation;
107110
}
@@ -147,21 +150,20 @@ namespace ts.formatting {
147150
useActualIndentation = start < ignoreActualIndentationRange.pos || start > ignoreActualIndentationRange.end;
148151
}
149152

150-
if (useActualIndentation) {
151-
// check if current node is a list item - if yes, take indentation from it
152-
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
153-
if (actualIndentation !== Value.Unknown) {
154-
return actualIndentation + indentationDelta;
155-
}
156-
}
157153
parentStart = getParentStart(parent, current, sourceFile);
158154
const parentAndChildShareLine =
159155
parentStart.line === currentStart.line ||
160156
childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStart.line, sourceFile);
161157

162158
if (useActualIndentation) {
159+
// check if current node is a list item - if yes, take indentation from it
160+
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options, !parentAndChildShareLine);
161+
if (actualIndentation !== Value.Unknown) {
162+
return actualIndentation + indentationDelta;
163+
}
164+
163165
// try to fetch actual indentation for current node from source text
164-
let actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
166+
actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
165167
if (actualIndentation !== Value.Unknown) {
166168
return actualIndentation + indentationDelta;
167169
}
@@ -355,9 +357,19 @@ namespace ts.formatting {
355357
return findColumnForFirstNonWhitespaceCharacterInLine(sourceFile.getLineAndCharacterOfPosition(list.pos), sourceFile, options);
356358
}
357359

358-
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings): number {
360+
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings, listIndentsChild: boolean): number {
361+
if (node.parent && node.parent.kind === SyntaxKind.VariableDeclarationList) {
362+
// VariableDeclarationList has no wrapping tokens
363+
return Value.Unknown;
364+
}
359365
const containingList = getContainingList(node, sourceFile);
360-
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
366+
const result = containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
367+
if (containingList && result === Value.Unknown) {
368+
return getActualIndentationForListStartLine(containingList, sourceFile, options) + (listIndentsChild ? options.indentSize : 0);
369+
}
370+
else {
371+
return result;
372+
}
361373

362374
function getActualIndentationFromList(list: ReadonlyArray<Node>): number {
363375
const index = indexOf(list, node);

0 commit comments

Comments
 (0)