[java, formatter] do not indent type after annotation in parameter IDEA-196021

Unfortunately, it is possible to implement either this issue(IDEA-196021) or IDEA-146315.
To support use case from IDEA-146315 new option is introduced

GitOrigin-RevId: 3e4620dd2a9330d8e566276c9d0844d5d4f0dd6f
This commit is contained in:
Roman Ivanov
2021-11-26 15:19:22 +01:00
committed by intellij-monorepo-bot
parent ed79372db5
commit 8d480d3bd9
7 changed files with 48 additions and 22 deletions

View File

@@ -218,8 +218,13 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
ApplicationBundle.message("wrapping.align.when.multiline"),
JavaBundle.message("wrapping.text.blocks") );
String groupName = ApplicationBundle.message("wrapping.fields.annotation");
consumer.showCustomOption(JavaCodeStyleSettings.class, "DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION", JavaBundle.message("checkbox.do.not.wrap.after.single.annotation"), groupName);
String fieldAnnotations = ApplicationBundle.message("wrapping.fields.annotation");
consumer.showCustomOption(JavaCodeStyleSettings.class, "DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION",
JavaBundle.message("checkbox.do.not.wrap.after.single.annotation"), fieldAnnotations);
String parameterAnnotationsWrapping = ApplicationBundle.message("wrapping.parameters.annotation");
consumer.showCustomOption(JavaCodeStyleSettings.class, "DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION_IN_PARAMETER",
JavaBundle.message("checkbox.do.not.wrap.after.single.annotation"), parameterAnnotationsWrapping);
// Record components
String recordComponentsGroup = JavaBundle.message("wrapping.record.components");

View File

@@ -101,8 +101,13 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
public boolean SPACE_BEFORE_OPENING_ANGLE_BRACKET_IN_TYPE_PARAMETER;
public boolean SPACE_AROUND_TYPE_BOUNDS_IN_TYPE_PARAMETERS = true;
// Only related to fields!
// @Foo int field;
public boolean DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION;
// @Foo int param
public boolean DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION_IN_PARAMETER = false;
@WrapConstant
public int ANNOTATION_PARAMETER_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
public boolean ALIGN_MULTILINE_ANNOTATION_PARAMETERS;

View File

@@ -302,7 +302,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
}
final ASTNode prevElement = skipCommentsAndWhitespacesBackwards(child);
if (prevElement != null && prevElement.getElementType() == JavaElementType.MODIFIER_LIST && !isMethodParameterAnnotation(prevElement)) {
if (prevElement != null && prevElement.getElementType() == JavaElementType.MODIFIER_LIST) {
return Indent.getNoneIndent();
}
@@ -334,15 +334,6 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return currNode;
}
private static boolean isMethodParameterAnnotation(@NotNull ASTNode element) {
ASTNode parent = element.getTreeParent();
if (parent != null && parent.getElementType() == JavaElementType.PARAMETER) {
ASTNode lastChild = element.getLastChildNode();
return lastChild != null && lastChild.getElementType() == JavaElementType.ANNOTATION;
}
return false;
}
@Nullable
private static Indent getChildIndent(@NotNull ASTNode parent, @NotNull CommonCodeStyleSettings.IndentOptions indentOptions) {
final IElementType parentType = parent.getElementType();

View File

@@ -300,7 +300,9 @@ public final class JavaFormatterUtil {
ASTNode last = prev.getLastChildNode();
if (last != null && last.getElementType() == JavaElementType.ANNOTATION) {
if (isTypeAnnotationOrFalseIfDumb(last) ||
javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isFieldModifierListWithSingleAnnotation(prev)) {
javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isModifierListWithSingleAnnotation(prev, JavaElementType.FIELD) ||
javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION_IN_PARAMETER && isModifierListWithSingleAnnotation(prev, JavaElementType.PARAMETER)
) {
return Wrap.createWrap(WrapType.NONE, false);
}
else {
@@ -333,7 +335,7 @@ public final class JavaFormatterUtil {
ASTNode prev = FormatterUtil.getPreviousNonWhitespaceSibling(child);
if (prev != null && prev.getElementType() == JavaElementType.ANNOTATION) {
if (javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isFieldModifierListWithSingleAnnotation(parent)) {
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);
@@ -440,9 +442,9 @@ public final class JavaFormatterUtil {
}
}
private static boolean isFieldModifierListWithSingleAnnotation(@NotNull ASTNode elem) {
private static boolean isModifierListWithSingleAnnotation(@NotNull ASTNode elem, IElementType parentElementType) {
ASTNode parent = elem.getTreeParent();
if (parent != null && parent.getElementType() == JavaElementType.FIELD) {
if (parent != null && parent.getElementType() == parentElementType) {
return isModifierListWithSingleAnnotation(elem);
}
return false;

View File

@@ -66,6 +66,7 @@
"continuation_indent_size": 8,
"do_not_indent_top_level_class_members": false,
"do_not_wrap_after_single_annotation": false,
"do_not_wrap_after_single_annotation_in_parameter": false,
"do_while_brace_force": "never",
"doc_add_blank_line_after_description": true,
"doc_add_blank_line_after_param_comments": false,

View File

@@ -45,9 +45,9 @@ public class AnnotationFormatterTest extends JavaFormatterTestCase {
" public void foo(\n" +
" @Ann1\n" +
" @Ann2\n" +
" int p1,\n" +
" int p1,\n" +
" @Ann3\n" +
" boolean p1) {\n" +
" boolean p1) {\n" +
" }\n" +
"}");
@@ -386,4 +386,22 @@ public class AnnotationFormatterTest extends JavaFormatterTestCase {
"class A {\n" +
"}\n");
}
public void testTypeAfterAnnotationInParametersNotIndented() {
doTextTest(
"class Cls {\n" +
" void foo(\n" +
" @Bar\n" +
" BarObj bar\n" +
" ) {}\n" +
"}\n",
"class Cls {\n" +
" void foo(\n" +
" @Bar\n" +
" BarObj bar\n" +
" ) {\n" +
" }\n" +
"}\n");
}
}

View File

@@ -623,18 +623,22 @@ public class JavaFormatterIndentationTest extends AbstractJavaFormatterTest {
}
public void testAnnotatedParameters() {
// it is supposed that this
getJavaSettings().DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION_IN_PARAMETER = true;
getSettings().KEEP_LINE_BREAKS = false;
getSettings().RIGHT_MARGIN = 120;
getSettings().WRAP_LONG_LINES = true;
String before = "public class Formatting {\n" +
" @RequestMapping(value = \"/\", method = GET)\n" +
" public HttpEntity<String> helloWorld(@RequestParam(\"name\") String name, @PageableDefault(page = 0, size = 10)\n" +
" Pageable pageable) {\n" +
" public HttpEntity<String> helloWorld(@RequestParam(\"name\") String name, @PageableDefault(page = 0, size = 10) Pageable pageable) {\n" +
" // I'd expect the line above to be indented by 4 spaces\n" +
" return ResponseEntity.ok(\"Hello \" + name);\n" +
" }\n" +
"}";
String after = "public class Formatting {\n" +
" @RequestMapping(value = \"/\", method = GET)\n" +
" public HttpEntity<String> helloWorld(@RequestParam(\"name\") String name, @PageableDefault(page = 0, size = 10)\n" +
" Pageable pageable) {\n" +
" public HttpEntity<String> helloWorld(@RequestParam(\"name\") String name,\n" +
" @PageableDefault(page = 0, size = 10) Pageable pageable) {\n" +
" // I'd expect the line above to be indented by 4 spaces\n" +
" return ResponseEntity.ok(\"Hello \" + name);\n" +
" }\n" +