From 38f058d76f7a005712b6fcb8e36cfe16ab09fadb Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 6 Nov 2013 19:15:52 +0100 Subject: [PATCH] failing test for PY-10319 --- .../com/jetbrains/python/formatter/PyBlock.java | 7 ++++++- python/testData/formatter/wrapBeforeElse.py | 1 + .../testData/formatter/wrapBeforeElse_after.py | 2 ++ .../com/jetbrains/python/PyFormatterTest.java | 17 ++++++++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 python/testData/formatter/wrapBeforeElse.py create mode 100644 python/testData/formatter/wrapBeforeElse_after.py diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index ab03c469a26d..6d343e2f301d 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -152,7 +152,7 @@ public class PyBlock implements ASTBlock { if (ourListElementTypes.contains(parentType)) { // wrapping in non-parenthesized tuple expression is not allowed (PY-1792) if ((parentType != PyElementTypes.TUPLE_EXPRESSION || grandparentType == PyElementTypes.PARENTHESIZED_EXPRESSION) && - !ourBrackets.contains(childType) && childType != PyTokenTypes.COMMA && !isSliceOperand(child)) { + !ourBrackets.contains(childType) && childType != PyTokenTypes.COMMA && !isSliceOperand(child) /*&& !isSubscriptionOperand(child)*/) { wrap = Wrap.createWrap(WrapType.NORMAL, true); } if (needListAlignment(child) && !isEmptyList(_node.getPsi())) { @@ -257,6 +257,11 @@ public class PyBlock implements ASTBlock { return new PyBlock(this, child, childAlignment, childIndent, wrap, myContext); } + private static boolean isSubscriptionOperand(ASTNode child) { + return child.getTreeParent().getElementType() == PyElementTypes.SUBSCRIPTION_EXPRESSION && + child.getPsi() == ((PySubscriptionExpression) child.getTreeParent().getPsi()).getOperand(); + } + private boolean isInControlStatement() { return PsiTreeUtil.getParentOfType(_node.getPsi(), PyStatementPart.class, false, PyStatementList.class) != null || PsiTreeUtil.getParentOfType(_node.getPsi(), PyWithItem.class) != null; diff --git a/python/testData/formatter/wrapBeforeElse.py b/python/testData/formatter/wrapBeforeElse.py new file mode 100644 index 000000000000..6e3f8c915476 --- /dev/null +++ b/python/testData/formatter/wrapBeforeElse.py @@ -0,0 +1 @@ +id = 1 if looooooooooooooooooooooooong_vaaaaaaaaaaaaaaaar == 'loooooooooooooooong_vaaaaaaaaaaaaaaaaaaaaaaaaaalue' else list('foo')[0] \ No newline at end of file diff --git a/python/testData/formatter/wrapBeforeElse_after.py b/python/testData/formatter/wrapBeforeElse_after.py new file mode 100644 index 000000000000..216c191f4b50 --- /dev/null +++ b/python/testData/formatter/wrapBeforeElse_after.py @@ -0,0 +1,2 @@ +id = 1 if looooooooooooooooooooooooong_vaaaaaaaaaaaaaaaar == 'loooooooooooooooong_vaaaaaaaaaaaaaaaaaaaaaaaaaalue' else list( + 'foo')[0] \ 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 d0275d5681f8..6cedef2af10b 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -337,12 +337,27 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void _testWrapBeforeElse() { // PY-10319 + doTest(true); + } + private void doTest() { + doTest(false); + } + + private void doTest(final boolean reformatText) { myFixture.configureByFile("formatter/" + getTestName(true) + ".py"); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { - CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile()); + CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myFixture.getProject()); + PsiFile file = myFixture.getFile(); + if (reformatText) { + codeStyleManager.reformatText(file, 0, file.getTextLength()); + } + else { + codeStyleManager.reformat(file); + } } }); myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.py");