diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index e15085bfda99..1085b7a18f5c 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -258,7 +258,7 @@ public class PyBlock implements ASTBlock { while (prev != null && prev.getElementType() == TokenType.WHITE_SPACE) { if (prev.getText().contains("\\") && !childIndent.equals(Indent.getContinuationIndent()) && !childIndent.equals(Indent.getContinuationIndent(true))) { - childIndent = Indent.getNormalIndent(); + childIndent = isIndentNext(child) ? Indent.getContinuationIndent() : Indent.getNormalIndent(); break; } prev = prev.getTreePrev(); @@ -267,6 +267,19 @@ public class PyBlock implements ASTBlock { return new PyBlock(this, child, childAlignment, childIndent, wrap, myContext); } + private static boolean isIndentNext(ASTNode child) { + PsiElement psi = PsiTreeUtil.getParentOfType(child.getPsi(), PyStatement.class); + + return psi instanceof PyIfStatement || + psi instanceof PyForStatement || + psi instanceof PyWithStatement || + psi instanceof PyClass || + psi instanceof PyFunction || + psi instanceof PyTryExceptStatement || + psi instanceof PyElsePart || + psi instanceof PyIfPart; + } + private static boolean isSubscriptionOperand(ASTNode child) { return child.getTreeParent().getElementType() == PyElementTypes.SUBSCRIPTION_EXPRESSION && child.getPsi() == ((PySubscriptionExpression)child.getTreeParent().getPsi()).getOperand(); @@ -438,7 +451,7 @@ public class PyBlock implements ASTBlock { if ((node1.getElementType() == PyElementTypes.FUNCTION_DECLARATION || node1.getElementType() == PyElementTypes.CLASS_DECLARATION) && _node.getElementType() instanceof PyFileElementType) { - + if (psi2 instanceof PsiComment) { final PsiElement psi3 = PsiTreeUtil.getNextSiblingOfType(psi2, PyElement.class); diff --git a/python/testData/formatter/continuationIndentAfterIf.py b/python/testData/formatter/continuationIndentAfterIf.py deleted file mode 100644 index 202df29316d4..000000000000 --- a/python/testData/formatter/continuationIndentAfterIf.py +++ /dev/null @@ -1,4 +0,0 @@ -def is_false(value): - if value is None \ - or value == 0: - return False \ No newline at end of file diff --git a/python/testData/formatter/continuationIndentAfterIf_after.py b/python/testData/formatter/continuationIndentAfterIf_after.py deleted file mode 100644 index 7f5ad966213b..000000000000 --- a/python/testData/formatter/continuationIndentAfterIf_after.py +++ /dev/null @@ -1,4 +0,0 @@ -def is_false(value): - if value is None \ - or value == 0: - return False \ No newline at end of file diff --git a/python/testData/formatter/continuationIndentInIndentingStatement.py b/python/testData/formatter/continuationIndentInIndentingStatement.py new file mode 100644 index 000000000000..5822bc4971bb --- /dev/null +++ b/python/testData/formatter/continuationIndentInIndentingStatement.py @@ -0,0 +1,31 @@ +if True \ + or False: + pass +elif \ + False: + pass + +for i in \ + range(1, 100): + pass + +with open('file1') as file1, \ + open('file2') as file2: + pass + + +class \ + A(object): + pass + + +def \ + foo(): + pass + + +try: + pass +except \ + AttributeError: + pass \ No newline at end of file diff --git a/python/testData/formatter/continuationIndentInIndentingStatement_after.py b/python/testData/formatter/continuationIndentInIndentingStatement_after.py new file mode 100644 index 000000000000..f38dd5199edd --- /dev/null +++ b/python/testData/formatter/continuationIndentInIndentingStatement_after.py @@ -0,0 +1,31 @@ +if True \ + or False: + pass +elif \ + False: + pass + +for i in \ + range(1, 100): + pass + +with open('file1') as file1, \ + open('file2') as file2: + pass + + +class \ + A(object): + pass + + +def \ + foo(): + pass + + +try: + pass +except \ + AttributeError: + 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 86e3431144be..f7290d328ee1 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -203,7 +203,7 @@ public class PyFormatterTest extends PyTestCase { doTest(); } - public void testContinuationIndentAfterIf() { // PY-9573 + public void testContinuationIndentInIndentingStatement() { // PY-9573 doTest(); }