Skip to content

Commit 8ec1626

Browse files
committed
fix: improve throws formatting
1 parent 95b1b36 commit 8ec1626

File tree

5 files changed

+216
-47
lines changed

5 files changed

+216
-47
lines changed

packages/prettier-plugin-java/src/printers/classes.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import {
1616
concat,
1717
group,
18+
ifBreak,
1819
indent,
1920
join,
2021
indentIfBreak
@@ -515,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
515516

516517
methodDeclaration(ctx: MethodDeclarationCtx) {
517518
const modifiers = sortModifiers(ctx.methodModifier);
519+
const throwsGroupId = Symbol("throws");
518520
const firstAnnotations = this.mapVisit(modifiers[0]);
519521
const otherModifiers = this.mapVisit(modifiers[1]);
520522

521-
const header = this.visit(ctx.methodHeader);
523+
const header = this.visit(ctx.methodHeader, { throwsGroupId });
522524
const body = this.visit(ctx.methodBody);
523525

524-
const headerBodySeparator = isStatementEmptyStatement(body) ? "" : " ";
526+
const headerBodySeparator = isStatementEmptyStatement(body)
527+
? ""
528+
: ctx.methodHeader[0].children.throws
529+
? ifBreak(hardline, " ", { groupId: throwsGroupId })
530+
: " ";
525531

526532
return rejectAndJoin(hardline, [
527-
rejectAndJoin(hardline, firstAnnotations),
533+
...firstAnnotations,
528534
rejectAndJoin(" ", [
529-
rejectAndJoin(" ", otherModifiers),
535+
...otherModifiers,
530536
rejectAndJoin(headerBodySeparator, [header, body])
531537
])
532538
]);
@@ -540,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
540546
return printTokenWithComments(this.getSingle(ctx) as IToken);
541547
}
542548

543-
methodHeader(ctx: MethodHeaderCtx) {
549+
methodHeader(ctx: MethodHeaderCtx, opts: { throwsGroupId: symbol }) {
544550
const typeParameters = this.visit(ctx.typeParameters);
545551
const annotations = this.mapVisit(ctx.annotation);
546552
const result = this.visit(ctx.result);
547553
const declarator = this.visit(ctx.methodDeclarator);
548-
const throws = this.visit(ctx.throws);
554+
const throws = this.visit(ctx.throws, opts);
549555

550556
return group(
551557
concat([
@@ -652,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
652658
return printTokenWithComments(this.getSingle(ctx) as IToken);
653659
}
654660

655-
throws(ctx: ThrowsCtx) {
661+
throws(ctx: ThrowsCtx, opts: { throwsGroupId: symbol }) {
656662
const exceptionTypeList = this.visit(ctx.exceptionTypeList);
657-
const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
658-
return group(indent(rejectAndConcat([softline, throwsDeclaration])));
663+
return group(indent(join(line, [ctx.Throws[0], exceptionTypeList])), {
664+
id: opts.throwsGroupId
665+
});
659666
}
660667

661668
exceptionTypeList(ctx: ExceptionTypeListCtx) {
662669
const exceptionTypes = this.mapVisit(ctx.exceptionType);
663-
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
670+
const commas = ctx.Comma?.map(comma => concat([comma, line]));
664671
return rejectAndJoinSeps(commas, exceptionTypes);
665672
}
666673

@@ -687,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
687694
}
688695

689696
constructorDeclaration(ctx: ConstructorDeclarationCtx) {
697+
const throwsGroupId = Symbol("throws");
690698
const modifiers = sortModifiers(ctx.constructorModifier);
691699
const firstAnnotations = this.mapVisit(modifiers[0]);
692700
const otherModifiers = this.mapVisit(modifiers[1]);
693-
694701
const constructorDeclarator = this.visit(ctx.constructorDeclarator);
695-
const throws = this.visit(ctx.throws);
702+
const throws = this.visit(ctx.throws, { throwsGroupId });
696703
const constructorBody = this.visit(ctx.constructorBody);
697704

698-
return rejectAndJoin(" ", [
705+
return concat([
699706
group(
700707
rejectAndJoin(hardline, [
701708
rejectAndJoin(hardline, firstAnnotations),
702709
rejectAndJoin(" ", [
703-
join(" ", otherModifiers),
710+
rejectAndJoin(" ", otherModifiers),
704711
constructorDeclarator,
705712
throws
706713
])
707714
])
708715
),
716+
ctx.throws ? ifBreak(hardline, " ", { groupId: throwsGroupId }) : " ",
709717
constructorBody
710718
]);
711719
}

packages/prettier-plugin-java/src/printers/interfaces.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { concat, group, indent } from "./prettier-builder.js";
1+
import { concat, group, ifBreak, indent } from "./prettier-builder.js";
22
import { printTokenWithComments } from "./comments/format-comments.js";
33
import {
44
displaySemicolon,
@@ -175,12 +175,17 @@ export class InterfacesPrettierVisitor extends BaseCstPrettierPrinter {
175175

176176
interfaceMethodDeclaration(ctx: InterfaceMethodDeclarationCtx) {
177177
const modifiers = sortModifiers(ctx.interfaceMethodModifier);
178+
const throwsGroupId = Symbol("throws");
178179
const firstAnnotations = this.mapVisit(modifiers[0]);
179180
const otherModifiers = this.mapVisit(modifiers[1]);
180181

181-
const methodHeader = this.visit(ctx.methodHeader);
182+
const methodHeader = this.visit(ctx.methodHeader, { throwsGroupId });
182183
const methodBody = this.visit(ctx.methodBody);
183-
const separator = isStatementEmptyStatement(methodBody) ? "" : " ";
184+
const separator = isStatementEmptyStatement(methodBody)
185+
? ""
186+
: ctx.methodHeader[0].children.throws
187+
? ifBreak(hardline, " ", { groupId: throwsGroupId })
188+
: " ";
184189

185190
return rejectAndJoin(hardline, [
186191
rejectAndJoin(hardline, firstAnnotations),

packages/prettier-plugin-java/src/printers/prettier-builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ export function dedent(doc: Doc | IToken) {
5353

5454
export function ifBreak(
5555
breakContents: Doc | IToken,
56-
flatContents: Doc | IToken
56+
flatContents: Doc | IToken,
57+
options?: { groupId?: symbol | undefined }
5758
) {
5859
return builders.ifBreak(
5960
processComments(breakContents),
60-
processComments(flatContents)
61+
processComments(flatContents),
62+
options
6163
);
6264
}
6365

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,55 @@ public Throws(String string1, String string2, String string3, String string4, St
8787
System.out.println("Constructor with throws that should wrap");
8888
}
8989
}
90+
91+
interface Example {
92+
void example1(String arg1, String arg2)
93+
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;
94+
95+
void example2(
96+
String arg1,
97+
String arg2,
98+
String arg3,
99+
String arg4,
100+
String arg5,
101+
String arg6
102+
) throws RuntimeException, RuntimeException, RuntimeException;
103+
104+
void example3(
105+
String arg1,
106+
String arg2,
107+
String arg3,
108+
String arg4,
109+
String arg5,
110+
String arg6
111+
)
112+
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;
113+
114+
default void example1(String arg1, String arg2)
115+
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
116+
throw new RuntimeException();
117+
}
118+
119+
default void example2(
120+
String arg1,
121+
String arg2,
122+
String arg3,
123+
String arg4,
124+
String arg5,
125+
String arg6
126+
) throws RuntimeException, RuntimeException, RuntimeException {
127+
throw new RuntimeException();
128+
}
129+
130+
default void example3(
131+
String arg1,
132+
String arg2,
133+
String arg3,
134+
String arg4,
135+
String arg5,
136+
String arg6
137+
)
138+
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
139+
throw new RuntimeException();
140+
}
141+
}

0 commit comments

Comments
 (0)