Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: break long lines on type arguments #584

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
])
)
)
]);
}

Expand All @@ -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);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/variables/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ 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<String, String> map =
// there is a random comment on this line up here
Expand Down
101 changes: 97 additions & 4 deletions packages/prettier-plugin-java/test/unit-test/variables/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ public class Variables {
"ghi",
"jkl"
);
private Map<Integer, String> genericVariable4 =
new HashMap<Integer, String>();
private Map<Integer, String, Integer, String> genericVariable5 =
new HashMap<Integer, String, Integer>();
private Map<Integer, String> genericVariable4 = new HashMap<
Integer,
String
>();
private Map<Integer, String, Integer, String> genericVariable5 = new HashMap<
Integer,
String,
Integer
>();

private Object variableWithComment1 /* comment */= new Object();
private Object variableWithComment2 = /* comment */new Object();
Expand Down Expand Up @@ -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<String, String> map =
// there is a random comment on this line up here
Expand Down