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.
This commit is contained in:
Mikhail Golubev
2017-01-23 17:41:20 +03:00
parent 466af87373
commit c0880210ab
8 changed files with 13 additions and 51 deletions
@@ -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
@@ -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;
/**
@@ -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);
}
}
@@ -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"));
@@ -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,
@@ -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, )
@@ -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, )
@@ -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();
}