Add wrapping options for enum fields annotations

IDEA-171773

GitOrigin-RevId: 51cf6a8154d9d278b182f165f98efa2d83d052d3
This commit is contained in:
Georgii Ustinov
2023-12-08 09:51:35 +02:00
committed by intellij-monorepo-bot
parent f1c4b0b423
commit dacbca4cb7
6 changed files with 87 additions and 4 deletions

View File

@@ -109,6 +109,10 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
@WrapConstant
public int ANNOTATION_PARAMETER_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
@WrapConstant
public int ENUM_FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
public boolean ALIGN_MULTILINE_ANNOTATION_PARAMETERS;
public boolean NEW_LINE_AFTER_LPAREN_IN_ANNOTATION = false;
public boolean RPAREN_ON_NEW_LINE_IN_ANNOTATION = false;

View File

@@ -202,6 +202,9 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
JavaBundle.message("wrapping.semicolon.after.call.chain"),
getInstance().WRAPPING_CALL_CHAIN);
consumer.showCustomOption(JavaCodeStyleSettings.class, "ENUM_FIELD_ANNOTATION_WRAP", JavaBundle.message("wrapping.annotation.enums"),
null, getInstance().WRAP_OPTIONS, CodeStyleSettingsCustomizable.WRAP_VALUES);
consumer.showCustomOption(JavaCodeStyleSettings.class,
"ANNOTATION_PARAMETER_WRAP",
JavaBundle.message("wrapping.annotation.parameters"),

View File

@@ -294,7 +294,7 @@ public final class JavaFormatterUtil {
return Wrap.createWrap(WrapType.NONE, false);
}
else {
return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent, child, settings)), true);
return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent, child, settings, javaSettings)), true);
}
}
}
@@ -309,7 +309,7 @@ public final class JavaFormatterUtil {
return null;
}
return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings)), true);
return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings, javaSettings)), true);
}
else if (childType == JavaTokenType.END_OF_LINE_COMMENT) {
return Wrap.createWrap(WrapType.NORMAL, true);
@@ -320,7 +320,7 @@ public final class JavaFormatterUtil {
if (javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isModifierListWithSingleAnnotation(parent, JavaElementType.FIELD)) {
return Wrap.createWrap(WrapType.NONE, false);
}
Wrap wrap = Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings)), true);
Wrap wrap = Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings, javaSettings)), true);
putPreferredWrapInParentBlock(reservedWrapsProvider, wrap);
return wrap;
}
@@ -450,7 +450,7 @@ public final class JavaFormatterUtil {
return false;
}
private static int getAnnotationWrapType(ASTNode parent, ASTNode child, CommonCodeStyleSettings settings) {
private static int getAnnotationWrapType(ASTNode parent, ASTNode child, CommonCodeStyleSettings settings, JavaCodeStyleSettings javaSettings) {
IElementType nodeType = parent.getElementType();
if (nodeType == JavaElementType.METHOD) {
@@ -497,6 +497,10 @@ public final class JavaFormatterUtil {
return settings.CLASS_ANNOTATION_WRAP;
}
if (nodeType == JavaElementType.ENUM_CONSTANT) {
return javaSettings.ENUM_FIELD_ANNOTATION_WRAP;
}
return CommonCodeStyleSettings.DO_NOT_WRAP;
}

View File

@@ -90,6 +90,7 @@
"doc_use_throws_not_exception_tag": true,
"else_on_new_line": false,
"enum_constants_wrap": "off",
"enum_field_annotation_wrap": "off",
"extends_keyword_wrap": "off",
"extends_list_wrap": "off",
"field_annotation_wrap": "split_into_lines",

View File

@@ -497,4 +497,74 @@ public class AnnotationFormatterTest extends JavaFormatterTestCase {
}"""
);
}
public void testAnnotationEnumFieldsDoNotWrap() {
getCustomJavaSettings().ENUM_FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
doTextTest("""
public enum MyEnum {
@Anno1 FIRST
}
""",
"""
public enum MyEnum {
@Anno1 FIRST
}
""");
}
public void testAnnotationEnumFieldsWrapAlways() {
getCustomJavaSettings().ENUM_FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
doTextTest("""
public enum MyEnum {
@Anno1 FIRST
}
""",
"""
public enum MyEnum {
@Anno1
FIRST
}
""");
}
public void testAnnotationsEnumFieldsWrapAsNeeded() {
getCustomJavaSettings().ENUM_FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
getSettings().RIGHT_MARGIN = 20;
doTextTest("""
public enum MyEnum {
@Anno1 @Anno2 @Anno3 @Anno4 FIRST
}
""",
"""
public enum MyEnum {
@Anno1 @Anno2
@Anno3 @Anno4
FIRST
}
""");
}
public void testAnnotationsEnumFieldsWrapAlwaysWithDoNotWrapOnFields() {
getCustomJavaSettings().ENUM_FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
getSettings().ENUM_CONSTANTS_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
doTextTest("""
public enum MyEnum {
@Anno1 @Anno2
@Anno3 @Anno4
FIRST, SECOND, @Anno1 @Anno2 THIRD
}
""",
"""
public enum MyEnum {
@Anno1
@Anno2
@Anno3
@Anno4
FIRST, SECOND,
@Anno1
@Anno2
THIRD
}
""");
}
}

View File

@@ -1322,6 +1322,7 @@ unwrap.switch.statement=Unwrap 'switch' statement
usage.target.exception=Exception
usage.target.package.in.directory={0} (in {1})
use.external.annotations=Use &external annotations
wrapping.annotation.enums=Enum field annotations
wrapping.annotation.parameters=Annotation parameters
wrapping.record.components=Record components
wrapping.multi.catch.types=Types in multi-catch