Skip to content

Make Map.of() and ImmutableMap.of() more readable #1602

@mmitlin-lsw

Description

@mmitlin-lsw

Currently, the formatter will format Map.of() as either:

// Shorter list
Map<String, String> map1 = Map.of("key1", value1, "key2", value2, "key3", value3);

// Longer list
Map<String, String> map2 = Map.of(
        "key1",
        longerValue1,
        "key2",
        longerValue2,
        "key3",
        longerValue3,
        "key4",
        longerValue4,
        "key5",
        longerValue5,
        "key6",
        longerValue6);

These are not very readable, especially when the keys are not visually distinguishable from values.

It would be much more readable if it would format them like this instead:

// Shorter list
Map<String, String> map1 = Map.of(
        "key1", value1,
        "key2", value2,
        "key3", value3);

// Longer list
Map<String, String> map2 = Map.of(
        "key1", longerValue1,
        "key2", longerValue2,
        "key3", longerValue3,
        "key4", longerValue4,
        "key5", longerValue5,
        "key6", longerValue6);

You may need to special case Map.of() and ImmutableMap.of(), as I don't think there is any more general rule.

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