From 9ade99af07e7ecb423e3c3da588687180accea97 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 27 Apr 2015 17:31:17 +0300 Subject: [PATCH] PY-12360 Cleanup, improve JavaDoc, give better name to the new code style option --- .../python/formatter/PyCodeStyleSettings.java | 4 +-- .../PyLanguageCodeStyleSettingsProvider.java | 2 +- ...TrailingBlankLinesPostFormatProcessor.java | 27 ++++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java index 3bf9855ff9e3..c060b782d1ad 100644 --- a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java +++ b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java @@ -31,8 +31,8 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings { public boolean SPACE_BEFORE_BACKSLASH = true; public int BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS = 2; - // TODO make boolean - public int NEW_LINE_AT_FILE_END = 1; + // TODO make boolean (it needs special editor in BlankLinesPanel) + public int BLANKS_LINES_AT_FILE_END = 1; public boolean ALIGN_COLLECTIONS_AND_COMPREHENSIONS = true; public boolean ALIGN_MULTILINE_IMPORTS = true; diff --git a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java index 3780a5c1bcb4..ed213eaf36a9 100644 --- a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java +++ b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java @@ -88,7 +88,7 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin consumer.showCustomOption(PyCodeStyleSettings.class, "BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS", "Around top-level classes and functions:", BLANK_LINES); - consumer.showCustomOption(PyCodeStyleSettings.class, "NEW_LINE_AT_FILE_END", "New line at the end of file", BLANK_LINES); + consumer.showCustomOption(PyCodeStyleSettings.class, "BLANKS_LINES_AT_FILE_END", "Blank lines at the end of file", BLANK_LINES); } else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) { consumer.showStandardOptions("RIGHT_MARGIN", diff --git a/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java b/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java index 597979169134..c3f475f15905 100644 --- a/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java +++ b/python/src/com/jetbrains/python/formatter/PyTrailingBlankLinesPostFormatProcessor.java @@ -30,12 +30,15 @@ import com.jetbrains.python.psi.PyFile; import org.jetbrains.annotations.NotNull; /** - * Strip trailing extra blank lines at the end of the file and insert necessary line feed if corresponding whitespace element belongs to - * formatted range/element. If "Add new line at the file end" option was selected in Python code style settings final whitespace is replaced - * by single line feed, and it removed completely otherwise. - *

- * Note however that if option {@link EditorSettingsExternalizable#isEnsureNewLineAtEOF()} was also enabled line feed will be added at the - * end of file on next "Save" action regardless of the code style settings for Python. + * Handles extra blank lines at the end of the file if corresponding whitespace elements belong to formatted range/element. + * These trailing whitespaces are replaced by line feeds if either: + *

+ * If none of these conditions holds, blank lines are removed completely. * * @author Mikhail Golubev */ @@ -59,7 +62,7 @@ public class PyTrailingBlankLinesPostFormatProcessor implements PostFormatProces } final TextRange oldWhitespaceRange = findTrailingWhitespacesRange(source); if (rangeToReformat.intersects(oldWhitespaceRange)) { - final TextRange newWhitespaceRange = replaceOrDeleteTrailingWhitespaces((PyFile)source, oldWhitespaceRange);; + final TextRange newWhitespaceRange = replaceOrDeleteTrailingWhitespaces((PyFile)source, oldWhitespaceRange); final int delta = newWhitespaceRange.getLength() - oldWhitespaceRange.getLength(); if (newWhitespaceRange.contains(oldWhitespaceRange)) { return newWhitespaceRange; @@ -101,13 +104,13 @@ public class PyTrailingBlankLinesPostFormatProcessor implements PostFormatProces final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final Document document = documentManager.getDocument(pyFile); if (document != null) { - int numLineFeedsAtEnd = CodeStyleSettingsManager.getSettings(project).getCustomSettings(PyCodeStyleSettings.class).NEW_LINE_AT_FILE_END; - if (numLineFeedsAtEnd <= 0 && EditorSettingsExternalizable.getInstance().isEnsureNewLineAtEOF()) { - numLineFeedsAtEnd = 1; + int numLineFeeds = CodeStyleSettingsManager.getSettings(project).getCustomSettings(PyCodeStyleSettings.class).BLANKS_LINES_AT_FILE_END; + if (numLineFeeds <= 0 && EditorSettingsExternalizable.getInstance().isEnsureNewLineAtEOF()) { + numLineFeeds = 1; } documentManager.doPostponedOperationsAndUnblockDocument(document); - final String text = StringUtil.repeat("\n", numLineFeedsAtEnd); - if (numLineFeedsAtEnd > 0) { + final String text = StringUtil.repeat("\n", numLineFeeds); + if (numLineFeeds > 0) { if (!whitespaceRange.isEmpty()) { document.replaceString(whitespaceRange.getStartOffset(), whitespaceRange.getEndOffset(), text); }