Copilot commented on code in PR #4082:
URL:
https://github.com/apache/incubator-kie-tools/pull/4082#discussion_r4594421707
##########
packages/drools-lsp/drools-formatter/src/main/java/org/drools/formatter/RhsFormatter.java:
##########
@@ -220,10 +287,38 @@ private void emitRhsStatementInBlock(List<String> toks) {
return;
}
+ // ── Step 3: emit
─────────────────────────────────────────────────────────
emitRhsStatement(toks);
Review Comment:
For a braced `if` whose opening brace is on the next line (`if
(a)\n{\nx();\n}`), the preceding header has already increased `e.depth`. The
opening brace is therefore emitted one level too deep here; resetting depth
afterward cannot correct the emitted line. Align a standalone opening brace
with its header before emission, and cover this brace style in the tests.
##########
packages/drools-lsp/drools-formatter/src/main/java/org/drools/formatter/RhsFormatter.java:
##########
@@ -220,10 +287,38 @@ private void emitRhsStatementInBlock(List<String> toks) {
return;
}
+ // ── Step 3: emit
─────────────────────────────────────────────────────────
emitRhsStatement(toks);
+ // ── Step 4: depth adjustments after emitting
─────────────────────────────
if (last.equals("{")) {
+ // A "{" that immediately follows an unbraced if/else header satisfies
it;
+ // the context was for a single-statement consequence, but the user wrote
+ // a block — pop it without the depth that openBlock() will add again.
+ if (!ifStack.isEmpty() && !ifStack.peek().consequenceDone) {
+ IfContext ctx = ifStack.pop();
+ // Restore depth to base so that openBlock() brings it to base+1.
+ e.depth = ctx.baseDepth;
Review Comment:
For `if (a)\nif (b) {\nwork();\n}\nafter();`, this pops the outer
conditional as soon as the inner block opens. As a result, `work()` aligns with
`if (b)` and the closing brace aligns with `if (a)`; braced loops or `modify`
blocks used as the consequence have the same problem. Distinguish a standalone
body-opening brace from a nested statement's block, and retain the enclosing
context until the entire statement finishes.
##########
packages/drools-lsp/drools-formatter/src/main/java/org/drools/formatter/RhsFormatter.java:
##########
@@ -220,10 +287,38 @@ private void emitRhsStatementInBlock(List<String> toks) {
return;
}
+ // ── Step 3: emit
─────────────────────────────────────────────────────────
emitRhsStatement(toks);
+ // ── Step 4: depth adjustments after emitting
─────────────────────────────
if (last.equals("{")) {
+ // A "{" that immediately follows an unbraced if/else header satisfies
it;
+ // the context was for a single-statement consequence, but the user wrote
+ // a block — pop it without the depth that openBlock() will add again.
+ if (!ifStack.isEmpty() && !ifStack.peek().consequenceDone) {
+ IfContext ctx = ifStack.pop();
+ // Restore depth to base so that openBlock() brings it to base+1.
+ e.depth = ctx.baseDepth;
+ }
openBlock();
+ } else if (isUnbracedIfElseHeader(toks)) {
+ // Push a new context and indent one level for the consequence.
+ boolean canTakeElse = isIfOrElseIfHeader(toks);
+ ifStack.push(new IfContext(e.depth, canTakeElse));
+ e.depth++;
+ } else {
+ // This statement completes the consequence for the pending unbraced
if/else chain.
+ // In a nested chain like "if (a) if (b) stmt;", stmt completes both if
(b) and if (a).
+ if (!ifStack.isEmpty()) {
+ int targetDepth = e.depth;
+ for (IfContext ctx : ifStack) {
+ if (!ctx.consequenceDone) {
+ ctx.consequenceDone = true;
+ targetDepth = ctx.baseDepth;
+ }
+ }
Review Comment:
For `if (a)\nif (b)\none();\nelse\ntwo();\nafter();`, the outer context is
already marked complete when `two()` is emitted, so this loop restores only the
inner branch's base depth. `after()` stays one level too deep, and the extra
depth also carries into subsequent consequence/rule output. Restore the base
depth of the enclosing completed contexts as well, and add a nested-if/else
regression test with a following sibling.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]