google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Long generic types cause excessive indentation

Open peterbn opened this issue 3 months ago • 1 comments

Hi,

I was writing an interface that used some rather long generic type names, and when I ran it through google-java-format --aosp, I noticed that the type names got very uncomfortably formatted. I acknowledge that this is probably at the edge of what can be reasonably formatted, but I hope it can still be improved

I have provided an example input and the output I get below, as well as my suggestion for improvement.

The suggested points of improvement are:

  • Do not break line after ? extends or ? super if the following TypeName< can also fit on the line.
  • Use 4 spaces for each nested level of type. The first line is currently indented 16 spaces, which seems like a lot, and every following level uses 8 spaces.
  • Don't indent the method name, but keep it at the same level as the type.

Input file

class Example {
      List<? extends Consumer<BiConsumer<@ThisIsAnIntDefAnnotationWithATooLongNameToFit Integer, Object>>>
      methodWithComplexReturnType1();

      List<? extends Consumer<BiConsumer<VeryLongAndComplicatedTypeName, OtherVeryLongAndComplicatedName>>>
      methodWithComplexReturnType2();

      List<Consumer<BiConsumer<VeryLongAndComplicatedTypeNameIsTooLong, OtherVeryLongAndComplicatedName>>>
      methodWithComplexReturnType3();

      GenericType<VeryLongAndComplicatedTypeNameThatBreaksTheLineLength, OtherVeryLongAndComplicatedName>
      methodWithComplexReturnType4();
}

Produced output by running google-java-format --aosp Example.java

class Example {
    List<
                    ? extends
                            Consumer<
                                    BiConsumer<
                                            @ThisIsAnIntDefAnnotationWithATooLongNameToFit Integer,
                                            Object>>>
            methodWithComplexReturnType1();

    List<
                    ? extends
                            Consumer<
                                    BiConsumer<
                                            VeryLongAndComplicatedTypeName,
                                            OtherVeryLongAndComplicatedName>>>
            methodWithComplexReturnType2();

    List<
                    Consumer<
                            BiConsumer<
                                    VeryLongAndComplicatedTypeNameIsTooLong,
                                    OtherVeryLongAndComplicatedName>>>
            methodWithComplexReturnType3();

    GenericType<
                    VeryLongAndComplicatedTypeNameThatBreaksTheLineLength,
                    OtherVeryLongAndComplicatedName>
            methodWithComplexReturnType4();
}

Suggested better output

class Example {
    List<
      ? extends Consumer<
          BiConsumer<
              @ThisIsAnIntDefAnnotationWithATooLongNameToFit Integer,
              Object>>>
    methodWithComplexReturnType1();

    List<
      ? extends Consumer<
          BiConsumer<
              VeryLongAndComplicatedTypeName,
              OtherVeryLongAndComplicatedName>>>
    methodWithComplexReturnType2();

    List<
        Consumer<
            BiConsumer<
                VeryLongAndComplicatedTypeNameIsTooLong,
                OtherVeryLongAndComplicatedName>>>
    methodWithComplexReturnType3();

    GenericType<
        VeryLongAndComplicatedTypeNameThatBreaksTheLineLength,
        OtherVeryLongAndComplicatedName>
    methodWithComplexReturnType4();
}

peterbn avatar Sep 24 '25 15:09 peterbn