indent using list start position

This commit is contained in:
Kagami Sascha Rosylight
2018-11-05 21:45:18 +09:00
parent 655bf203d0
commit ac517336c4
+21 -12
View File
@@ -130,7 +130,10 @@ namespace ts.formatting {
}
// check if current node is a list item - if yes, take indentation from it
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
// do not consider parent-child line sharing yet:
// function foo(a
// | preceding node 'a' does share line with its parent but indentation is expected
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options, /*listIndentsChild*/ true);
if (actualIndentation !== Value.Unknown) {
return actualIndentation;
}
@@ -171,22 +174,20 @@ namespace ts.formatting {
useActualIndentation = start < ignoreActualIndentationRange.pos || start > ignoreActualIndentationRange.end;
}
if (useActualIndentation) {
// check if current node is a list item - if yes, take indentation from it
const actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
}
const containingListOrParentStart = getContainingListOrParentStart(parent, current, sourceFile);
const parentAndChildShareLine =
containingListOrParentStart.line === currentStart.line ||
childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStart.line, sourceFile);
if (useActualIndentation) {
// check if current node is a list item - if yes, take indentation from it
let actualIndentation = getActualIndentationForListItem(current, sourceFile, options, !parentAndChildShareLine);
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
// try to fetch actual indentation for current node from source text
let actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
@@ -407,13 +408,21 @@ namespace ts.formatting {
return findColumnForFirstNonWhitespaceCharacterInLine(sourceFile.getLineAndCharacterOfPosition(list.pos), sourceFile, options);
}
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings): number {
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings, listIndentsChild: boolean): number {
if (node.parent && node.parent.kind === SyntaxKind.VariableDeclarationList) {
// VariableDeclarationList has no wrapping tokens
return Value.Unknown;
}
const containingList = getContainingList(node, sourceFile);
if (containingList) {
const index = containingList.indexOf(node);
if (index !== -1) {
return deriveActualIndentationFromList(containingList, index, sourceFile, options);
const result = deriveActualIndentationFromList(containingList, index, sourceFile, options);
if (result !== Value.Unknown) {
return result;
}
}
return getActualIndentationForListStartLine(containingList, sourceFile, options) + (listIndentsChild ? options.indentSize : 0);
}
return Value.Unknown;
}