Skip to content

Force a line break before text blocks after return - #1467

Open
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-1258-return-text-block
Open

tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-1258-return-text-block

Conversation

@tanvir-ux

Copy link
Copy Markdown

Summary

Fixes #1258.

visitReturn always emitted a space before the expression. For ordinary expressions that is correct, but for text blocks Google Java Style (§4.8.9) wants the opening delimiter on its own line:

return
    """
    this is sample text
    """;

Because return """ fits on one line, a fill-mode break never fires and the formatter previously kept the delimiter attached to return, producing incorrect content indentation (confirmed by @cushon on the issue).

Change

  • Detect text-block literals in visitReturn and emit a forced +4 break before scanning them (same structural idea as assignment / variable initializers).
  • Leave ordinary return "…" formatting unchanged.

Test plan

  • mvn -pl core -Dtest=FormatterTest#returnTextBlockStartsOnOwnLine+returnOrdinaryStringUnchanged test — 2/2
  • mvn -pl core -Dtest=FormatterTest,StringWrapperTest test — 49/49
  • CLI format of the issue repro matches the expected style above

Google Java Style requires the opening """ of a text block to be on its
own line. visitReturn used a plain space before the expression, so
`return """` stayed on one line (it fits) and content indentation was
wrong. Force a +4 break when the return expression is a text block.

Fixes google#1258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect behaviour on Text-blocks preceded by return

1 participant