PY-33060 custom wrapping modes for parameters

close #1490

GitOrigin-RevId: df01bff589eb0872e5132300fb78e8a86d21e45c
This commit is contained in:
ruro
2020-12-29 21:07:15 +03:00
committed by intellij-monorepo-bot
parent d6c25c7116
commit 196ff9cd20
11 changed files with 74 additions and 2 deletions

View File

@@ -92,6 +92,7 @@ public class PyBlock implements ASTBlock {
private Alignment myDictAlignment = null;
private Wrap myDictWrapping = null;
private Wrap myFromImportWrapping = null;
private Wrap myParameterListWrapping = null;
public PyBlock(@Nullable PyBlock parent,
@NotNull ASTNode node,
@@ -107,6 +108,7 @@ public class PyBlock implements ASTBlock {
myContext = context;
myEmptySequence = isEmptySequence(node);
final CommonCodeStyleSettings settings = myContext.getSettings();
final PyCodeStyleSettings pySettings = myContext.getPySettings();
if (node.getElementType() == PyElementTypes.DICT_LITERAL_EXPRESSION) {
myDictAlignment = Alignment.createAlignment(true);
@@ -115,6 +117,9 @@ public class PyBlock implements ASTBlock {
else if (node.getElementType() == PyElementTypes.FROM_IMPORT_STATEMENT) {
myFromImportWrapping = Wrap.createWrap(pySettings.FROM_IMPORT_WRAPPING, false);
}
else if (node.getElementType() == PyElementTypes.PARAMETER_LIST) {
myParameterListWrapping = Wrap.createWrap(settings.METHOD_PARAMETERS_WRAP, settings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE);
}
}
@Override
@@ -398,6 +403,12 @@ public class PyBlock implements ASTBlock {
if (childType == PyElementTypes.KEY_VALUE_EXPRESSION && isChildOfDictLiteral(child)) {
childWrap = myDictWrapping;
}
if (parentType == PyElementTypes.PARAMETER_LIST &&
childType != PyTokenTypes.COMMA &&
childType != PyTokenTypes.LPAR &&
childType != PyTokenTypes.RPAR) {
childWrap = myParameterListWrapping;
}
if (isAfterStatementList(child) &&
!hasLineBreaksBeforeInSameParent(child, 2) &&

View File

@@ -98,6 +98,7 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin
"KEEP_LINE_BREAKS",
"WRAP_LONG_LINES",
"ALIGN_MULTILINE_PARAMETERS",
"METHOD_PARAMETERS_WRAP",
"METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE",
"METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE",
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS");
@@ -157,6 +158,7 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin
// e.g. in SpacingBuilder#blankLines(), and can lead to unexpected side-effects in formatter's
// behavior
commonSettings.KEEP_BLANK_LINES_IN_CODE = 1;
commonSettings.METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
}
@Nullable
@@ -206,8 +208,7 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin
"\n" +
"long_expression = component_one + component_two + component_three + component_four + component_five + component_six\n" +
"\n" +
"def xyzzy(long_parameter_1,\n" +
"long_parameter_2):\n" +
"def xyzzy(a1, a2, long_parameter_1, a3, a4, long_parameter_2):\n" +
" pass\n" +
"\n" +
"xyzzy('long_string_constant1',\n" +

View File

@@ -0,0 +1,2 @@
def foo_decl(long_param1: Dict[str, Any], long_param2: List[str], long_param3: List[float], long_kwarg1: str = "with value", long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,4 @@
def foo_decl(long_param1: Dict[str, Any], long_param2: List[str],
long_param3: List[float], long_kwarg1: str = "with value",
long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,2 @@
def foo_decl(long_param1: Dict[str, Any], long_param2: List[str], long_param3: List[float], long_kwarg1: str = "with value", long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,6 @@
def foo_decl(
long_param1: Dict[str, Any], long_param2: List[str],
long_param3: List[float], long_kwarg1: str = "with value",
long_kwarg2: int = 42
) -> bool:
pass

View File

@@ -0,0 +1,2 @@
def foo_decl(long_param1: Dict[str, Any], long_param2: List[str], long_param3: List[float], long_kwarg1: str = "with value", long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,6 @@
def foo_decl(long_param1: Dict[str, Any],
long_param2: List[str],
long_param3: List[float],
long_kwarg1: str = "with value",
long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,2 @@
def foo_decl(long_param1: Dict[str, Any], long_param2: List[str], long_param3: List[float], long_kwarg1: str = "with value", long_kwarg2: int = 42) -> bool:
pass

View File

@@ -0,0 +1,8 @@
def foo_decl(
long_param1: Dict[str, Any],
long_param2: List[str],
long_param3: List[float],
long_kwarg1: str = "with value",
long_kwarg2: int = 42
) -> bool:
pass

View File

@@ -153,6 +153,34 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
public void testDefaultWrappingForMethodParameters() { // PY-33060
getCodeStyleSettings().setRightMargin(PythonLanguage.getInstance(), 80);
doTest();
}
public void testDefaultWrappingWithNewLineParensForMethodParameters() { // PY-33060
getCodeStyleSettings().setRightMargin(PythonLanguage.getInstance(), 80);
getCommonCodeStyleSettings().ALIGN_MULTILINE_PARAMETERS = false;
getCommonCodeStyleSettings().METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
getCommonCodeStyleSettings().METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
doTest();
}
public void testWrappingChopDownIfLongForMethodParameters() { // PY-33060
getCodeStyleSettings().setRightMargin(PythonLanguage.getInstance(), 80);
getCommonCodeStyleSettings().METHOD_PARAMETERS_WRAP = WrapType.CHOP_DOWN_IF_LONG.getLegacyRepresentation();
doTest();
}
public void testWrappingChopDownIfLongWithNewLineParensForMethodParameters() { // PY-33060
getCodeStyleSettings().setRightMargin(PythonLanguage.getInstance(), 80);
getCommonCodeStyleSettings().METHOD_PARAMETERS_WRAP = WrapType.CHOP_DOWN_IF_LONG.getLegacyRepresentation();
getCommonCodeStyleSettings().ALIGN_MULTILINE_PARAMETERS = false;
getCommonCodeStyleSettings().METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
getCommonCodeStyleSettings().METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
doTest();
}
public void testNoAlignForMethodArguments() { // PY-3995
getCommonCodeStyleSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false;
doTest();