From 15cf5b06f6b3950c2c863717335dedae2baaee95 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 27 Apr 2015 17:43:12 +0300 Subject: [PATCH] PY-12360 Do not add extra blank line at the end of empty file --- .../formatter/PyTrailingBlankLinesPostFormatProcessor.java | 2 +- python/testData/formatter/trailingBlankLinesInEmptyFile.py | 0 .../formatter/trailingBlankLinesInEmptyFile_after.py | 0 python/testSrc/com/jetbrains/python/PyFormatterTest.java | 5 +++++ 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 python/testData/formatter/trailingBlankLinesInEmptyFile.py create mode 100644 python/testData/formatter/trailingBlankLinesInEmptyFile_after.py diff --git a/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java b/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java index c3f475f15905..aad5b31e68cf 100644 --- a/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java +++ b/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java @@ -110,7 +110,7 @@ public class PyTrailingBlankLinesPostFormatProcessor implements PostFormatProces } documentManager.doPostponedOperationsAndUnblockDocument(document); final String text = StringUtil.repeat("\n", numLineFeeds); - if (numLineFeeds > 0) { + if (numLineFeeds > 0 && whitespaceRange.getStartOffset() != 0) { if (!whitespaceRange.isEmpty()) { document.replaceString(whitespaceRange.getStartOffset(), whitespaceRange.getEndOffset(), text); } diff --git a/python/testData/formatter/trailingBlankLinesInEmptyFile.py b/python/testData/formatter/trailingBlankLinesInEmptyFile.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testData/formatter/trailingBlankLinesInEmptyFile_after.py b/python/testData/formatter/trailingBlankLinesInEmptyFile_after.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index 44a4c5eb3fda..02c62a2113c2 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -562,6 +562,11 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + // PY-11552 + public void testTrailingBlankLinesInEmptyFile() { + doTest(); + } + // PY-15530 public void testAlignmentInArgumentListWhereFirstArgumentIsEmptyCall() { doTest();