Skip to content

Commit ac51733

Browse files
committed
indent using list start position
1 parent 655bf20 commit ac51733

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ namespace ts.formatting {
130130
}
131131

132132
// check if current node is a list item - if yes, take indentation from it
133-
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
133+
// do not consider parent-child line sharing yet:
134+
// function foo(a
135+
// | preceding node 'a' does share line with its parent but indentation is expected
136+
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options, /*listIndentsChild*/ true);
134137
if (actualIndentation !== Value.Unknown) {
135138
return actualIndentation;
136139
}
@@ -171,22 +174,20 @@ namespace ts.formatting {
171174
useActualIndentation = start < ignoreActualIndentationRange.pos || start > ignoreActualIndentationRange.end;
172175
}
173176

174-
if (useActualIndentation) {
175-
// check if current node is a list item - if yes, take indentation from it
176-
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
177-
if (actualIndentation !== Value.Unknown) {
178-
return actualIndentation + indentationDelta;
179-
}
180-
}
181-
182177
const containingListOrParentStart = getContainingListOrParentStart(parent, current, sourceFile);
183178
const parentAndChildShareLine =
184179
containingListOrParentStart.line === currentStart.line ||
185180
childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStart.line, sourceFile);
186181

187182
if (useActualIndentation) {
183+
// check if current node is a list item - if yes, take indentation from it
184+
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options, !parentAndChildShareLine);
185+
if (actualIndentation !== Value.Unknown) {
186+
return actualIndentation + indentationDelta;
187+
}
188+
188189
// try to fetch actual indentation for current node from source text
189-
let actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
190+
actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
190191
if (actualIndentation !== Value.Unknown) {
191192
return actualIndentation + indentationDelta;
192193
}
@@ -407,13 +408,21 @@ namespace ts.formatting {
407408
return findColumnForFirstNonWhitespaceCharacterInLine(sourceFile.getLineAndCharacterOfPosition(list.pos), sourceFile, options);
408409
}
409410

410-
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings): number {
411+
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings, listIndentsChild: boolean): number {
412+
if (node.parent && node.parent.kind === SyntaxKind.VariableDeclarationList) {
413+
// VariableDeclarationList has no wrapping tokens
414+
return Value.Unknown;
415+
}
411416
const containingList = getContainingList(node, sourceFile);
412417
if (containingList) {
413418
const index = containingList.indexOf(node);
414419
if (index !== -1) {
415-
return deriveActualIndentationFromList(containingList, index, sourceFile, options);
420+
const result = deriveActualIndentationFromList(containingList, index, sourceFile, options);
421+
if (result !== Value.Unknown) {
422+
return result;
423+
}
416424
}
425+
return getActualIndentationForListStartLine(containingList, sourceFile, options) + (listIndentsChild ? options.indentSize : 0);
417426
}
418427
return Value.Unknown;
419428
}

0 commit comments

Comments
 (0)