How to keep custom indentation for binary operation in Eclipse formatter

74 views Asked by At

I was testing the eclipse formatter to try to have something that match my current coding format in Java. However, I was not able to find an option to keep current indentation for string concatenation (binary operation). For example, if I want to write this string (SQL query):

// Current code, I want to keep this format
String query = "select "
            + "a, "
            + "b, "
            + "c, "
        + "from table "
        + "where "
            + "a = 1 "
            + "and b = 2 "
        + "order by c";

Everything will be wrapped at the same indentation (I checked the option Never join already wrapped lines)

// Formatted code
String query = "select "
        + "a, "
        + "b, "
        + "c, "
        + "from table "
        + "where "
        + "a = 1 "
        + "and b = 2 "
        + "order by c";

Which I find less readable.

I saw there was an option to turn off formatter for portion of code, but I would like to know if there is an already built-in option for my need.

1

There are 1 answers

0
greg-449 On BEST ANSWER

From Java 15 onward a "text block" would be most readable:

String query = """
    select
      a,
      b,
      c,
     from table
     where
      a = 1
      and b = 2
     order by c
     """.replace("\n", "");

which produces:

select  a,  b,  c, from table where  a = 1  and b = 2 order by c

which has a few extra unimportant blanks.