Community
Participate
Working Groups
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.
What is your settings for brace position for a block? Select end_of_line and not next_line_shifted?
It's set to "Same line", not "Next line indented".
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.
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;
I will investigate.
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).
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.
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?
Yes, perfect.
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?
Yup.
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.
Verified for 3.0M9