eclipse-jdt / eclipse.jdt.ui

Eclipse Public License 2.0
36 stars 86 forks source link

Code formatter: Suboptimal intent of method arguments #1372

Open jubax opened 4 months ago

jubax commented 4 months ago

The String.formatted() method is not correctly intended for text blocks: I want:

StringBuilder sql = new StringBuilder();
sql.append("""
        INSERT INTO %s (%s) SELECT DISTINCT %s FROM %s
        """.formatted(
                "1",
                "2",
                "3",
                "4"));

but I get

StringBuilder sql = new StringBuilder();
sql.append("""
        INSERT INTO %s (%s) SELECT DISTINCT %s FROM %s
        """.formatted(
        "1",
        "2",
        "3",
        "4"));

For regular strings the formatting is better. But I think it is wrong that the formatting does not add another indentation level.

jukzi commented 4 months ago

The "problem" is not specific to .formatted() but applies to all methods with argument. Example:

public class C {
    public static void main(String[] args) {
        new String("""
                hello
                """.formatted(
                "HalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongString"));
        new String("hello".formatted(
                "HalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongString"));
        new Integer("""
                hello
                """.indexOf(
                "HalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongString"));
        new Integer("hello".indexOf(
                "HalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongStringHalloVeryLongString"));
    }
}

however i don't see that regular Strings are any other in my simplified example.