Skip to content

Commit

Permalink
Fix indent of body of try-with-resources statement (#2229)
Browse files Browse the repository at this point in the history
- block indenting for a try-with-resources block uses the indent
  of the left parenthesis line as base indent but when multiple
  resources are used, they are indented so the block must use
  the indent of the try statement start
- this is needed for
  eclipse-jdt/eclipse.jdt.ui#1261
- fix ASTRewriteFlattener to properly add space before body parenthesis
- fixes #2227
  • Loading branch information
jjohnstn committed Mar 27, 2024
1 parent aa8844b commit 30df176
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -2310,7 +2310,23 @@ public boolean visit(Block node) {
} else {
startPos= getPosAfterLeftBrace(node.getStartPosition());
}
int startIndent= getIndent(node.getStartPosition()) + 1;
// for a try-with-resources body, the start of the block may be on the line of the
// last resource statement which means it could already be indented and so we
// must use the base indent of the try statement to indent the body correctly
boolean needParentIndent= false;
if (node.getLocationInParent() == TryStatement.BODY_PROPERTY) {
TryStatement parent= (TryStatement)node.getParent();
if (!parent.resources().isEmpty()) {
List<Expression> resources= parent.resources();
int lastResourcePos= resources.get(resources.size() - 1).getStartPosition();
int lastResourceLine= getLineInformation().getLineOfOffset(lastResourcePos);
int blockLine= getLineInformation().getLineOfOffset(node.getStartPosition());
if (blockLine == lastResourceLine) {
needParentIndent= true;
}
}
}
int startIndent= needParentIndent ? (getIndent(node.getParent().getStartPosition()) + 1) : getIndent(node.getStartPosition()) + 1;
rewriteParagraphList(node, Block.STATEMENTS_PROPERTY, startPos, startIndent, 0, 1);
return false;
}
Expand Down Expand Up @@ -4024,7 +4040,6 @@ public boolean visit(TryStatement node) {
pos= doVisit(node, desc, pos);
}
}

pos= rewriteRequiredNode(node, TryStatement.BODY_PROPERTY);

if (isChanged(node, TryStatement.CATCH_CLAUSES_PROPERTY)) {
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1175,6 +1175,7 @@ public boolean visit(TryStatement node) {
StructuralPropertyDescriptor desc = level < JLS9_INTERNAL ? INTERNAL_TRY_STATEMENT_RESOURCES_PROPERTY : TryStatement.RESOURCES2_PROPERTY;
visitList(node, desc, String.valueOf(';'), String.valueOf('('), String.valueOf(')'));
}
this.result.append(' ');
getChildNode(node, TryStatement.BODY_PROPERTY).accept(this);
this.result.append(' ');
visitList(node, TryStatement.CATCH_CLAUSES_PROPERTY, null);
Expand Down

0 comments on commit 30df176

Please sign in to comment.