palantir / palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Apache License 2.0
426 stars 45 forks source link

Text block get formatted uggly #930

Open scott-avery opened 12 months ago

scott-avery commented 12 months ago

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.

talios commented 11 months ago

+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.

bmarwell commented 11 months ago

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

20231009_002339.jpg

carterkozak commented 11 months ago

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 :-)

talios commented 11 months ago

@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.

rmannibucau commented 6 months ago

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

cushon commented 6 months ago

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).

bmarwell commented 4 months ago

@CRogers any chance we can get an update here?

mhagnumdw commented 3 months ago

Another example case:

image