mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 17:05:58 -05:00
getLineIndentation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user