This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
Summary: | Format code doesn't handle double brace initialization | ||
---|---|---|---|
Product: | java | Reporter: | dfa <dfa> |
Component: | Editor | Assignee: | Dusan Balek <dbalek> |
Status: | NEW --- | ||
Severity: | blocker | CC: | emanuel_ |
Priority: | P3 | ||
Version: | 6.x | ||
Hardware: | PC | ||
OS: | Windows Vista | ||
URL: | http://c2.com/cgi/wiki?DoubleBraceInitialization | ||
Issue Type: | ENHANCEMENT | Exception Reporter: |
Description
dfa
2008-11-21 23:09:23 UTC
BTW, the idiom seems quite wasteful to me... maybe wasteful, but is there any way to force syntax formatter keep the double braces initialization in the same format as shown by @dfa? new Foo() {{ // some code here }} alt-shift-f breaks it into new Foo() { { // some code here } } which is waste of lines and looks ugly Agreed. Handling double braces in a special way would be very helpful. Although the idiom is often discouraged, I think it does have a place, particularly when using a tool like JMockit. You see this very often when using JMockit: new Expectations() {{ myClass.getValue(); returns(11); }}; However, it gets reformatted to this: new Expectations() { { myClass.getValue(); returns(11); } }; You also see this idiom initializing Sets, etc.: Set<Integer> intSet = new HashSet<Integer>(){{ add(4); }}; Which gets reformatted to: Set<Integer> intSet = new HashSet<Integer>() { { add(4); } }; I think the first sample would be better, and something like this if the other preferences cause the initialization to wrap: Set<Integer> intSet = new HashSet<Integer>() {{ add(4); }}; I think a reasonable solution would be an option for "keep braces together in double brace initialization idiom" -- which means when a class/variable initialization/override uses the initialization idiom, for formatting purposes treat it as a single brace (subject to all the newline stuff, etc. associated with single braces). |