diff --git a/python/python-psi-impl/src/com/jetbrains/python/formatter/PyBlock.java b/python/python-psi-impl/src/com/jetbrains/python/formatter/PyBlock.java index 34893a661ee1..6fc5830aff2f 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/python-psi-impl/src/com/jetbrains/python/formatter/PyBlock.java @@ -831,7 +831,7 @@ public class PyBlock implements ASTBlock { return getBlankLinesForOption(pySettings.BLANK_LINES_BEFORE_FIRST_METHOD); } } - if (childType1 == PyTokenTypes.COLON && needLineBreakInStatement()) { + if (childType1 == PyTokenTypes.COLON && (needLineBreakInStatement())) { return Spacing.createSpacing(0, 0, 1, true, settings.KEEP_BLANK_LINES_IN_CODE); } } @@ -935,11 +935,16 @@ public class PyBlock implements ASTBlock { } private boolean needLineBreakInStatement() { - final PyStatement statement = PsiTreeUtil.getParentOfType(myNode.getPsi(), PyStatement.class); - if (statement != null) { - final Collection parts = PsiTreeUtil.collectElementsOfType(statement, PyStatementPart.class); - return (parts.size() == 1 && myContext.getPySettings().NEW_LINE_AFTER_COLON) || - (parts.size() > 1 && myContext.getPySettings().NEW_LINE_AFTER_COLON_MULTI_CLAUSE); + if (myNode.getPsi() instanceof PyStatementListContainer) { + final PyStatement statement = PsiTreeUtil.getParentOfType(myNode.getPsi(), PyStatement.class); + if (statement != null) { + final Collection parts = PsiTreeUtil.collectElementsOfType(statement, PyStatementPart.class); + return (parts.size() == 1 && myContext.getPySettings().NEW_LINE_AFTER_COLON) || + (parts.size() > 1 && myContext.getPySettings().NEW_LINE_AFTER_COLON_MULTI_CLAUSE); + } + else { + return myContext.getPySettings().NEW_LINE_AFTER_COLON; + } } return false; } diff --git a/python/testData/formatter/pep8MultipleStatementsOnOneLine.py b/python/testData/formatter/pep8MultipleStatementsOnOneLine.py new file mode 100644 index 000000000000..16fd5d2e6ba7 --- /dev/null +++ b/python/testData/formatter/pep8MultipleStatementsOnOneLine.py @@ -0,0 +1,9 @@ +class Foo: pass + +if x > 5: y = 10 + +def f(): pass + +def mul(x, y): return x * y + +with foo() as foo: pass \ No newline at end of file diff --git a/python/testData/formatter/pep8MultipleStatementsOnOneLine_after.py b/python/testData/formatter/pep8MultipleStatementsOnOneLine_after.py new file mode 100644 index 000000000000..667b803df2f6 --- /dev/null +++ b/python/testData/formatter/pep8MultipleStatementsOnOneLine_after.py @@ -0,0 +1,18 @@ +class Foo: + pass + + +if x > 5: + y = 10 + + +def f(): + pass + + +def mul(x, y): + return x * y + + +with foo() as foo: + pass diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index 894a4b477253..34e15261c6d9 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -46,6 +46,12 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + // PY-35936 + public void testPep8MultipleStatementsOnOneLine() { + getPythonCodeStyleSettings().NEW_LINE_AFTER_COLON = true; + doTest(); + } + public void testUnaryMinus() { doTest(); }