diff --git a/packages/prettier-plugin-java/src/printers/types-values-and-variables.ts b/packages/prettier-plugin-java/src/printers/types-values-and-variables.ts index 9318115b..32b80ef2 100644 --- a/packages/prettier-plugin-java/src/printers/types-values-and-variables.ts +++ b/packages/prettier-plugin-java/src/printers/types-values-and-variables.ts @@ -1,10 +1,12 @@ "use strict"; import forEach from "lodash/forEach"; +import { builders } from "prettier/doc"; -import { concat, join } from "./prettier-builder"; +import { concat, group, indent, join } from "./prettier-builder"; import { printTokenWithComments } from "./comments/format-comments"; import { + putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, @@ -41,6 +43,8 @@ import { isTypeArgumentsCstNode } from "../types/utils"; +const { line, softline } = builders; + export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrinter { primitiveType(ctx: PrimitiveTypeCtx) { const annotations = this.mapVisit(ctx.annotation); @@ -180,10 +184,16 @@ export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrint const classOrInterfaceType = this.visit(ctx.classOrInterfaceType); const additionalBound = this.mapVisit(ctx.additionalBound); - return rejectAndJoin(" ", [ - ctx.Extends[0], - classOrInterfaceType, - join(" ", additionalBound) + return concat([ + rejectAndJoin(" ", [ctx.Extends[0], classOrInterfaceType]), + indent( + group( + concat([ + additionalBound.length ? line : "", + rejectAndJoin(line, additionalBound) + ]) + ) + ) ]); } @@ -196,12 +206,17 @@ export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrint typeArguments(ctx: TypeArgumentsCtx) { const typeArgumentList = this.visit(ctx.typeArgumentList); - return rejectAndConcat([ctx.Less[0], typeArgumentList, ctx.Greater[0]]); + return putIntoBraces( + typeArgumentList, + softline, + ctx.Less[0], + ctx.Greater[0] + ); } typeArgumentList(ctx: TypeArgumentListCtx) { const typeArguments = this.mapVisit(ctx.typeArgument); - const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : []; + const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : []; return rejectAndJoinSeps(commas, typeArguments); } diff --git a/packages/prettier-plugin-java/test/unit-test/variables/_input.java b/packages/prettier-plugin-java/test/unit-test/variables/_input.java index 77d5563e..d5fe5636 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_input.java @@ -156,6 +156,27 @@ public void breakAfterEquals() { : new Object(); } + public , ExtremelyLongAndObnoxiousClassName> & ExtremelyLongAndObnoxiousInterfaceName & ExtremelyLongAndObnoxiousInterfaceName & ExtremelyLongAndObnoxiousInterfaceName> void breakOnTypeArguments( + ExtremelyLongAndObnoxiousClassName parameter, + ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName> parameter + ) { + ExtremelyLongAndObnoxiousClassName variable; + + ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName> variable; + + ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName> variable = + new MyExtremelyLongAndObnoxiousClassName<>(); + + ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName> variable = + new MyExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>(); + + ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName> aParticularlyLongAndObnoxiousNameForIllustrativePurposes = + new MyExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>(); + + new MyExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>() + .method(); + } + public methodWithVariableInitializationWithComments() { Map map = // there is a random comment on this line up here diff --git a/packages/prettier-plugin-java/test/unit-test/variables/_output.java b/packages/prettier-plugin-java/test/unit-test/variables/_output.java index a301d365..9b69cda5 100644 --- a/packages/prettier-plugin-java/test/unit-test/variables/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/variables/_output.java @@ -20,10 +20,15 @@ public class Variables { "ghi", "jkl" ); - private Map genericVariable4 = - new HashMap(); - private Map genericVariable5 = - new HashMap(); + private Map genericVariable4 = new HashMap< + Integer, + String + >(); + private Map genericVariable5 = new HashMap< + Integer, + String, + Integer + >(); private Object variableWithComment1 /* comment */= new Object(); private Object variableWithComment2 = /* comment */new Object(); @@ -224,6 +229,94 @@ public void breakAfterEquals() { : new Object(); } + public < + A extends ShortClassName & ShortClassName & ShortClassName & ShortClassName, + B extends ShortClassName + & ShortClassName + & ShortClassName + & ShortClassName + & ShortClassName, + C extends ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > + & ExtremelyLongAndObnoxiousInterfaceName + & ExtremelyLongAndObnoxiousInterfaceName + & ExtremelyLongAndObnoxiousInterfaceName + > void breakOnTypeArguments( + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName + > parameter, + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > parameter + ) { + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName + > variable; + + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > variable; + + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > variable = new MyExtremelyLongAndObnoxiousClassName<>(); + + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > variable = new MyExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + >(); + + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + > aParticularlyLongAndObnoxiousNameForIllustrativePurposes = + new MyExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + >(); + + new MyExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName< + ExtremelyLongAndObnoxiousClassName, + ExtremelyLongAndObnoxiousClassName + >, + ExtremelyLongAndObnoxiousClassName + >() + .method(); + } + public methodWithVariableInitializationWithComments() { Map map = // there is a random comment on this line up here