getLineIndentation

This commit is contained in:
SaschaNaz
2015-05-16 16:24:50 +09:00
parent 3f487bb0dd
commit a9eba62b68

View File

@@ -110,12 +110,6 @@ module ts.formatting {
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
// check if current node is a block-form item - if yes, take indentation from it
actualIndentation = getActualIndentationForBlockFormItem(current, sourceFile, options);
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
}
parentStart = getParentStart(parent, current, sourceFile);
let parentAndChildShareLine =
@@ -283,23 +277,48 @@ module ts.formatting {
return undefined;
}
function getActualIndentationForBlockFormItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
if (isPassableBlockForm(node.kind)) {
let firstChild = node.getChildAt(0);
let lineAndCharacter = getStartLineAndCharacterForNode(firstChild, sourceFile);
return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options);
}
return Value.Unknown;
}
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
let containingList = getContainingList(node, sourceFile);
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
if (containingList) {
let lineIndentation = getLineIndentationWhenExpressionIsInMultiLine();
if (lineIndentation !== Value.Unknown)
return lineIndentation;
return getActualIndentationFromList(containingList);
}
return Value.Unknown;
function getActualIndentationFromList(list: Node[]): number {
let index = indexOf(list, node);
return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : Value.Unknown;
}
function getLineIndentationWhenExpressionIsInMultiLine() {
if (node.parent.kind === SyntaxKind.CallExpression) {
let parentExpression = (<CallExpression>node.parent).expression;
let startingExpression = getStartingExpression(<CallExpression>parentExpression);
if (parentExpression === startingExpression) {
return Value.Unknown;
}
let parentExpressionEnd = sourceFile.getLineAndCharacterOfPosition(parentExpression.end);
let startingExpressionEnd = sourceFile.getLineAndCharacterOfPosition(startingExpression.end);
if (parentExpressionEnd.line === startingExpressionEnd.line) {
return Value.Unknown;
}
return findColumnForFirstNonWhitespaceCharacterInLine(parentExpressionEnd, sourceFile, options);
}
return Value.Unknown;
}
function getStartingExpression(expression: CallExpression) {
while (expression.expression)
expression = <CallExpression>expression.expression;
return expression;
}
}
function deriveActualIndentationFromList(list: Node[], index: number, sourceFile: SourceFile, options: EditorOptions): number {
@@ -415,16 +434,5 @@ module ts.formatting {
return false;
}
}
export function isPassableBlockForm(kind: SyntaxKind): boolean {
switch (kind) {
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ObjectLiteralExpression:
return true;
}
return false;
}
}
}