palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
403 stars 43 forks source link

Poor formatting of generic parameters on methods when class names are long #1074

Open christophercolumbusdog opened 2 months ago

christophercolumbusdog commented 2 months ago

What happened?

We encountered a class in our codebase with a generic parameter T on one of its methods. T has bounds that involve classes with long names, and when we format this class with Palantir format, we get the following:

public class CrazyGenerics {

    public <
                    T extends
                            FantasticVeryLongNamedContainerWithLongName<?, ?, ?, ?>
                                    & OtherLongNamedTypeWithOnlyOneGenericType<?>>
            void computeInfo() {}

    public interface FantasticVeryLongNamedContainerWithLongName<FirstType, SecondType, ThirdType, FourthType> {
        FirstType getFirst();

        SecondType getSecond();

        ThirdType getThird();

        FourthType getFourth();
    }

    public interface OtherLongNamedTypeWithOnlyOneGenericType<FirstType> {
        FirstType getFirst();
    }
}

What did you want to happen?

I could see a few better alternatives.

Be consistent with the indentation after methods:

    public <
            T extends
                    FantasticVeryLongNamedContainerWithLongName<?, ?, ?, ?>
                            & OtherLongNamedTypeWithOnlyOneGenericType<?>>
            void computeInfo() {}

Or perhaps leave the generic type on the original line:

    public <T extends
            FantasticVeryLongNamedContainerWithLongName<?, ?, ?, ?>
                    & OtherLongNamedTypeWithOnlyOneGenericType<?>>
            void computeInfo() {}

Other suggestions welcome, but I hope it's clear why the original is not ideal.