From c0880210abf1bf9016e8280f0f29a745ec0c6a4a Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 23 Jan 2017 16:08:14 +0300 Subject: [PATCH] PY-20633 Wrap only multiline "from" imports in parentheses Effectively, I've removed "Always" variant of "Force parentheses" option. It doesn't seem that people ever need to wrap one line "from" imports in parentheses, so there is no point in making code style settings more complicated. We can return combo box with "Always" variant later if someone specifically asks for it. --- .../src/com/jetbrains/python/PyBundle.properties | 2 +- .../python/formatter/PyCodeStyleSettings.java | 2 +- .../PyFromImportPostFormatProcessor.java | 6 ++---- .../PyLanguageCodeStyleSettingsProvider.java | 7 +++---- .../fromImportForceParenthesesAlways.py | 12 ------------ .../fromImportForceParenthesesAlways_after.py | 13 ------------- ...omImportTrailingCommaWithParentheses_after.py | 6 ++---- .../com/jetbrains/python/PyFormatterTest.java | 16 ++++------------ 8 files changed, 13 insertions(+), 51 deletions(-) delete mode 100644 python/testData/formatter/fromImportForceParenthesesAlways.py delete mode 100644 python/testData/formatter/fromImportForceParenthesesAlways_after.py diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 3820a1a69a5b..8070a9e5bd51 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -1034,7 +1034,7 @@ formatter.force.new.line.after.colon=Force new line after colon formatter.align.when.multiline=Align when multiline formatter.collections.and.comprehensions=Collections and Comprehensions formatter.from.import.statements="From" Import Statements -formatter.from.import.statements.force.parentheses=Force parentheses +formatter.from.import.statements.force.parentheses.if.multiline=Force parentheses if multiline formatter.from.import.statements.force.comma.if.multline=Force trailing comma if multiline formatter.dictionary.literals=Dictionary literals formatter.hang.closing.brackets=Hang closing brackets diff --git a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java index 1c48174e4a43..a8d81a9de18d 100644 --- a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java +++ b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java @@ -119,7 +119,7 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings { CommonCodeStyleSettings.FORCE_BRACES_IF_MULTILINE, CommonCodeStyleSettings.FORCE_BRACES_ALWAYS} ) - public int FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.DO_NOT_FORCE; + public boolean FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = false; public boolean FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = false; /** diff --git a/python/src/com/jetbrains/python/formatter/PyFromImportPostFormatProcessor.java b/python/src/com/jetbrains/python/formatter/PyFromImportPostFormatProcessor.java index ab87b880cb09..825fb2153188 100644 --- a/python/src/com/jetbrains/python/formatter/PyFromImportPostFormatProcessor.java +++ b/python/src/com/jetbrains/python/formatter/PyFromImportPostFormatProcessor.java @@ -66,16 +66,14 @@ public class PyFromImportPostFormatProcessor implements PostFormatProcessor { if (importedNames.size() > 1) { final PyCodeStyleSettings pySettings = ((CodeStyleSettings)myHelper.getSettings()).getCustomSettings(PyCodeStyleSettings.class); - final boolean forcedParentheses = pySettings.FROM_IMPORT_PARENTHESES_FORCE == CommonCodeStyleSettings.FORCE_BRACES_ALWAYS || - pySettings.FROM_IMPORT_PARENTHESES_FORCE == CommonCodeStyleSettings.FORCE_BRACES_IF_MULTILINE && - PostFormatProcessorHelper.isMultiline(node); + final boolean forcedParens = pySettings.FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE && PostFormatProcessorHelper.isMultiline(node); final boolean forcedComma = pySettings.FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE && PostFormatProcessorHelper.isMultiline(node); final PyImportElement lastImportedName = importedNames.get(importedNames.size() - 1); final PsiElement afterLastName = PyPsiUtils.getNextNonCommentSibling(lastImportedName, true); final PsiElement openingParen = node.getLeftParen(); final boolean missingComma = afterLastName == null || afterLastName.getNode().getElementType() != PyTokenTypes.COMMA; // Trailing comma is allowed only in "from" imports wrapped in parentheses - if (forcedParentheses && openingParen == null || forcedComma && missingComma && openingParen != null) { + if (forcedParens && openingParen == null || forcedComma && missingComma && openingParen != null) { myImportStatements.add(node); } } diff --git a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java index bdd2ce2a88da..4d9bf537c737 100644 --- a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java +++ b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java @@ -131,10 +131,9 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin consumer.showCustomOption(PyCodeStyleSettings.class, "FROM_IMPORT_NEW_LINE_BEFORE_RIGHT_PARENTHESIS", ApplicationBundle.message("wrapping.rpar.on.new.line"), PyBundle.message("formatter.from.import.statements")); - consumer.showCustomOption(PyCodeStyleSettings.class, "FROM_IMPORT_PARENTHESES_FORCE", - PyBundle.message("formatter.from.import.statements.force.parentheses"), - PyBundle.message("formatter.from.import.statements"), - BRACE_OPTIONS, BRACE_VALUES); + consumer.showCustomOption(PyCodeStyleSettings.class, "FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE", + PyBundle.message("formatter.from.import.statements.force.parentheses.if.multiline"), + PyBundle.message("formatter.from.import.statements")); consumer.showCustomOption(PyCodeStyleSettings.class, "FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE", PyBundle.message("formatter.from.import.statements.force.comma.if.multline"), PyBundle.message("formatter.from.import.statements")); diff --git a/python/testData/formatter/fromImportForceParenthesesAlways.py b/python/testData/formatter/fromImportForceParenthesesAlways.py deleted file mode 100644 index 4367abfb876b..000000000000 --- a/python/testData/formatter/fromImportForceParenthesesAlways.py +++ /dev/null @@ -1,12 +0,0 @@ -from module import foo -from module import foo, bar -# | margin -from module import foo, bar, baz -from module import foo, \ - bar -from module import foo, \ - bar # comment -from module import foo, \ - \ - \ - bar, \ No newline at end of file diff --git a/python/testData/formatter/fromImportForceParenthesesAlways_after.py b/python/testData/formatter/fromImportForceParenthesesAlways_after.py deleted file mode 100644 index 8f29c68abdd7..000000000000 --- a/python/testData/formatter/fromImportForceParenthesesAlways_after.py +++ /dev/null @@ -1,13 +0,0 @@ -from module import foo -from module import (foo, bar) -# | margin -from module import (foo, bar, - baz) -from module import (foo, - bar) -from module import (foo, - bar # comment - ) -from module import (foo, - - bar, ) diff --git a/python/testData/formatter/fromImportTrailingCommaWithParentheses_after.py b/python/testData/formatter/fromImportTrailingCommaWithParentheses_after.py index 1857e3d30298..0940bfab0ebb 100644 --- a/python/testData/formatter/fromImportTrailingCommaWithParentheses_after.py +++ b/python/testData/formatter/fromImportTrailingCommaWithParentheses_after.py @@ -1,8 +1,6 @@ from module import foo -from module import (foo, - bar, ) -from module import (foo, - bar, ) +from module import foo, bar +from module import foo, bar, # | margin from module import (foo, bar, baz, ) diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index dd6048fa5525..8f48d4970e9e 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -20,7 +20,6 @@ import com.intellij.openapi.command.WriteCommandAction; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.fixtures.PyTestCase; import com.jetbrains.python.formatter.PyCodeStyleSettings; @@ -765,24 +764,17 @@ public class PyFormatterTest extends PyTestCase { doTest(); } - // PY-20633 - public void testFromImportForceParenthesesAlways() { - getCodeStyleSettings().setRightMargin(PythonLanguage.INSTANCE, 30); - getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS; - doTest(); - } - // PY-20633 public void testFromImportForceParenthesesIfMultiline() { getCodeStyleSettings().setRightMargin(PythonLanguage.INSTANCE, 30); - getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.FORCE_BRACES_IF_MULTILINE; + getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true; doTest(); } // PY-20633 // See http://docs.pylonsproject.org/en/latest/community/codestyle.html public void testPyramidFromImportFormatting() { - getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS; + getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true; getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_AFTER_LEFT_PARENTHESIS = true; getPythonCodeStyleSettings().FROM_IMPORT_NEW_LINE_BEFORE_RIGHT_PARENTHESIS = true; getPythonCodeStyleSettings().FROM_IMPORT_WRAPPING = WrapType.ALWAYS.getLegacyRepresentation(); @@ -794,7 +786,7 @@ public class PyFormatterTest extends PyTestCase { // PY-9764 public void testFromImportTrailingCommaWithParentheses() { getCodeStyleSettings().setRightMargin(PythonLanguage.INSTANCE, 30); - getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS; + getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = true; getPythonCodeStyleSettings().FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = true; doTest(); } @@ -802,7 +794,7 @@ public class PyFormatterTest extends PyTestCase { // PY-9764 public void testFromImportTrailingCommaWithoutParentheses() { getCodeStyleSettings().setRightMargin(PythonLanguage.INSTANCE, 30); - getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE = CommonCodeStyleSettings.DO_NOT_FORCE; + getPythonCodeStyleSettings().FROM_IMPORT_PARENTHESES_FORCE_IF_MULTILINE = false; getPythonCodeStyleSettings().FROM_IMPORT_TRAILING_COMMA_IF_MULTILINE = true; doTest(); }