Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 124622

Summary: [formatter] Comments in switch-case wrong aligned in some cases
Product: [Eclipse Project] JDT Reporter: Benjamin Pasero <bpasero>
Component: CoreAssignee: Mateusz Matela <mateusz.matela>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P5 CC: daniel_megert, mateusz.matela, parker.david.a
Version: 3.2   
Target Milestone: 4.5   
Hardware: PC   
OS: Windows 2000   
Whiteboard:
Attachments:
Description Flags
Proposed fix
none
Regression tests none

Description Benjamin Pasero CLA 2006-01-20 05:48:24 EST
When I run code-format on these two switch-case conditions, I get a different result for the alignment of comments, though I expect the same result. And I think in 3.1 I did get the same result:

switch (0) {

  /** My Comment */
  case 0:
    break;

  /** My Comment */
  case 1:
    break;
}

and

switch (0) {

  /** My Comment */
  case 0:
    return; //Changed to return here!

    /** My Comment */
  case 1:
    break;
}

The second comment is not correctly formatted. I think the formatter is not handling a return like a break, though it should I guess. Same for the keywork "continue" btw.

Ben
Comment 1 Olivier Thomann CLA 2006-01-25 15:37:51 EST
Indeed the formatter is not doing a specific treatment for return or continue statements.
Comment 2 Olivier Thomann CLA 2007-02-19 11:57:14 EST
The second "/** My Comment */" should always have the same identation of the case statement regardless of the fact that the previous statement is a break, a return or anything else.
Comment 3 Olivier Thomann CLA 2007-02-19 12:04:15 EST
In fact we do have a problem here.
I am not sure I can make the assumption that the comment should always be indented like a case statement.
For cases like:
		switch (i) {
			case 0:
			// handling 0
				System.out.println(\"Case was 0\");
			// fall through
			case 1:
			// handling 1.
			// some additional remark (also handles 0).
				System.out.println(\"Case was 0 or 1\");
				break;
			case 2:
			// fall through
			default:
			// some default action\n" + 
				System.out.println(\"Case was :\" + i);
		}

How can we decide how the diffent comments should be indented compare to the other statements around them?

Daniel,

If you have any suggestions, they are welcome.
Comment 4 Olivier Thomann CLA 2007-02-19 12:14:43 EST
So I think the question is more.
Should a comment be considered as a statement in term of indentation?
This would mean that a comment would end up being indented compare to the case statements based on the preferences "Statement within switch body" or "Statement within case body" according to their positions relative to the cases inside the switch statements.
So your given test case would be:
switch (0) {

  /** My Comment */
  case 0:
    break;

    /** My Comment */
  case 1:
    break;
}
or:
switch (0) {

  /** My Comment */
  case 0:
    return;

    /** My Comment */
  case 1:
    break;
}
since this is still part of the "case" body. The first comment is not considered to be part of a case body, so it is indented as a switch body.

And the second example:
switch (i) {
    case 0:
        // handling 0
        System.out.println(\"Case was 0\");
        // fall through
    case 1:
        // handling 1.
        // some additional remark (also handles 0).
        System.out.println(\"Case was 0 or 1\");
        break;
    case 2:
        // fall through
    default:
        // some default action\n" + 
        System.out.println(\"Case was :\" + i);
}

Would this make sense ?

Then we would end up with a consistent indentation in both cases.
Comment 5 Olivier Thomann CLA 2007-02-19 12:33:34 EST
Created attachment 59285 [details]
Proposed fix
Comment 6 Olivier Thomann CLA 2007-02-19 12:33:47 EST
Created attachment 59286 [details]
Regression tests
Comment 7 Olivier Thomann CLA 2007-02-19 12:34:22 EST
The patch implements the behavior described in comment 4.
Comment 8 Benjamin Pasero CLA 2007-02-19 14:20:40 EST
Right, I can see the problem now, thanks for the clarification. I agree that comments should be treated in a consistent way, though I am not very happy with the visual result (but I think i can live with it :-) ).

Ben
Comment 9 Olivier Thomann CLA 2007-02-19 14:25:31 EST
If you have a better proposal, please let me know.
For me as long as the treatment is consistent, it is good enough.
Comment 10 Benjamin Pasero CLA 2007-02-19 15:42:16 EST
Well, I can see the problem for the case of a fall-through, but wouldn't it be possible to detect a return-statement (the same way a break statement is detected) and consider the following comment to be part of the following switch-block?
Comment 11 Olivier Thomann CLA 2007-02-19 23:32:13 EST
Sure I could do that. But then we would need to rephrase the "break statement" indentation option to cover return, throw and continue statements as well.

Daniel,

Any thoughts on this?
Comment 12 Dani Megert CLA 2007-02-20 02:50:06 EST
Comment handling is always fuzzy and depending on where we do it we have special rules (e.g. move refactoring). I think a natural solution to treat those comments is:

1. if below or above the comments are 0..n empty line(s) then align the 
   comments to the statement that is not separated by empty lines (that would 
   handle comment 0)

2. apply the strategy from comment 4
Comment 13 Olivier Thomann CLA 2007-02-20 11:34:31 EST
(In reply to comment #12)
> 1. if below or above the comments are 0..n empty line(s) then align the 
>    comments to the statement that is not separated by empty lines (that would 
>    handle comment 0)
To be honest, I don't like this one too much. Empty lines might be removed by the code formatter. So basing a behavior on empty lines looks awkwards.

> 2. apply the strategy from comment 4
I'll go with this. At least the issue in comment 0 would be fixed and the second comment would be indented the same way in both cases.

There is no perfect way to format comments.
Comment 14 Dani Megert CLA 2007-02-20 11:40:34 EST
>So basing a behavior on empty lines looks awkwards.
Maybe for you as a coder but from a user point of view it would be natural to do this.
Comment 15 Olivier Thomann CLA 2007-02-21 14:09:26 EST
So what about lines breaks in the middle of the case body ?
Comment 16 Dani Megert CLA 2007-02-22 02:02:04 EST
>So what about lines breaks in the middle of the case body ?
What do you mean with "in the middle"?
Comment 17 Frederic Fusier CLA 2008-08-18 08:01:37 EDT
Ownership has changed for the formatter, but I surely will not have enough time to fix your bug during the 3.5 development process, hence set its priority to P5.
Please finalize the posted patch if you definitely need the bug to be fixed in this version and I'll have a look at it...
TIA
Comment 18 Dani Megert CLA 2014-06-13 06:49:32 EDT
*** Bug 437223 has been marked as a duplicate of this bug. ***
Comment 19 David Parker CLA 2014-06-13 09:47:49 EDT
My bug report for Eclipse 4.3.2 (bug 437223) was marked as a duplicate of this bug from Eclipse 3.5.  The last comment on this bug was in 2008.  Has this really been unfixed for 6 years, across several new releases?  Can this please be fixed soon?
Comment 20 Mateusz Matela CLA 2016-01-08 18:42:33 EST

*** This bug has been marked as a duplicate of bug 463590 ***