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

fix: improve throws formatting #591

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
concat,
group,
ifBreak,
indent,
join,
indentIfBreak
Expand Down Expand Up @@ -515,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {

methodDeclaration(ctx: MethodDeclarationCtx) {
const modifiers = sortModifiers(ctx.methodModifier);
const throwsGroupId = Symbol("throws");
const firstAnnotations = this.mapVisit(modifiers[0]);
const otherModifiers = this.mapVisit(modifiers[1]);

const header = this.visit(ctx.methodHeader);
const header = this.visit(ctx.methodHeader, { throwsGroupId });
const body = this.visit(ctx.methodBody);

const headerBodySeparator = isStatementEmptyStatement(body) ? "" : " ";
const headerBodySeparator = isStatementEmptyStatement(body)
? ""
: ctx.methodHeader[0].children.throws
? ifBreak(hardline, " ", { groupId: throwsGroupId })
: " ";

return rejectAndJoin(hardline, [
rejectAndJoin(hardline, firstAnnotations),
...firstAnnotations,
rejectAndJoin(" ", [
rejectAndJoin(" ", otherModifiers),
...otherModifiers,
rejectAndJoin(headerBodySeparator, [header, body])
])
]);
Expand All @@ -540,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
return printTokenWithComments(this.getSingle(ctx) as IToken);
}

methodHeader(ctx: MethodHeaderCtx) {
methodHeader(ctx: MethodHeaderCtx, opts: { throwsGroupId: symbol }) {
const typeParameters = this.visit(ctx.typeParameters);
const annotations = this.mapVisit(ctx.annotation);
const result = this.visit(ctx.result);
const declarator = this.visit(ctx.methodDeclarator);
const throws = this.visit(ctx.throws);
const throws = this.visit(ctx.throws, opts);

return group(
concat([
Expand Down Expand Up @@ -652,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
return printTokenWithComments(this.getSingle(ctx) as IToken);
}

throws(ctx: ThrowsCtx) {
throws(ctx: ThrowsCtx, opts: { throwsGroupId: symbol }) {
const exceptionTypeList = this.visit(ctx.exceptionTypeList);
const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
return group(indent(rejectAndConcat([softline, throwsDeclaration])));
return group(indent(join(line, [ctx.Throws[0], exceptionTypeList])), {
id: opts.throwsGroupId
});
}

exceptionTypeList(ctx: ExceptionTypeListCtx) {
const exceptionTypes = this.mapVisit(ctx.exceptionType);
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
const commas = ctx.Comma?.map(comma => concat([comma, line]));
return rejectAndJoinSeps(commas, exceptionTypes);
}

Expand All @@ -687,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
}

constructorDeclaration(ctx: ConstructorDeclarationCtx) {
const throwsGroupId = Symbol("throws");
const modifiers = sortModifiers(ctx.constructorModifier);
const firstAnnotations = this.mapVisit(modifiers[0]);
const otherModifiers = this.mapVisit(modifiers[1]);

const constructorDeclarator = this.visit(ctx.constructorDeclarator);
const throws = this.visit(ctx.throws);
const throws = this.visit(ctx.throws, { throwsGroupId });
const constructorBody = this.visit(ctx.constructorBody);

return rejectAndJoin(" ", [
return concat([
group(
rejectAndJoin(hardline, [
rejectAndJoin(hardline, firstAnnotations),
rejectAndJoin(" ", [
join(" ", otherModifiers),
rejectAndJoin(" ", otherModifiers),
constructorDeclarator,
throws
])
])
),
ctx.throws ? ifBreak(hardline, " ", { groupId: throwsGroupId }) : " ",
constructorBody
]);
}
Expand Down
11 changes: 8 additions & 3 deletions packages/prettier-plugin-java/src/printers/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concat, group, indent } from "./prettier-builder.js";
import { concat, group, ifBreak, indent } from "./prettier-builder.js";
import { printTokenWithComments } from "./comments/format-comments.js";
import {
displaySemicolon,
Expand Down Expand Up @@ -175,12 +175,17 @@ export class InterfacesPrettierVisitor extends BaseCstPrettierPrinter {

interfaceMethodDeclaration(ctx: InterfaceMethodDeclarationCtx) {
const modifiers = sortModifiers(ctx.interfaceMethodModifier);
const throwsGroupId = Symbol("throws");
const firstAnnotations = this.mapVisit(modifiers[0]);
const otherModifiers = this.mapVisit(modifiers[1]);

const methodHeader = this.visit(ctx.methodHeader);
const methodHeader = this.visit(ctx.methodHeader, { throwsGroupId });
const methodBody = this.visit(ctx.methodBody);
const separator = isStatementEmptyStatement(methodBody) ? "" : " ";
const separator = isStatementEmptyStatement(methodBody)
? ""
: ctx.methodHeader[0].children.throws
? ifBreak(hardline, " ", { groupId: throwsGroupId })
: " ";

return rejectAndJoin(hardline, [
rejectAndJoin(hardline, firstAnnotations),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export function dedent(doc: Doc | IToken) {

export function ifBreak(
breakContents: Doc | IToken,
flatContents: Doc | IToken
flatContents: Doc | IToken,
options?: { groupId?: symbol | undefined }
) {
return builders.ifBreak(
processComments(breakContents),
processComments(flatContents)
processComments(flatContents),
options
);
}

Expand Down
52 changes: 52 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/throws/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,55 @@ public Throws(String string1, String string2, String string3, String string4, St
System.out.println("Constructor with throws that should wrap");
}
}

interface Example {
void example1(String arg1, String arg2)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;

void example2(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
) throws RuntimeException, RuntimeException, RuntimeException;

void example3(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;

default void example1(String arg1, String arg2)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}

default void example2(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
) throws RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}

default void example3(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}
}
Loading
Loading