diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 42584d1c7504..8b05d34e6155 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -209,13 +209,8 @@ public class PyBlock implements ASTBlock { return false; } if (PyTokenTypes.CLOSE_BRACES.contains(childType)) { - PsiElement psi = child.getPsi(); - PyArgumentList argumentList = PsiTreeUtil.getParentOfType(psi, PyArgumentList.class); - if (argumentList != null) { - if (psi != null && psi.getParent() == argumentList && - (child.getElementType() == PyTokenTypes.RPAR || argumentList.getArguments().length == 1)) { - return false; - } + ASTNode prevNonSpace = findPrevNonSpaceNode(child); + if (prevNonSpace != null && prevNonSpace.getElementType() == PyTokenTypes.COMMA) { return true; } return false; @@ -227,6 +222,14 @@ public class PyBlock implements ASTBlock { return true; } + @Nullable + private static ASTNode findPrevNonSpaceNode(ASTNode node) { + do { + node = node.getTreePrev(); + } while(node != null && (node.getElementType() == TokenType.WHITE_SPACE || PyTokenTypes.WHITESPACE.contains(node.getElementType()))); + return node; + } + private static boolean hasLineBreaksBefore(ASTNode child, int minCount) { return isWhitespaceWithLineBreaks(TreeUtil.findLastLeaf(child.getTreePrev()), minCount) || isWhitespaceWithLineBreaks(child.getFirstChildNode(), minCount); diff --git a/python/testSrc/com/jetbrains/python/PyIndentTest.java b/python/testSrc/com/jetbrains/python/PyIndentTest.java index b46bc7a20ecf..cb224633ef9a 100644 --- a/python/testSrc/com/jetbrains/python/PyIndentTest.java +++ b/python/testSrc/com/jetbrains/python/PyIndentTest.java @@ -71,6 +71,16 @@ public class PyIndentTest extends PyLightFixtureTestCase { doTest("__all__ = [a for", "__all__ = [a for\n" + " "); } + public void testAlignInListOnceMore() { // PY-2407 + doTest("for id in [\"SEARCH_RESULT_ATTRIBUTES\", \n" + + " \"WRITE_SEARCH_RESULT_ATTRIBUTES\", \n" + + " \"IDENTIFIER_UNDER_CARET_ATTRIBUTES\",]:", + "for id in [\"SEARCH_RESULT_ATTRIBUTES\", \n" + + " \"WRITE_SEARCH_RESULT_ATTRIBUTES\", \n" + + " \"IDENTIFIER_UNDER_CARET_ATTRIBUTES\",\n" + + " ]:"); + } + public void testAlignInDict() { doTest("some_call({'aaa': 'v1',})", "some_call({'aaa': 'v1',\n" +