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.

Bug 269050 - Improve java source reformatting of try and synchronized blocks with control structures
Summary: Improve java source reformatting of try and synchronized blocks with control ...
Status: STARTED
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: Dev
Hardware: All All
: P3 normal (vote)
Assignee: bondolo
URL:
Keywords: PATCH_AVAILABLE
Depends on:
Blocks:
 
Reported: 2016-11-21 00:40 UTC by bondolo
Modified: 2018-02-20 11:19 UTC (History)
2 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
patch to improve formatting of try/synchronized blocks with control structures. (1.78 KB, patch)
2016-11-26 23:55 UTC, bondolo
Details | Diff
Improved patch (1.77 KB, patch)
2016-11-27 21:46 UTC, bondolo
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description bondolo 2016-11-21 00:40:08 UTC
Currently the java source reformatter (java.source.base org.netbeans.modules.java.source.save.Reformatter) does not treat try and synchronized blocks as blocks when it encounters them with control structures. So


if (foo == bar) try {
   baz();
} catch (Exception all) {
   log.(....);
}

is reformatted as :

if (foo == bar) {
  try {
    baz();
  } catch (Exception all) {
    log.(....);
  }
}

The additional added basic block layer is not needed as the try is already a block. The same applies for a synchronized block as well.

if (foo == bar) synchronized(quux) {
   baz();
}

is reformatted as :

if (foo == bar) {
  synchronized(quux) {
    baz();
  }
}

In addition to "if/else" this formatting all affects other control structures such as "for", "for-each" and "while".
Comment 1 bondolo 2016-11-21 00:43:05 UTC
I am going to attempt to fix this issue as my first contribution to the Apache NetBeans Incubator project. (Hints would be welcomed).
Comment 2 bondolo 2016-11-26 23:55:12 UTC
Created attachment 163037 [details]
patch to improve formatting of try/synchronized blocks with control structures.
Comment 3 bondolo 2016-11-27 21:46:43 UTC
Created attachment 163045 [details]
Improved patch
Comment 4 bondolo 2016-12-21 17:44:28 UTC
In addition to try and synchronized blocks this could be extended to switch and do blocks.
Comment 5 emi 2017-01-18 13:27:26 UTC
[Copying what I've mentioned on nbdev@]

The patch is quite clean, although I don't personally like the style it encourages.

Even if try and synchronized blocks are blocks, I really prefer the way the current NetBeans formatter wraps them in an explicit block.

It really does feel like a matter of preference so perhaps it should be hidden behind some flag?
Comment 6 bondolo 2017-01-18 22:15:50 UTC
Try it, you'll love it! 

I would be fine with adding a preference if others also would prefer this be controllable.
Comment 7 emi 2017-01-18 22:42:15 UTC
> Try it, you'll love it!

As I've said, it's a matter of preference. I have never seen such formatting. Is there any other IDE or tool that formats blocks this way?

For example, to me, synchronized really should begin the line. Hiding it after an `if` would really invite some bugs because it's easy to overlook it.

> I would be fine with adding a preference if others also would prefer this be controllable.

I'm not entirely certain how the process would be for this.

This seems a niche feature that could just have a System property flag. Then users that prefer it should edit their netbeans.conf file.

As an UI preference I guess it could go under Editor | Formatting | Java/Braces. Something like "Special try / synchronized Blocks Treatement' underneath "Special else if Treatment". But I really wonder how many people would touch it.
Comment 8 ebakke 2018-02-20 11:19:11 UTC
I must agree with Emi here--the proposed style is non-standard, and arguably problematic. Are there any large existing codebases or organizations that use it?