Bug 52679 - Code formatter formats braces in case and default statements, but no settings exist
Summary: Code formatter formats braces in case and default statements, but no settings...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: 3.0 M9   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-02-20 13:24 EST by John P. A. Verhaeg CLA
Modified: 2004-05-18 10:26 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John P. A. Verhaeg CLA 2004-02-20 13:24:10 EST
The code formatter formats the following code:

    switch (key) {
        case VALUE1: {
            doCase1();
            break;
        }
        default: {
            doDefault();
        }
    }

as:

    switch (key) {
        case VALUE1:
            {
                doCase1();
                break;
            }
        default:
            {
                doDefault();
            }
    }

but no settings exist to control this behavior.  If or when these settings get 
included, I would like there to be some way to get the code formatted as I 
originally typed the statement at the top of this description.
Comment 1 Olivier Thomann CLA 2004-02-22 15:31:34 EST
What is your settings for brace position for a block?
Select end_of_line and not next_line_shifted?
Comment 2 John P. A. Verhaeg CLA 2004-02-22 22:50:33 EST
It's set to "Same line", not "Next line indented".
Comment 3 Olivier Thomann CLA 2004-02-23 08:51:06 EST
My mistake.
What happens is that you have "indent statements compare to case" set to true.
Then the block is indented and this follows the settings.
Comment 4 John P. A. Verhaeg CLA 2004-02-23 10:09:34 EST
It seems to me that you're using a different version of Eclipse than I am.  
The closest setting I can find that seems like what you're referring to 
is "Statements within 'case' body", found within the "Indent" group under 
the "Indentation" tab.  If I uncheck this, it does indeed fix the extra 
indentation within the curly braces.  However, it still doesn't fix the 
problem with the starting curly brace being moved to the next line.

Futhermore, this doesn't seem very intuitive, since the preview window's 
example code doesn't use curly braces after the case statement, and thus the 
user has to figure out that the indentation of curly braces will be handled 
like regular code.  In other words, what looked perfectly good for case 
statements without curly braces:

    case 0:
        Other.doFoo();
        break;

didn't lead me to believe adding curly braces would add yet another level of 
indentation.  Furthermore, after unchecking the aforementioned option, the 
preview window's code now looks unformatted even though the code now would 
look more correct with curly braces:

    case 0:
    Other.doFoo();
        break;
Comment 5 Olivier Thomann CLA 2004-02-23 11:59:55 EST
I will investigate.
Comment 6 Olivier Thomann CLA 2004-04-16 15:51:52 EDT
What do you expect in this case?
    switch (key) {
        case VALUE1: {
            doCase1();
        }
            break;
        default:
            doDefault();
    }
Do you expect the closing brace to be at the same indentation level than the
break; or you expect the break; and the doDefault(); to be at the same level
than the case and the default?
Please let me know asap as this might have an impact on the code formatter
constants (API will be frozen very soon).

Comment 7 John P. A. Verhaeg CLA 2004-04-16 16:05:12 EDT
I would expect the closing brace to be at the same level as the case, which 
mimics all the other control statements.  How the break and "doDefault" 
statements are formatted would be determined by the "Indentation" tab's 
already existing "'break' statements" and "Statements within 'case' body" 
options, respectively.

I personally would never put the break or "doDefault" statement outside of a 
block, but if I did, using my current settings I would expect the result to be 
exactly what you show in Comment #6.
Comment 8 Olivier Thomann CLA 2004-04-16 16:09:23 EDT
Ok so if the block opening brace is on the same line than the case, you always
expect the closing brace to be indented with the case?
If this is the case I simply need to add one option for that. I will then use
the same kind of positionning for the opening brace than I did at other
locations (method declarations, ...).
NEXT_LINE, NEXT_LINE_SHIFTED and END_OF_LINE.

Would this make it for you?
Comment 9 John P. A. Verhaeg CLA 2004-04-16 16:17:38 EDT
Yes, perfect.
Comment 10 Olivier Thomann CLA 2004-04-16 16:18:59 EDT
I forgot to mention that this would be done only for a block following the case.
So in this case:
switch(i) {
    case 1 :
        doCase1();
        {
             ....
        }
        break;
    case 2 : {
        ....
    }
    default :
        break;
}

the block between the doCase1(); and break; would be indented as other statements.
Do you agree with this?
Comment 11 John P. A. Verhaeg CLA 2004-04-16 16:20:18 EDT
Yup.
Comment 12 Olivier Thomann CLA 2004-04-19 16:54:31 EDT
The option FORMATTER_BRACE_POSITION_FOR_BLOCK_IN_CASE has been added.
Regression tests added.
By default the option is END_OF_LINE.
Fixed and released in HEAD.
Comment 13 David Audel CLA 2004-05-18 10:26:36 EDT
Verified for 3.0M9