Bug 49351 - New code formatter: left curly brace placement
Summary: New code formatter: left curly brace placement
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.0 M7   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-12-26 15:16 EST by Igor Fedorenko CLA
Modified: 2004-02-11 10:42 EST (History)
1 user (show)

See Also:


Attachments
An implementation of requested enhancement (5.27 KB, patch)
2003-12-27 02:03 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.ui (4.40 KB, patch)
2003-12-27 02:05 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.core.tests.model. (10.20 KB, patch)
2003-12-27 02:06 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.core (5.60 KB, patch)
2003-12-27 03:11 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.core.tests.model (11.11 KB, patch)
2003-12-27 03:12 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.core (6.09 KB, patch)
2003-12-28 23:00 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.core.tests.model (11.21 KB, patch)
2003-12-28 23:02 EST, Igor Fedorenko CLA
no flags Details | Diff
Patch for org.eclipse.jdt.ui (6.45 KB, patch)
2003-12-28 23:10 EST, Igor Fedorenko CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Igor Fedorenko CLA 2003-12-26 15:16:02 EST
It would be nice if the new code formatter supported "new line on wrap" way of
placing left curly brace. The following is a quote from CheckStyle documentation:

If the brace will fit on the first line of the statement, taking into account
maximum line length, then apply eol rule [The brace must always be on the end of
the line]. Otherwise apply the nl rule [The brace must always be on a new line].
nlow is a mnemonic for "new line on wrap". For the example above Checkstyle will
enforce:

    if (condition) {
        ...

But for a statement spanning multiple lines, Checkstyle will enforce:

    if (condition1 && condition2 &&
        condition3 && condition4)
    {
        ...

Thanks in advacena and sorry if this is a dup.
Comment 1 Igor Fedorenko CLA 2003-12-27 02:03:20 EST
Created attachment 7281 [details]
An implementation of requested enhancement
Comment 2 Igor Fedorenko CLA 2003-12-27 02:05:34 EST
Created attachment 7282 [details]
Patch for org.eclipse.jdt.ui
Comment 3 Igor Fedorenko CLA 2003-12-27 02:06:18 EST
Created attachment 7283 [details]
Patch for org.eclipse.jdt.core.tests.model.
Comment 4 Igor Fedorenko CLA 2003-12-27 03:11:04 EST
Created attachment 7284 [details]
Patch for org.eclipse.jdt.core
Comment 5 Igor Fedorenko CLA 2003-12-27 03:12:47 EST
Created attachment 7285 [details]
Patch for org.eclipse.jdt.core.tests.model
Comment 6 Igor Fedorenko CLA 2003-12-27 03:25:16 EST
I have attached more or less complete implementation of the requested
enhancement and hope you'll find this code useful (apply the patches to the
corresponding plugins). I did not implement "new line on wrap" formatting of
"catch" blocks, anonymous classes, "do" and "switch" statements and
"synchronized" blocks as I was not sure about practical benefits of this. It
should not be difficult to implement this missing formatting and I will be glad
to do that if you think it is important.

I also would like to thank you for the great code; it was real pleasure to work
with it. One little suggestion – FormatterRegressionTests does need to be broken
into a number of smaller test suites ;-)

Regards,
Igor Fedorenko

PS: just noticed that tests patch contains extra file
workspace/Converter/test0191/Test.java which I did not create. Looks like
Eclipse lost CVS/Entries file.
Comment 7 Igor Fedorenko CLA 2003-12-28 23:00:38 EST
Created attachment 7289 [details]
Patch for org.eclipse.jdt.core

Fixed a problem with comments preceding type/method declarations and
statements.
Comment 8 Igor Fedorenko CLA 2003-12-28 23:02:25 EST
Created attachment 7290 [details]
Patch for org.eclipse.jdt.core.tests.model

Fixed a problem with comments preceding type/method declarations and
statements.
Comment 9 Igor Fedorenko CLA 2003-12-28 23:10:23 EST
Created attachment 7291 [details]
Patch for org.eclipse.jdt.ui

Set line width for brace placement preview window.

PS: sorry for the flurry of updates. will try not to do that in the future.
Comment 10 Olivier Thomann CLA 2004-01-02 13:34:35 EST
I will review your patch.
Comment 11 Olivier Thomann CLA 2004-01-06 14:53:10 EST
I agree with you for comment 6, but to be consistent we should do it for all
kind of blocks:
Class declaration
Anonymous class declarations
Method declaration
Blocks
'switch...case' statement.

So we could exclude:
Anonymous class declarations
'switch...case' statement.

But we would have to handle the three others. This means that catch,
synchronized statement and do/while would be handled as well.

What do you think?
Comment 12 Igor Fedorenko CLA 2004-01-07 00:59:39 EST
This rule is supposed to make it easy to find where block code begins. 

So, for "one-world" blocks -- "try", "do", "finally" -- this rule does not seem
to help at all because you cannot write a keyword on multiple lines. The only
condition when this rule can be applied is page width, more precisely, when
keyword fits within set limits but lcrurly does not. In my opinion moving lcurly
to the next line in this case make code less readable.

The other two types of block are "synchronized(...)" and "catch(...)". For these
two blocks I agree, applying this rule makes sense, I even have sensibly
examples (input for unit tests ;-)

   synchronized (object
                       .method()
                       .method()
                       .method())
   {
       ...

and

   catch (com
             .ibm
             .tivoli
             .orchestrator
             .de
             .scriptlet
             .ExecutionException ex)
   {
      ...

Note that the same logic can not be applied to "switch" statement because it
does not directly contain any code, only case/default keywords.

I am also not so sure about anonymous class declarations... It may actually make
sense to apply this rule to something like the code below. What do you think?

   Object o = new com
                     .thinkdynamics
                     .kanaha
                     .dataacquisitionengine
                     .Driver() 
   {
      ...

Thanks for looking at this and I will update the patch to handle
synchronized/catch blocks tomorrow. Btw, my sametime id is igorf@ca.ibm.com,
don't hesitate to contact me if you have any further questions or concerns.
Comment 13 Silvio Böhler CLA 2004-01-07 05:56:43 EST
If I understood correctly, this would add some boolean options for each of the
aforementioned blocks? No problem to add them to the preference page.
Comment 14 Igor Fedorenko CLA 2004-01-07 10:49:51 EST
The patch adds new value to existing configuration options. For UI it means that
new items are added to existing drop-downs, but otherwise layout of the
preference page does not change.
Comment 15 Silvio Böhler CLA 2004-01-07 11:17:32 EST
So you mean that for XXX_ALIGNMENT, more options are possible? Does this apply 
to ANY XXX_ALIGNMENT option? Else I'd have to do a major rewrite of this 
specific tab page.
Comment 16 Olivier Thomann CLA 2004-01-07 11:24:05 EST
My understanding is that this is true only for the open brace positioning.
So you would have some different combo box between different kinds of blocks. 
This is why I asked you how easy it was for you to handle this.
Comment 17 Igor Fedorenko CLA 2004-01-07 13:49:41 EST
I don't know if you've noticed that, but my patch is supposed to deal with this.
It shows "Next line on wrap" option only for applicable block types.
Implementation is somewhat clumsy though.
Comment 18 Olivier Thomann CLA 2004-01-07 15:10:01 EST
I am only looking at the JDT/Core part of the patch.
I will update your code to handle all types of blocks. Then Silvio will update
the code formatter preference page.
Comment 19 Olivier Thomann CLA 2004-01-07 16:03:09 EST
Fixed and released in HEAD.
Regression test added.
Silvio, please update the code formatter preference page.
The new value NEXT_LINE_ON_WRAP can be used for all brace positionning except
for the 'switch...case' statement.
Not all cases with blocks are covered, because for some of them it would not
change anything.
Hopefully this corresponds to the requested feature.
Comment 20 Igor Fedorenko CLA 2004-01-14 22:29:34 EST
I've checked the latest integration build (200401130800) and everything works as
requested. One minor suggestion is to improve preferance page to show what this
new options means.
Comment 21 Frederic Fusier CLA 2004-02-11 10:42:19 EST
Verified for 3.0-M7 with build I200402102000.