diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index dc7cee7ca3b8..a195013a2aa2 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -141,11 +141,18 @@ public class PyBlock implements ASTBlock { } else if (parentType == PyElementTypes.BINARY_EXPRESSION && (PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens().contains(childType) || PyTokenTypes.OPERATIONS.contains(childType))) { - if (grandparentType == PyElementTypes.BINARY_EXPRESSION && myParent != null) { - childAlignment = myParent.getAlignmentForChildren(); + if (isInControlStatement() ) { + PyParenthesizedExpression parens = PsiTreeUtil.getParentOfType(_node.getPsi(), PyParenthesizedExpression.class, true, + PyStatementPart.class); + childIndent = parens != null ? Indent.getNormalIndent() : Indent.getContinuationIndent(); } else { - childAlignment = getAlignmentForChildren(); + if (grandparentType == PyElementTypes.BINARY_EXPRESSION && myParent != null) { + childAlignment = myParent.getAlignmentForChildren(); + } + else { + childAlignment = getAlignmentForChildren(); + } } } @@ -162,7 +169,7 @@ public class PyBlock implements ASTBlock { childIndent = Indent.getNoneIndent(); } else { - childIndent = parentType == PyElementTypes.PARAMETER_LIST || isCallInControlStatement() + childIndent = parentType == PyElementTypes.PARAMETER_LIST || isInControlStatement() ? Indent.getContinuationIndent() : Indent.getNormalIndent(); } @@ -219,7 +226,8 @@ public class PyBlock implements ASTBlock { ASTNode prev = child.getTreePrev(); while (prev != null && prev.getElementType() == TokenType.WHITE_SPACE) { - if (prev.getText().contains("\\")) { + if (prev.getText().contains("\\") && !childIndent.equals(Indent.getContinuationIndent()) && + !childIndent.equals(Indent.getContinuationIndent(true))) { childIndent = Indent.getNormalIndent(); break; } @@ -229,7 +237,7 @@ public class PyBlock implements ASTBlock { return new PyBlock(this, child, childAlignment, childIndent, wrap, myContext); } - private boolean isCallInControlStatement() { + private boolean isInControlStatement() { return PsiTreeUtil.getParentOfType(_node.getPsi(), PyStatementPart.class, false, PyStatementList.class) != null; } @@ -472,6 +480,9 @@ public class PyBlock implements ASTBlock { @Nullable private Alignment getChildAlignment() { if (ourListElementTypes.contains(_node.getElementType())) { + if (isInControlStatement()) { + return null; + } if (_node.getPsi() instanceof PyParameterList && !myContext.getSettings().ALIGN_MULTILINE_PARAMETERS) { return null; } @@ -480,7 +491,7 @@ public class PyBlock implements ASTBlock { if (elements.length == 0) { return null; } - PyKeyValueExpression last = elements[elements.length-1]; + PyKeyValueExpression last = elements[elements.length - 1]; if (last.getValue() == null) { // incomplete return null; } diff --git a/python/testData/formatter/alignInBinaryExpression.py b/python/testData/formatter/alignInBinaryExpression.py index 1e9777a37b1f..b4e809ba3bff 100644 --- a/python/testData/formatter/alignInBinaryExpression.py +++ b/python/testData/formatter/alignInBinaryExpression.py @@ -1,2 +1,3 @@ if isintance(True, bool) and\ - isinstance(1, int): pass \ No newline at end of file + isinstance(1, int): + pass diff --git a/python/testData/formatter/alignInBinaryExpression_after.py b/python/testData/formatter/alignInBinaryExpression_after.py index dd87ebb7e168..67c5db31f6df 100644 --- a/python/testData/formatter/alignInBinaryExpression_after.py +++ b/python/testData/formatter/alignInBinaryExpression_after.py @@ -1,2 +1,3 @@ if isintance(True, bool) and \ - isinstance(1, int): pass \ No newline at end of file + isinstance(1, int): + pass diff --git a/python/testData/formatter/ifConditionContinuation.py b/python/testData/formatter/ifConditionContinuation.py new file mode 100644 index 000000000000..480226635cd5 --- /dev/null +++ b/python/testData/formatter/ifConditionContinuation.py @@ -0,0 +1,3 @@ +if (some_long_condition or + another_long_condition): + pass diff --git a/python/testData/formatter/ifConditionContinuation_after.py b/python/testData/formatter/ifConditionContinuation_after.py new file mode 100644 index 000000000000..2a968f4d0c9a --- /dev/null +++ b/python/testData/formatter/ifConditionContinuation_after.py @@ -0,0 +1,3 @@ +if (some_long_condition or + another_long_condition): + pass diff --git a/python/testData/wrap/BackslashOnWrap.after.py b/python/testData/wrap/BackslashOnWrap.after.py index 279565e6743f..65995c3264cc 100644 --- a/python/testData/wrap/BackslashOnWrap.after.py +++ b/python/testData/wrap/BackslashOnWrap.after.py @@ -1,4 +1,4 @@ def copy_location(new_node, old_node): for attr in 'lineno', 'col_offset': if attr in old_node._attributes and attr in new_node._attributes and \ - hasattr(old_node, attr): \ No newline at end of file + hasattr(old_node, attr): \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index caffdcfc185c..3a843a51a3df 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -264,6 +264,10 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testIfConditionContinuation() { // PY-8195 + doTest(); + } + public void testIndentAfterBackslash() { doTest(); }