diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index f934beb0b646..4c58ebfbe928 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -514,14 +514,9 @@ public class PyBlock implements ASTBlock { private boolean needListAlignment(ASTNode child) { final IElementType childType = child.getElementType(); - final ASTNode firstGrandchild = child.getFirstChildNode(); - final IElementType firstGrandchildType = firstGrandchild == null ? null : firstGrandchild.getElementType(); if (PyTokenTypes.OPEN_BRACES.contains(childType)) { return false; } - if (PyTokenTypes.OPEN_BRACES.contains(firstGrandchildType) && isEmptySequence(child)) { - return false; - } if (PyTokenTypes.CLOSE_BRACES.contains(childType)) { final ASTNode prevNonSpace = findPrevNonSpaceNode(child); if (prevNonSpace != null && @@ -549,7 +544,7 @@ public class PyBlock implements ASTBlock { if (child.getElementType() == PyTokenTypes.COMMA) { return false; } - return myContext.getPySettings().ALIGN_COLLECTIONS_AND_COMPREHENSIONS; + return myContext.getPySettings().ALIGN_COLLECTIONS_AND_COMPREHENSIONS && !hasHangingIndent(myNode.getPsi()); } @Nullable diff --git a/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple.py b/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple.py new file mode 100644 index 000000000000..1de818952c89 --- /dev/null +++ b/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple.py @@ -0,0 +1,2 @@ +xs = [(), + 42] \ No newline at end of file diff --git a/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple_after.py b/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple_after.py new file mode 100644 index 000000000000..e220a7ffae50 --- /dev/null +++ b/python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple_after.py @@ -0,0 +1,2 @@ +xs = [(), + 42] \ No newline at end of file diff --git a/python/testData/formatter/alignmentOfEmptyCollectionLiterals.py b/python/testData/formatter/alignmentOfEmptyCollectionLiterals.py new file mode 100644 index 000000000000..030759749221 --- /dev/null +++ b/python/testData/formatter/alignmentOfEmptyCollectionLiterals.py @@ -0,0 +1,4 @@ +xs = [[], + {}, + (), + 42] \ No newline at end of file diff --git a/python/testData/formatter/alignmentOfEmptyCollectionLiterals_after.py b/python/testData/formatter/alignmentOfEmptyCollectionLiterals_after.py new file mode 100644 index 000000000000..60464a7a896f --- /dev/null +++ b/python/testData/formatter/alignmentOfEmptyCollectionLiterals_after.py @@ -0,0 +1,4 @@ +xs = [[], + {}, + (), + 42] \ 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 ea0e917b9673..c8fa8b54bf7d 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -546,6 +546,10 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testAlignmentInListLiteralWhereFirstItemIsEmptyTuple() { + doTest(); + } + public void testHangingIndentInNamedArgumentValue() { doTest(); } @@ -554,6 +558,10 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testAlignmentOfEmptyCollectionLiterals() { + doTest(); + } + private CommonCodeStyleSettings getCommonSettings() { return settings().getCommonSettings(PythonLanguage.getInstance()); }