Skip to content

Commit

Permalink
break long assignments after equals
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel authored and clementdessoude committed Mar 4, 2023
1 parent 3bb0653 commit 3358ada
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 33 deletions.
44 changes: 30 additions & 14 deletions packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
sortClassTypeChildren,
sortModifiers
} from "./printer-utils";
import { concat, group, indent, join } from "./prettier-builder";
import { concat, group, indent, join, indentIfBreak } from "./prettier-builder";
import { printTokenWithComments } from "./comments/format-comments";
import {
hasLeadingComments,
Expand Down Expand Up @@ -93,7 +93,7 @@ import { Doc } from "prettier";
import { isAnnotationCstNode, isTypeArgumentsCstNode } from "../types/utils";
import { printArgumentListWithBraces } from "../utils";

const { line, softline, hardline } = builders;
const { line, softline, hardline, lineSuffixBoundary } = builders;

export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
classDeclaration(ctx: ClassDeclarationCtx) {
Expand Down Expand Up @@ -330,10 +330,14 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
ctx.variableInitializer![0].children.expression![0].children
.ternaryExpression[0].children.QuestionMark !== undefined)
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
const groupId = Symbol("assignment");
return group([
group(variableDeclaratorId),
" ",
ctx.Equals[0],
variableInitializer
group(indent(line), { id: groupId }),
lineSuffixBoundary,
indentIfBreak(variableInitializer, { groupId })
]);
}

Expand All @@ -351,10 +355,14 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
firstPrimary.children.primaryPrefix[0].children.castExpression !==
undefined
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
const groupId = Symbol("assignment");
return group([
group(variableDeclaratorId),
" ",
ctx.Equals[0],
variableInitializer
group(indent(line), { id: groupId }),
lineSuffixBoundary,
indentIfBreak(variableInitializer, { groupId })
]);
}

Expand All @@ -363,10 +371,14 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
firstPrimary.children.primaryPrefix[0].children.newExpression !==
undefined
) {
return rejectAndJoin(" ", [
variableDeclaratorId,
const groupId = Symbol("assignment");
return group([
group(variableDeclaratorId),
" ",
ctx.Equals[0],
variableInitializer
group(indent(line), { id: groupId }),
lineSuffixBoundary,
indentIfBreak(variableInitializer, { groupId })
]);
}

Expand All @@ -383,10 +395,14 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
const isUniqueMethodInvocation =
isMethodInvocation && isUniqueUnaryExpression;
if (isUniqueMethodInvocation) {
return rejectAndJoin(" ", [
variableDeclaratorId,
const groupId = Symbol("assignment");
return group([
group(variableDeclaratorId),
" ",
ctx.Equals[0],
variableInitializer
group(indent(line), { id: groupId }),
lineSuffixBoundary,
indentIfBreak(variableInitializer, { groupId })
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export function ifBreak(
);
}

export function indentIfBreak(contents: Doc | IToken, opts?: any) {
return prettier.indentIfBreak(processComments(contents), opts);
}

// TODO: remove this once prettier 3.0 is released
const processEmptyDocs = (doc: Fill | Concat): Doc => {
return doc.parts?.length === 0 ? "" : doc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ void should_cast_with_single_element() {
var myElem = (int) othrElement;
var myElem = (A) othrElement;
var myElem = (A) (othrElement, value) -> othrElement + value;
var myElem = (Aaeaozeaonzeoazneaozenazonelkadndpndpazdpazdpazdpazdpazeazpeaazdpazdpazpdazdpa) othrElement;
var myElem =
(Aaeaozeaonzeoazneaozenazonelkadndpndpazdpazdpazdpazdpazeazpeaazdpazdpazpdazdpa) othrElement;
}

void should_cast_with_additional_bounds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ void t() {}
*/
void t() {}

public static final List<Object> XXXXXXXXXXXXXXXXXX = Collections.unmodifiableList(
Arrays.asList( // a
// b
// c
// d
)
);

public static final List<Object> XXXXXXXXXXXXXXXXXX = Collections.unmodifiableList(
Arrays.asList( // a
// b
// c
// d
/*e*/
)
);
public static final List<Object> XXXXXXXXXXXXXXXXXX =
Collections.unmodifiableList(
Arrays.asList( // a
// b
// c
// d
)
);

public static final List<Object> XXXXXXXXXXXXXXXXXX =
Collections.unmodifiableList(
Arrays.asList( // a
// b
// c
// d
/*e*/
)
);
}
56 changes: 56 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 @@ -16,6 +16,11 @@ public class Variables {
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();
private Object variableWithComment3 /* very very very long comment */= new Object();
private Object variableWithComment4 =/* very very very long comment */ new Object();

private Object[] arrayVariable1 = new Object[3];
private Object[][] arrayVariable2 = new Object[3][3];
private Object[] arrayVariable3 = new Object[] {};
Expand Down Expand Up @@ -100,6 +105,57 @@ public void breakMultipleMethods() {
boolean willDrop = predictDropResponse.getSendResult().isIgnorableFailure() || predictDropResponsegetSendResultisFatalError;
}

public void breakAfterEquals() {
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object()
.other()
.methods();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object()
.a()
.number()
.of()
.other()
.methods()
.that()
.should()
.cause()
.a()
.wrap();

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object[10];

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object[] {
new Object(),
new Object()
};

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes = new Object[] {
new Object(),
new Object(),
new Object(),
new Object(),
new Object()
};

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = SomeClass.someStaticMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = someMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = someMethod()
.anotherMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = someBooleanVariable
? new Object()
: null;

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes = anotherVeryLongNameForIllustrativePurposes !=
null
? anotherVeryLongNameForIllustrativePurposes
: new Object();
}

public methodWithVariableInitializationWithComments() {
Map<String, String> map =
// there is a random comment on this line up here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ 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();
private Object variableWithComment3 /* very very very long comment */=
new Object();
private Object variableWithComment4 =
/* very very very long comment */new Object();

private Object[] arrayVariable1 = new Object[3];
private Object[][] arrayVariable2 = new Object[3][3];
Expand Down Expand Up @@ -162,6 +171,59 @@ public void breakMultipleMethods() {
predictDropResponsegetSendResultisFatalError;
}

public void breakAfterEquals() {
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object().other().methods();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object()
.a()
.number()
.of()
.other()
.methods()
.that()
.should()
.cause()
.a()
.wrap();

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object[10];

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object[] { new Object(), new Object() };

Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object[] {
new Object(),
new Object(),
new Object(),
new Object(),
new Object(),
};

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
SomeClass.someStaticMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
someMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
someMethod().anotherMethod();

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
someBooleanVariable ? new Object() : null;

Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherVeryLongNameForIllustrativePurposes != null
? anotherVeryLongNameForIllustrativePurposes
: new Object();
}

public methodWithVariableInitializationWithComments() {
Map<String, String> map =
// there is a random comment on this line up here
Expand Down

0 comments on commit 3358ada

Please sign in to comment.