Skip to content

Fix indentation and closing-paren placement for multi-resource try-with-resources - #1454

Open
saloni-eng wants to merge 2 commits into
google:masterfrom
saloni-eng:fix/try-with-resources-formatting
Open

saloni-eng wants to merge 2 commits into
google:masterfrom
saloni-eng:fix/try-with-resources-formatting

Conversation

@saloni-eng

Copy link
Copy Markdown

Fixes #1392.

Try-with-resources statements with more than one resource were
formatting inconsistently: the first resource stayed glued to try (,
and the closing ) was glued to the last resource, e.g.:

try (var input = Files.newInputStream(Path.of("./input"));
    var output = Files.newOutputStream(Path.of("./output")); ) {

This changes multi-resource try statements to break after (, put
each resource on its own line, and place the closing ) back at the
same indentation as try, matching how other multi-line constructs
(e.g. argument lists) are formatted:

try (
    var input = Files.newInputStream(Path.of("./input"));
    var output = Files.newOutputStream(Path.of("./output"))) {

Single-resource try-with-resources statements are unaffected.

Changes

  • JavaInputAstVisitor.visitTry: conditionally open a plusFour
    indent and force a break after ( only when there's more than one
    resource; close the indent level before the trailing break so the
    closing ) renders at the outer indent instead of the resource
    indent.
  • FormatterTest: added multivariableTryWithResources covering the
    new formatting.
  • testdata/B21465217.output, testdata/B26159561.output: regenerated
    golden files that encoded the old (pre-fix) multi-resource layout.

Testing

mvn clean test passes, including the CR/LF/CRLF idempotency checks
in FormatterIntegrationTest.

…cement for multi-resource try-with-resources
@google-cla

google-cla Bot commented Sep 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

Consistent formatting of multivariable try-with-resource blocks.

1 participant