diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index c46601c85d27..f471765e298b 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -241,7 +241,8 @@ public class PyBlock implements ASTBlock { } } - if (isAfterStatementList(child) && !hasLineBreaksBefore(child, 2)) { // maybe enter was pressed and cut us from a previous (nested) statement list + if (isAfterStatementList(child) && !hasLineBreaksBefore(child, 2) && child.getElementType() != PyTokenTypes.END_OF_LINE_COMMENT) { + // maybe enter was pressed and cut us from a previous (nested) statement list childIndent = Indent.getNormalIndent(); } diff --git a/python/testData/formatter/commentAfterBlock.py b/python/testData/formatter/commentAfterBlock.py new file mode 100644 index 000000000000..fd8584a4f2c4 --- /dev/null +++ b/python/testData/formatter/commentAfterBlock.py @@ -0,0 +1,5 @@ +def test(): + if bar: + foo() + # comment + foobar() \ No newline at end of file diff --git a/python/testData/formatter/commentAfterBlock_after.py b/python/testData/formatter/commentAfterBlock_after.py new file mode 100644 index 000000000000..fd8584a4f2c4 --- /dev/null +++ b/python/testData/formatter/commentAfterBlock_after.py @@ -0,0 +1,5 @@ +def test(): + if bar: + foo() + # comment + foobar() \ 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 4b77c179a158..f6f671602d0f 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -350,6 +350,10 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testCommentAfterBlock() { // PY-9542 + doTest(); + } + private void doTest() { doTest(false); }