prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

String concatenation formatting

Open JanecekPetr opened this issue 3 years ago • 2 comments

Prettier-Java 1.6.1

Related to https://github.com/jhipster/prettier-java/issues/77. In this case we're getting compilable code, it's just sub-ideal.

# Options (if any):
--print-width 140

Input:

PreparedStatement statement =
        connection.prepareStatement(
                "INSERT INTO noneofyourbusiness(" +
                        "field1," +
                        "field2," +
                        "field3," +
                        "field4) " +
                        "VALUES (?, ?, ?, ?)");

Output:

PreparedStatement statement = connection.prepareStatement(
    "INSERT INTO noneofyourbusiness(" + "field1," + "field2," + "field3," + "field4) " + "VALUES (?, ?, ?, ?)"
);

Expected behavior: I see two options:

  • do not join the lines if they contain string concatenation like this
  • join, but make sure to get rid of all the concatenation

JanecekPetr avatar Apr 05 '22 15:04 JanecekPetr

Hi @JanecekPetr,

I understand that this might be a little annoying.

However, I do not to get rid of the concatenation as it would change the AST, which we want to avoid. And I don't think we want to introduce specificity for printing string concatenation either, as it would introduce some complexity for a limited (and maybe controversial) value.

clementdessoude avatar May 22 '22 05:05 clementdessoude

That's fair. So we do not want to make a single concatenated String, I'm fine with that.

Would it be better in those cases to not join the lines, but rather follow the human flow, and keep the strings on separate lines as a human put them there originally?

JanecekPetr avatar Jun 16 '22 00:06 JanecekPetr