Skip to content

Commit

Permalink
feat: break long variable declarations/assignments on type arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Jul 9, 2023
1 parent f93d55a commit 2c6b86c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use strict";

import forEach from "lodash/forEach";
import { builders } from "prettier/doc";

import { concat, join } from "./prettier-builder";
import { printTokenWithComments } from "./comments/format-comments";
import {
putIntoBraces,
rejectAndConcat,
rejectAndJoin,
rejectAndJoinSeps,
Expand Down Expand Up @@ -41,6 +43,8 @@ import {
isTypeArgumentsCstNode
} from "../types/utils";

const { line, softline } = builders;

export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrinter {
primitiveType(ctx: PrimitiveTypeCtx) {
const annotations = this.mapVisit(ctx.annotation);
Expand Down Expand Up @@ -196,12 +200,17 @@ export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrint
typeArguments(ctx: TypeArgumentsCtx) {
const typeArgumentList = this.visit(ctx.typeArgumentList);

return rejectAndConcat([ctx.Less[0], typeArgumentList, ctx.Greater[0]]);
return putIntoBraces(
typeArgumentList,
softline,
ctx.Less[0],
ctx.Greater[0]
);
}

typeArgumentList(ctx: TypeArgumentListCtx) {
const typeArguments = this.mapVisit(ctx.typeArgument);
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
return rejectAndJoinSeps(commas, typeArguments);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @surely this is invalid
*/
public enum Enum {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

public enum Enum {
SOME_ENUM,
ANOTHER_ENUM,
LAST_ENUM,
}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
THIS_IS_GOOD("abc"),
THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

Expand All @@ -22,15 +22,12 @@ public Enum(String value) {
public String toString() {
return "STRING";
}

}

class CLassWithEnum {

public static enum VALID_THINGS {

FIRST, SECOND

FIRST,
SECOND,
}

}
15 changes: 15 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 @@ -156,6 +156,21 @@ public void breakAfterEquals() {
: new Object();
}

public void breakOnTypeArguments() {
ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName> variable;

ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable;

ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable =
new MyExtremelyLongAndObnoxiousClassName<>();

ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> variable =
new MyExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName>();

ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName> aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new MyExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName<ExtremelyLongAndObnoxiousClassName, ExtremelyLongAndObnoxiousClassName>, ExtremelyLongAndObnoxiousClassName>();
}

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,10 +20,15 @@ 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();
Expand Down Expand Up @@ -224,6 +229,57 @@ public void breakAfterEquals() {
: new Object();
}

public void breakOnTypeArguments() {
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName
> variable;

ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
> variable;

ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
> variable = new MyExtremelyLongAndObnoxiousClassName<>();

ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
> variable = new MyExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
>();

ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
> aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new MyExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName<
ExtremelyLongAndObnoxiousClassName,
ExtremelyLongAndObnoxiousClassName
>,
ExtremelyLongAndObnoxiousClassName
>();
}

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

0 comments on commit 2c6b86c

Please sign in to comment.