palantir-java-format icon indicating copy to clipboard operation
palantir-java-format copied to clipboard

Text block get formatted uggly

Open scott-avery opened this issue 2 years ago • 9 comments

What happened?

this tool formats text block in not ideal format.

see original verion.

    @Update("""
    update master_data_manager
    set version = version + 1, update_time = now(), update_by = #{userId}
    where site_id = #{siteId} and table_name = #{tableName}
    """)

output

    @Update(
            """
    update master_data_manager
    set version = version + 1, update_time = now(), update_by = #{userId}
    where site_id = #{siteId} and table_name = #{tableName}
    """)

What did you want to happen?

keep the same as original.

scott-avery avatar Sep 18 '23 06:09 scott-avery

+1000 on improving the formatting of text blocks, for me I find I use .formatted() at the end of them often and that also drops to the next line which looks ugly, kinda lie:

var text =
        """
    update master_data_manager
    set version = version + 1, update_time = now(), update_by = #{userId}
    where site_id = %s and table_name = #{tableName}
    """
         .formatted("site");

Ideally, the opening """ would also be hanging, and the code following on the same line, something like:

var text = """
    update master_data_manager
    set version = version + 1, update_time = now(), update_by = #{userId}
    where site_id = %s and table_name = #{tableName}
    """.formatted("site");

would be ideal.

talios avatar Sep 22 '23 11:09 talios

Example from a wiremock test case. A beauty in its own... don't you think? 😉

20231009_002339.jpg

bmarwell avatar Oct 08 '23 22:10 bmarwell

If memory serves, we don’t currently format text blocks, only the code around them. This is due to their structure being both a string a block which is indention-sensitive. Definitely room for improvement on this :-)

carterkozak avatar Oct 08 '23 23:10 carterkozak

@carterkozak From memory, javac automatically strips away the incidental/grey white space (the initial indentation on the first line of the text block), so changing that initial base indentation shouldn't cause any issues - as long as any additional indentation is respected.

talios avatar Oct 10 '23 23:10 talios

+1 to get something more common/standard and less unlikely by default, formatting should stay well readable after all ;)

rmannibucau avatar Feb 22 '24 08:02 rmannibucau

javac automatically strips away the incidental/grey white space (the initial indentation on the first line of the text block), so changing that initial base indentation shouldn't cause any issues - as long as any additional indentation is respected.

+1, there is a nice overview here: https://docs.oracle.com/en/java/javase/21/text-blocks/index.html#incidental-white-space

For what it's worth, the approach google-java-format is currently taking for text blocks is to re-indent them to the current continuation indent (see here).

cushon avatar Mar 02 '24 23:03 cushon

@CRogers any chance we can get an update here?

bmarwell avatar May 06 '24 18:05 bmarwell

Another example case:

image

mhagnumdw avatar Jun 11 '24 17:06 mhagnumdw