Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I stop Intellij from splitting string literals when running the code formatter?

More or less exactly what the title says. When I cmd + f, it formats the code according to the settings for the project. One of those settings is the right margin. The problem is it will split string literals also to avoid over running the margin. So this:

stringBldr.append("some text that's more than 80 characters");

Becomes this:

stringBldr.append("some text that's more " +
          "than 80 characters");

Which completely defeats the purpose of using the StringBuilder in the first place. In Eclipse, I could tell it to ignore String literals when formatting. Is there a way to do this in IntelliJ without manually disabling the code formatter for that section (e.g. @formatter:off)?

like image 530
Chris Thompson Avatar asked Jan 19 '26 08:01

Chris Thompson


1 Answers

In your case, try to set

Editor > Code Style > Java > Wrapping and Braces > Method call arguments

to Wrap if long. And potentially with New line after '('. It would put the start of the String on the next line and unless the string is too long on it's own (+ indent), it won't be cut into two.

Here is before formatting (my hard wrap limit is at 120 characters and the lorem is 100 characters long making second line 125 characters long)

    StringBuilder stringBldr = new StringBuilder();
    stringBldr.append("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...");

Here is with Method call arguments set at Wrap if long and New line after '(' selected

    StringBuilder stringBldr = new StringBuilder();
    stringBldr.append(
        "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...");
like image 86
Ylane Turam Avatar answered Jan 21 '26 20:01

Ylane Turam