Skip to content

Avoid line continuation escapes in text blocks with mixed newline content#975

Merged
timtebeek merged 1 commit intomainfrom
Jenson3210/fix-textblock-line-continuation
Jan 29, 2026
Merged

Avoid line continuation escapes in text blocks with mixed newline content#975
timtebeek merged 1 commit intomainfrom
Jenson3210/fix-textblock-line-continuation

Conversation

@Jenson3210
Copy link
Contributor

@Jenson3210 Jenson3210 commented Jan 27, 2026

Summary

  • When UseTextBlocks converts a string concatenation that mixes newline-terminated strings with non-newline strings, it no longer inserts \ line continuations for the non-newline joins
  • Instead, those parts are placed on the same text block line
  • Line continuations are still used when the entire content has no newlines, where they provide useful visual structure

Problem

When converting SQL query string concatenations to text blocks, the recipe inserts \ (line continuation) escapes between adjacent strings that aren't separated by \n. In a mixed concatenation like:

String query = "select count(1)\n" +
               "from my_table\n" +
               "where a.id = b.id\n" +
               "and a.stat_cd = 'ACTV'" +
               " and a.del_fl = 'N'" +
               " and a.rgn = :region";

The recipe would produce unexpected \ characters:

String query = """
               ...
               and a.stat_cd = 'ACTV'\
                and a.del_fl = 'N'\
                and a.rgn = :region""";

While semantically correct, the \ line continuation is unfamiliar to many developers and perceived as a bug.

Solution

Added a contentHasNewlines guard to the passPhrase insertion logic. When the concatenated content already contains interior newlines (mixed case), non-newline-joined strings are simply concatenated on the same text block line instead of being split with \ continuations:

String query = """
               ...
               and a.stat_cd = 'ACTV' and a.del_fl = 'N' and a.rgn = :region""";

Test plan

  • Existing tests pass (all 27 UseTextBlocksTest cases)

  • New tests: noLineContinuationWhenContentHasNewlines — trailing non-newline strings

  • New tests: noLineContinuationInMixedConcatenation — end-of-query non-newline strings

  • New tests: noLineContinuationInMiddleOfMixedConcatenation — mid-query non-newline join

  • Fixes moderneinc/customer-requests#1763

@Jenson3210
Copy link
Contributor Author

Note for reviewers: This seems to be bit opinionated issue raised.

While semantically correct, the \ line continuation is unfamiliar to many developers and perceived as a bug.
Do we really want to concatenate all to single line when they had it on different lines earlier? Or do we want to keep the \ in there?

The \s escape for trailing whitespace preservation is correct behavior — without it, Java text blocks would strip trailing spaces, changing the string's value. This is not a bug.

@Jenson3210 Jenson3210 requested a review from timtebeek January 27, 2026 11:42
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Jan 27, 2026
@Jenson3210 Jenson3210 force-pushed the Jenson3210/fix-textblock-line-continuation branch from f6b92f7 to 4ead755 Compare January 27, 2026 15:14
Add a new recipe option `avoidLineContinuations` (default: false) that,
when enabled, avoids using `\` line continuation escapes in text blocks
where the content contains newlines. Non-newline-joined strings are
placed on the same text block line instead.

Previously with the option enabled, "foo\n" + "bar" + " baz" produces:
    foo
    bar baz

Without the option (default), the existing behavior is preserved:
    foo
    bar\
     baz

Fixes moderneinc/customer-requests#1763
@Jenson3210 Jenson3210 force-pushed the Jenson3210/fix-textblock-line-continuation branch from 67e90f4 to 2fd94c6 Compare January 29, 2026 09:34
@timtebeek timtebeek merged commit 653dd7c into main Jan 29, 2026
2 checks passed
@timtebeek timtebeek deleted the Jenson3210/fix-textblock-line-continuation branch January 29, 2026 12:19
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants