From ff28809f20a030515ac1903c3a5ae3075a4f1f29 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Thu, 23 Apr 2015 19:49:08 +0300 Subject: [PATCH] Remove formatter logic that prevented proper alignment of empty collection literals It was added a while back for PY-6751, but corresponding test passes without it now. Also it turned out that PyIndentTest#testEnterInDictInTuple depended on this change, but behavior tested there was recreated (once again) by proper use of hasHangingIndent in formatter, i.e. once opening and closing braces of the dict are on different lines, the literal should not have alignment. --- python/src/com/jetbrains/python/formatter/PyBlock.java | 7 +------ .../alignmentInListLiteralWhereFirstItemIsEmptyTuple.py | 2 ++ ...gnmentInListLiteralWhereFirstItemIsEmptyTuple_after.py | 2 ++ .../formatter/alignmentOfEmptyCollectionLiterals.py | 4 ++++ .../formatter/alignmentOfEmptyCollectionLiterals_after.py | 4 ++++ python/testSrc/com/jetbrains/python/PyFormatterTest.java | 8 ++++++++ 6 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple.py create mode 100644 python/testData/formatter/alignmentInListLiteralWhereFirstItemIsEmptyTuple_after.py create mode 100644 python/testData/formatter/alignmentOfEmptyCollectionLiterals.py create mode 100644 python/testData/formatter/alignmentOfEmptyCollectionLiterals_after.py 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()); }