Skip to content

Long generic types cause excessive indentation #1286

@peterbn

Description

@peterbn

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions