-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Indentation fix for multi-line call expression #3179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b85d56b
3f487bb
a9eba62
4b4fc01
e20be95
50a0223
910079a
4d91fff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ module ts.formatting { | |
precedingToken.kind === SyntaxKind.TemplateHead || | ||
precedingToken.kind === SyntaxKind.TemplateMiddle || | ||
precedingToken.kind === SyntaxKind.TemplateTail; | ||
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) { | ||
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) { | ||
return 0; | ||
} | ||
|
||
|
@@ -66,6 +66,10 @@ module ts.formatting { | |
if (actualIndentation !== Value.Unknown) { | ||
return actualIndentation; | ||
} | ||
actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); | ||
if (actualIndentation !== Value.Unknown) { | ||
return actualIndentation + options.IndentSize; | ||
} | ||
|
||
previous = current; | ||
current = current.parent; | ||
|
@@ -122,6 +126,10 @@ module ts.formatting { | |
if (actualIndentation !== Value.Unknown) { | ||
return actualIndentation + indentationDelta; | ||
} | ||
actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); | ||
if (actualIndentation !== Value.Unknown) { | ||
return actualIndentation + indentationDelta; | ||
} | ||
} | ||
|
||
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line | ||
|
@@ -287,6 +295,41 @@ module ts.formatting { | |
} | ||
} | ||
|
||
function getLineIndentationWhenExpressionIsInMultiLine(node: Node, sourceFile: SourceFile, options: EditorOptions): number { | ||
// actual indentation should not be used when: | ||
// - node is close parenthesis - this is the end of the expression | ||
// - node is property access expression | ||
if (node.kind !== SyntaxKind.CloseParenToken && | ||
node.kind !== SyntaxKind.PropertyAccessExpression && | ||
node.parent && ( | ||
node.parent.kind === SyntaxKind.CallExpression || | ||
node.parent.kind === SyntaxKind.NewExpression)) { | ||
|
||
let parentExpression = (<CallExpression | NewExpression>node.parent).expression; | ||
let startingExpression = getStartingExpression(<PropertyAccessExpression | CallExpression | ElementAccessExpression>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: PropertyAccessExpression | CallExpression | ElementAccessExpression) { | ||
while (expression.expression) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrap loop body in curlies and add a check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add tests for that will touch all codepaths |
||
expression = <PropertyAccessExpression | CallExpression | ElementAccessExpression>expression.expression; | ||
return expression; | ||
} | ||
} | ||
|
||
function deriveActualIndentationFromList(list: Node[], index: number, sourceFile: SourceFile, options: EditorOptions): number { | ||
Debug.assert(index >= 0 && index < list.length); | ||
let node = list[index]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CallExpression
case is not covered by tests