Fix align multiline elements in parenthesized expressions, parameters and arguments lists.

This commit is contained in:
Dmitry Trofimov
2014-01-23 22:12:30 +01:00
parent 74c850caf7
commit 4db90932c0
9 changed files with 94 additions and 14 deletions
@@ -191,16 +191,6 @@ public class PyBlock implements ASTBlock {
childIndent = Indent.getNormalIndent();
}
}
else if (parentType == PyElementTypes.ARGUMENT_LIST || parentType == PyElementTypes.PARAMETER_LIST) {
if (childType == PyTokenTypes.RPAR) {
childIndent = Indent.getNoneIndent();
}
else {
childIndent = parentType == PyElementTypes.PARAMETER_LIST || isInControlStatement()
? Indent.getContinuationIndent()
: Indent.getNormalIndent(/*true*/);
}
}
else if (parentType == PyElementTypes.DICT_LITERAL_EXPRESSION || parentType == PyElementTypes.SET_LITERAL_EXPRESSION ||
parentType == PyElementTypes.SET_COMP_EXPRESSION || parentType == PyElementTypes.DICT_COMP_EXPRESSION) {
if (childType == PyTokenTypes.RBRACE || !hasLineBreaksBefore(child, 1)) {
@@ -236,7 +226,25 @@ public class PyBlock implements ASTBlock {
childIndent = Indent.getNormalIndent();
}
}
else if (parentType == PyElementTypes.PARENTHESIZED_EXPRESSION || parentType == PyElementTypes.GENERATOR_EXPRESSION) {
//Align elements vertically if there is an argument in the first line of parenthesized expression
else if (((parentType == PyElementTypes.PARENTHESIZED_EXPRESSION && myContext.getSettings().ALIGN_MULTILINE_PARENTHESIZED_EXPRESSION)
|| (parentType == PyElementTypes.ARGUMENT_LIST && myContext.getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS)
|| (parentType == PyElementTypes.PARAMETER_LIST && myContext.getSettings().ALIGN_MULTILINE_PARAMETERS)) &&
!isIndentNext(child) &&
!hasLineBreaksBefore(_node.getFirstChildNode(), 1)
&& !ourListElementTypes.contains(childType)) {
if (!ourBrackets.contains(childType)) {
childAlignment = getAlignmentForChildren();
if (parentType != PyElementTypes.CALL_EXPRESSION) {
childIndent = Indent.getNormalIndent();
}
}
else if (childType == PyTokenTypes.RPAR) {
childIndent = Indent.getNoneIndent();
}
}
else if (parentType == PyElementTypes.GENERATOR_EXPRESSION || parentType == PyElementTypes.PARENTHESIZED_EXPRESSION) {
if (childType == PyTokenTypes.RPAR || !hasLineBreaksBefore(child, 1)) {
childIndent = Indent.getNoneIndent();
}
@@ -244,6 +252,16 @@ public class PyBlock implements ASTBlock {
childIndent = isIndentNext(child) ? Indent.getContinuationIndent() : Indent.getNormalIndent();
}
}
else if (parentType == PyElementTypes.ARGUMENT_LIST || parentType == PyElementTypes.PARAMETER_LIST) {
if (childType == PyTokenTypes.RPAR) {
childIndent = Indent.getNoneIndent();
}
else {
childIndent = parentType == PyElementTypes.PARAMETER_LIST || isInControlStatement()
? Indent.getContinuationIndent()
: Indent.getNormalIndent(/*true*/);
}
}
else if (parentType == PyElementTypes.SUBSCRIPTION_EXPRESSION) {
PyExpression indexExpression = ((PySubscriptionExpression)_node.getPsi()).getIndexExpression();
if (indexExpression != null && child == indexExpression.getNode()) {
@@ -254,9 +272,21 @@ public class PyBlock implements ASTBlock {
if (child != _node.getFirstChildNode()) {
childIndent = Indent.getNormalIndent();
if (hasLineBreaksBefore(child, 1)) {
if (isIndentNext(child)) {
if (isInControlStatement()) {
childIndent = Indent.getContinuationIndent();
}
else {
PyBlock b = myParent;
while (b != null) {
if (b.getNode().getPsi() instanceof PyParenthesizedExpression ||
b.getNode().getPsi() instanceof PyArgumentList ||
b.getNode().getPsi() instanceof PyParameterList) {
childAlignment = getAlignmentOfChild(b, 1);
break;
}
b = b.myParent;
}
}
}
}
}
@@ -279,6 +309,14 @@ public class PyBlock implements ASTBlock {
return new PyBlock(this, child, childAlignment, childIndent, wrap, myContext);
}
private static Alignment getAlignmentOfChild(PyBlock b, int childNum) {
if (b.getSubBlocks().size() > childNum) {
ChildAttributes attributes = b.getChildAttributes(childNum);
return attributes.getAlignment();
}
return null;
}
private static boolean isIndentNext(ASTNode child) {
PsiElement psi = PsiTreeUtil.getParentOfType(child.getPsi(), PyStatement.class);
@@ -0,0 +1,3 @@
foo(1 +
x.
call())
@@ -0,0 +1,3 @@
foo(1 +
x.
call())
@@ -0,0 +1,4 @@
def foo(x=
y.
call()):
pass
@@ -0,0 +1,4 @@
def foo(x=
y.
call()):
pass
@@ -0,0 +1,8 @@
print(1 + (1 + (1
+
x
.calc())
+
x.
calc()
))
@@ -0,0 +1,8 @@
print(1 + (1 + (1
+
x
.calc())
+
x.
calc()
))
+1 -1
View File
@@ -1,2 +1,2 @@
sitesettings = call(settings_manager
.get_whitelabel_settings_by_site_id(myuser.site_id))
.get_whitelabel_settings_by_site_id(myuser.site_id))
@@ -299,6 +299,10 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
public void testAlignInCallExpression() {
doTest();
}
public void testContinuationIndentForCallInStatementPart() { // PY-8577
doTest();
}
@@ -307,7 +311,7 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
public void _testIndentInNestedCall() { // PY-8195
public void _testIndentInNestedCall() { // PY-11906 TODO: fix indent
doTest();
}
@@ -377,6 +381,14 @@ public class PyFormatterTest extends PyTestCase {
public void testIndentParensInImport() { // PY-9075
doTest();
}
public void testAlignInParenthesizedExpression() {
doTest();
}
public void testAlignInParameterList() {
doTest();
}
private void doTest() {
doTest(false);