IDEA-59536 New variation of "chop down if long" for annotations

1. Method annotation wrap is preferred to parameters wrap;
2. Corresponding test is added;
This commit is contained in:
Denis Zhdanov
2011-01-31 12:42:21 +03:00
parent a2c183e59f
commit 034bcc56c9
2 changed files with 46 additions and 1 deletions
@@ -474,7 +474,16 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
}
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.PARAMETER_LIST) {
final Wrap wrap = Wrap.createWrap(getWrapType(mySettings.METHOD_PARAMETERS_WRAP), false);
final Wrap wrap;
Wrap reservedWrap = getReservedWrap(JavaElementType.MODIFIER_LIST);
// There is a possible case that particular annotated method definition is too long. We may wrap either after annotation
// or after opening lbrace then. Our strategy is to wrap after annotation whenever possible.
if (reservedWrap == null) {
wrap = Wrap.createWrap(getWrapType(mySettings.METHOD_PARAMETERS_WRAP), false);
}
else {
wrap = Wrap.createChildWrap(reservedWrap, getWrapType(mySettings.METHOD_PARAMETERS_WRAP), false);
}
child = processParenthesisBlock(result, child,
WrappingStrategy.createDoNotWrapCommaStrategy(wrap),
mySettings.ALIGN_MULTILINE_PARAMETERS);
@@ -533,6 +542,19 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
myAnnotationWrap = null;
}
}
else if (childType == JavaElementType.PARAMETER_LIST && nodeType == JavaElementType.METHOD) {
// We prefer wrapping after method annotation to wrapping method parameter list, hence, deliver target wrap object
// to child block if necessary.
if (!result.isEmpty()) {
Block firstChildBlock = result.get(0);
if (firstChildBlock instanceof AbstractJavaBlock) {
AbstractJavaBlock childJavaBlock = (AbstractJavaBlock)firstChildBlock;
if (firstChildIsAnnotation(childJavaBlock.getNode())) {
javaBlock.setReservedWrap(childJavaBlock.getReservedWrap(JavaElementType.MODIFIER_LIST), JavaElementType.MODIFIER_LIST);
}
}
}
}
}
result.add(block);
@@ -747,6 +769,14 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
}
private static boolean firstChildIsAnnotation(final ASTNode child) {
ASTNode current = child.getFirstChildNode();
while (current != null && current.getElementType() == TokenType.WHITE_SPACE) {
current = current.getTreeNext();
}
return current != null && current.getElementType() == JavaElementType.ANNOTATION;
}
private static boolean lastChildIsAnnotation(final ASTNode child) {
ASTNode current = child.getLastChildNode();
while (current != null && current.getElementType() == TokenType.WHITE_SPACE) {
@@ -17,6 +17,7 @@ package com.intellij.psi.formatter.java;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
/**
* Is intended to hold specific java formatting tests for 'wrapping' settings.
@@ -224,4 +225,18 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
"}"
);
}
public void testWrapMethodAnnotationBeforeParams() throws Exception {
// Inspired by IDEA-59536
getSettings().RIGHT_MARGIN = 90;
getSettings().METHOD_ANNOTATION_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
getSettings().METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
doClassTest(
"@SuppressWarnings({\"SomeInspectionIWantToIgnore\"}) public void doSomething(int x, int y) {}",
"@SuppressWarnings({\"SomeInspectionIWantToIgnore\"})\n" +
"public void doSomething(int x, int y) {" +
"\n}"
);
}
}