Skip to content

Commit 20da8ea

Browse files
jtkieselclementdessoude
authored andcommitted
feat: break long lines on type arguments
1 parent 8c5b8cd commit 20da8ea

3 files changed

Lines changed: 140 additions & 11 deletions

File tree

packages/prettier-plugin-java/src/printers/types-values-and-variables.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"use strict";
22

33
import forEach from "lodash/forEach";
4+
import { builders } from "prettier/doc";
45

5-
import { concat, join } from "./prettier-builder";
6+
import { concat, group, indent, join } from "./prettier-builder";
67
import { printTokenWithComments } from "./comments/format-comments";
78
import {
9+
putIntoBraces,
810
rejectAndConcat,
911
rejectAndJoin,
1012
rejectAndJoinSeps,
@@ -41,6 +43,8 @@ import {
4143
isTypeArgumentsCstNode
4244
} from "../types/utils";
4345

46+
const { line, softline } = builders;
47+
4448
export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrinter {
4549
primitiveType(ctx: PrimitiveTypeCtx) {
4650
const annotations = this.mapVisit(ctx.annotation);
@@ -180,10 +184,16 @@ export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrint
180184
const classOrInterfaceType = this.visit(ctx.classOrInterfaceType);
181185
const additionalBound = this.mapVisit(ctx.additionalBound);
182186

183-
return rejectAndJoin(" ", [
184-
ctx.Extends[0],
185-
classOrInterfaceType,
186-
join(" ", additionalBound)
187+
return concat([
188+
rejectAndJoin(" ", [ctx.Extends[0], classOrInterfaceType]),
189+
indent(
190+
group(
191+
concat([
192+
additionalBound.length ? line : "",
193+
rejectAndJoin(line, additionalBound)
194+
])
195+
)
196+
)
187197
]);
188198
}
189199

@@ -196,12 +206,17 @@ export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrint
196206
typeArguments(ctx: TypeArgumentsCtx) {
197207
const typeArgumentList = this.visit(ctx.typeArgumentList);
198208

199-
return rejectAndConcat([ctx.Less[0], typeArgumentList, ctx.Greater[0]]);
209+
return putIntoBraces(
210+
typeArgumentList,
211+
softline,
212+
ctx.Less[0],
213+
ctx.Greater[0]
214+
);
200215
}
201216

202217
typeArgumentList(ctx: TypeArgumentListCtx) {
203218
const typeArguments = this.mapVisit(ctx.typeArgument);
204-
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
219+
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
205220
return rejectAndJoinSeps(commas, typeArguments);
206221
}
207222

packages/prettier-plugin-java/test/unit-test/variables/_input.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ public void breakAfterEquals() {
156156
: new Object();
157157
}
158158

159+
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(
160+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName> parameter,
161+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> parameter
162+
) {
163+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName> variable;
164+
165+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable;
166+
167+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable =
168+
new MyExtremelyLongAndObnoxiousClassName<>();
169+
170+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable =
171+
new MyExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName>();
172+
173+
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
174+
new MyExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName>();
175+
176+
new MyExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName>()
177+
.method();
178+
}
179+
159180
public methodWithVariableInitializationWithComments() {
160181
Map<String, String> map =
161182
// there is a random comment on this line up here

packages/prettier-plugin-java/test/unit-test/variables/_output.java

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ public class Variables {
2020
"ghi",
2121
"jkl"
2222
);
23-
private Map<Integer, String> genericVariable4 =
24-
new HashMap<Integer, String>();
25-
private Map<Integer, String, Integer, String> genericVariable5 =
26-
new HashMap<Integer, String, Integer>();
23+
private Map<Integer, String> genericVariable4 = new HashMap<
24+
Integer,
25+
String
26+
>();
27+
private Map<Integer, String, Integer, String> genericVariable5 = new HashMap<
28+
Integer,
29+
String,
30+
Integer
31+
>();
2732

2833
private Object variableWithComment1 /* comment */= new Object();
2934
private Object variableWithComment2 = /* comment */new Object();
@@ -224,6 +229,94 @@ public void breakAfterEquals() {
224229
: new Object();
225230
}
226231

232+
public <
233+
A extends ShortClassName & ShortClassName & ShortClassName & ShortClassName,
234+
B extends ShortClassName
235+
& ShortClassName
236+
& ShortClassName
237+
& ShortClassName
238+
& ShortClassName,
239+
C extends ExtremelyLongAndObnoxiousClassName<
240+
ExtremelyLongAndObnoxiousClassName<
241+
ExtremelyLongAndObnoxiousClassName,
242+
ExtremelyLongAndObnoxiousClassName
243+
>,
244+
ExtremelyLongAndObnoxiousClassName
245+
>
246+
& ExtremelyLongAndObnoxiousInterfaceName
247+
& ExtremelyLongAndObnoxiousInterfaceName
248+
& ExtremelyLongAndObnoxiousInterfaceName
249+
> void breakOnTypeArguments(
250+
ExtremelyLongAndObnoxiousClassName<
251+
ExtremelyLongAndObnoxiousClassName
252+
> parameter,
253+
ExtremelyLongAndObnoxiousClassName<
254+
ExtremelyLongAndObnoxiousClassName<
255+
ExtremelyLongAndObnoxiousClassName,
256+
ExtremelyLongAndObnoxiousClassName
257+
>,
258+
ExtremelyLongAndObnoxiousClassName
259+
> parameter
260+
) {
261+
ExtremelyLongAndObnoxiousClassName<
262+
ExtremelyLongAndObnoxiousClassName
263+
> variable;
264+
265+
ExtremelyLongAndObnoxiousClassName<
266+
ExtremelyLongAndObnoxiousClassName<
267+
ExtremelyLongAndObnoxiousClassName,
268+
ExtremelyLongAndObnoxiousClassName
269+
>,
270+
ExtremelyLongAndObnoxiousClassName
271+
> variable;
272+
273+
ExtremelyLongAndObnoxiousClassName<
274+
ExtremelyLongAndObnoxiousClassName<
275+
ExtremelyLongAndObnoxiousClassName,
276+
ExtremelyLongAndObnoxiousClassName
277+
>,
278+
ExtremelyLongAndObnoxiousClassName
279+
> variable = new MyExtremelyLongAndObnoxiousClassName<>();
280+
281+
ExtremelyLongAndObnoxiousClassName<
282+
ExtremelyLongAndObnoxiousClassName<
283+
ExtremelyLongAndObnoxiousClassName,
284+
ExtremelyLongAndObnoxiousClassName
285+
>,
286+
ExtremelyLongAndObnoxiousClassName
287+
> variable = new MyExtremelyLongAndObnoxiousClassName<
288+
ExtremelyLongAndObnoxiousClassName<
289+
ExtremelyLongAndObnoxiousClassName,
290+
ExtremelyLongAndObnoxiousClassName
291+
>,
292+
ExtremelyLongAndObnoxiousClassName
293+
>();
294+
295+
ExtremelyLongAndObnoxiousClassName<
296+
ExtremelyLongAndObnoxiousClassName<
297+
ExtremelyLongAndObnoxiousClassName,
298+
ExtremelyLongAndObnoxiousClassName
299+
>,
300+
ExtremelyLongAndObnoxiousClassName
301+
> aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
302+
new MyExtremelyLongAndObnoxiousClassName<
303+
ExtremelyLongAndObnoxiousClassName<
304+
ExtremelyLongAndObnoxiousClassName,
305+
ExtremelyLongAndObnoxiousClassName
306+
>,
307+
ExtremelyLongAndObnoxiousClassName
308+
>();
309+
310+
new MyExtremelyLongAndObnoxiousClassName<
311+
ExtremelyLongAndObnoxiousClassName<
312+
ExtremelyLongAndObnoxiousClassName,
313+
ExtremelyLongAndObnoxiousClassName
314+
>,
315+
ExtremelyLongAndObnoxiousClassName
316+
>()
317+
.method();
318+
}
319+
227320
public methodWithVariableInitializationWithComments() {
228321
Map<String, String> map =
229322
// there is a random comment on this line up here

0 commit comments

Comments
 (0)