Skip to content

Fix sout expansion in unbraced for-loop bodies - #9595

Open
Stuaarts wants to merge 1 commit into
apache:masterfrom
Stuaarts:fix/5244-sout-for-loop
Open

Fix sout expansion in unbraced for-loop bodies#9595
Stuaarts wants to merge 1 commit into
apache:masterfrom
Stuaarts:fix/5244-sout-for-loop

Conversation

@Stuaarts

@Stuaarts Stuaarts commented Sep 2, 2026

Copy link
Copy Markdown

Description

Fixes Apache NetBeans issue #5244. Java code templates such as sout were rejected when entered as the single-statement body of a for loop without braces.

The filter already supported FOR_LOOP and ENHANCED_FOR_LOOP, but it applied a loop-header closing-parenthesis check to the body as well. The fix uses the loop statement source position to distinguish body context from header context, preserving the existing header validation.

Tests

  • ant '-Dcluster.config=basic' build (baseline passed)
  • ant -f java/java.editor/build.xml test-single -Dtest.includes=org/netbeans/modules/editor/java/JavaCodeTemplateProcessorTest.java (passed)
  • Manual test in rebuilt NetBeans IDE: original unbraced for body sequence expands sout to System.out.println("");
  • Full java.editor unit suite: unrelated ComputeImportsTest CRLF/LF fixture failures on Windows; other suites passed

Assisted-by: GPT-5 Codex GPT-5

Preserve loop-header validation while accepting code templates in single-statement loop bodies. Add regression coverage for classic and enhanced for loops and incomplete headers.

Assisted-by: GPT-5 Codex GPT-5
@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Sep 2, 2026
@mbien mbien linked an issue Sep 2, 2026 that may be closed by this pull request
@apache apache locked and limited conversation to collaborators Sep 2, 2026
@apache apache unlocked this conversation Sep 2, 2026
@mbien mbien added this to the NB32 milestone Sep 2, 2026

@mbien mbien left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, looks good so far!

We would require a proper email address in the commit header, you can check it by looking at the patch of the PR: https://github.com/apache/netbeans/pull/9595.patch

Github anonymizes the email address automatically if submitted via web interface, but there is a setting somewhere in your account options if you like to change it there. You can also fix the commit locally if you want. Once fixed you can force push into the PR.

(git commit --amend --author="Name <email@example.com>" --no-edit would change the mail of the last commit. git log to verify)

Its ok to edit the commit and force push into PR branches for this project, since we merge PRs as-is without squashing. So it should be only one commit before merge.

Comment on lines +208 to +213
assertSoutTemplateAccepted("public class Test {\n"
+ " private void test() {\n"
+ " for (int i = 0; i < 3; i++)\n"
+ " sout\n"
+ " }\n"
+ "}", true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: feel free to use multi line Strings. The editor has a hint to convert to it.

(same for the other new test cases you added)

@mbien
mbien marked this pull request as ready for review September 3, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sout + <tab> does not work under for

2 participants