Do not wrap nested method call parameters, if wrap on parent method call parameters produces result not exceeding right margin

This commit is contained in:
Yaroslav Lepenkin
2015-04-21 18:27:16 +03:00
parent fea3ba33c3
commit 4dc4e97318
2 changed files with 31 additions and 6 deletions
@@ -463,8 +463,8 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
}
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.EXPRESSION_LIST) {
final Wrap wrap = Wrap.createWrap(getWrapType(mySettings.CALL_PARAMETERS_WRAP), false);
if (mySettings.PREFER_PARAMETERS_WRAP) {
wrap.ignoreParentWraps();
if (mySettings.PREFER_PARAMETERS_WRAP && !isInsideMethodCall(myNode.getPsi())) {
wrap.ignoreParentWraps();
}
child = processParenthesisBlock(result, child,
WrappingStrategy.createDoNotWrapCommaStrategy(wrap),
@@ -543,6 +543,19 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return child;
}
private boolean isInsideMethodCall(@NotNull PsiElement element) {
PsiElement e = element.getParent();
int parentsVisited = 0;
while (e != null && !(e instanceof PsiStatement) && parentsVisited < 5) {
if (e instanceof PsiExpressionList) {
return true;
}
e = e.getParent();
parentsVisited++;
}
return false;
}
@NotNull
private Wrap getMethodParametersWrap() {
Wrap preferredWrap = getModifierListWrap();
@@ -381,14 +381,17 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
getSettings().CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
getSettings().CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
doMethodTest(before, after);
doMethodTest(before,
"processingEnv.getMessenger().printMessage(\n" +
" Diagnostic.Kind.ERROR,\n" +
" String.format(\"Could not process annotations: %s%n%s\", e.toString(), writer.toString())\n" +
");");
String literal = "\"" + StringUtil.repeatSymbol('A', 128) + "\"";
before = "processingEnv.getMessenger().printMessage(Diagnostic.Kind.ERROR, call(" + literal + "));\n";
after = "processingEnv.getMessenger().printMessage(\n" +
" Diagnostic.Kind.ERROR, call(\n" +
" " + literal + "\n" +
" )\n" +
" Diagnostic.Kind.ERROR,\n" +
" call(" + literal + ")\n" +
");\n";
doMethodTest(before, after);
@@ -498,4 +501,13 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
"}"
);
}
public void test_Do_Not_Wrap_On_Nested_Call_Arguments_If_Not_Needed() {
getSettings().PREFER_PARAMETERS_WRAP = true;
getSettings().CALL_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
doMethodTest("call(aaaaaaaaaaabbbbbbbbbbbbsdfsdfsdfsdfsdfsdfsdfb, 1 + call(111111111, 32213123123, 123123123123, 234234234234324234234));",
"call(aaaaaaaaaaabbbbbbbbbbbbsdfsdfsdfsdfsdfsdfsdfb,\n" +
" 1 + call(111111111, 32213123123, 123123123123, 234234234234324234234));");
}
}