diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index a0e82a143d27..1bd96bf8f244 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -33,7 +33,7 @@ public class PyBlock implements ASTBlock { private final Wrap _wrap; private final CodeStyleSettings mySettings; private List _subBlocks = null; - private final Alignment _childListAlignment; + private Alignment myChildAlignment; private static final boolean DUMP_FORMATTING_BLOCKS = false; private static final TokenSet ourListElementTypes = TokenSet.create(PyElementTypes.LIST_LITERAL_EXPRESSION, @@ -61,7 +61,6 @@ public class PyBlock implements ASTBlock { _node = node; _wrap = wrap; mySettings = settings; - _childListAlignment = Alignment.createAlignment(); } @NotNull @@ -74,6 +73,13 @@ public class PyBlock implements ASTBlock { return _node.getTextRange(); } + private Alignment getAlignmentForChildren() { + if (myChildAlignment == null) { + myChildAlignment = Alignment.createAlignment(); + } + return myChildAlignment; + } + @NotNull public List getSubBlocks() { if (_subBlocks == null) { @@ -131,9 +137,12 @@ public class PyBlock implements ASTBlock { wrap = Wrap.createWrap(WrapType.NORMAL, true); } if (needListAlignment(child)) { - childAlignment = _childListAlignment; + childAlignment = getAlignmentForChildren(); } } + else if (parentType == PyElementTypes.BINARY_EXPRESSION && PyElementTypes.EXPRESSIONS.contains(childType)) { + childAlignment = getAlignmentForChildren(); + } if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION) { if (childType == PyTokenTypes.RBRACKET || childType == PyTokenTypes.LBRACKET) { childIndent = Indent.getNoneIndent(); @@ -452,7 +461,7 @@ public class PyBlock implements ASTBlock { private Alignment getChildAlignment() { if (ourListElementTypes.contains(_node.getElementType())) { - return _childListAlignment; + return getAlignmentForChildren(); } return null; } diff --git a/python/testData/formatter/alignInBinaryExpression.py b/python/testData/formatter/alignInBinaryExpression.py new file mode 100644 index 000000000000..1e9777a37b1f --- /dev/null +++ b/python/testData/formatter/alignInBinaryExpression.py @@ -0,0 +1,2 @@ +if isintance(True, bool) and\ + isinstance(1, int): pass \ No newline at end of file diff --git a/python/testData/formatter/alignInBinaryExpression_after.py b/python/testData/formatter/alignInBinaryExpression_after.py new file mode 100644 index 000000000000..0dd55b3f3cdd --- /dev/null +++ b/python/testData/formatter/alignInBinaryExpression_after.py @@ -0,0 +1,2 @@ +if isintance(True, bool) and\ + isinstance(1, int): pass \ 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 bb21029ce74a..3d102208759c 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -76,6 +76,10 @@ public class PyFormatterTest extends PyLightFixtureTestCase { doTest(); } + public void testAlignInBinaryExpression() { + doTest(); + } + private void doTest() { myFixture.configureByFile("formatter/" + getTestName(true) + ".py"); ApplicationManager.getApplication().runWriteAction(new Runnable() {