Skip to content

Commit

Permalink
fix: improve throws formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Apr 10, 2024
1 parent 95b1b36 commit 8ec1626
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 47 deletions.
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

0 comments on commit 8ec1626

Please sign in to comment.