Bug 144449 - [formatter] switch statement auto format fails
Summary: [formatter] switch statement auto format fails
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P5 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2006-05-30 10:21 EDT by Randy Hudson CLA
Modified: 2019-09-06 17:56 EDT (History)
1 user (show)

See Also:


Attachments
format prefs (26.57 KB, text/plain)
2006-05-30 11:57 EDT, Randy Hudson CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Randy Hudson CLA 2006-05-30 10:21:23 EDT
Formatting the following:
	switch (key.intValue()) {
	case BUTTON:
		return SketchFactory.eINSTANCE.createButton();
	case HYPERLINK:
		return SketchFactory.eINSTANCE.createHyperlink();
	default:
		break;
	}

results in:

		switch (key.intValue()) {
	case BUTTON:
		return SketchFactory.eINSTANCE.createButton();
	case HYPERLINK:
		return SketchFactory.eINSTANCE.createHyperlink();
	default:
		break;
	}
Comment 1 Dani Megert CLA 2006-05-30 10:26:56 EDT
This works for me using 3.2 RC6.
Comment 2 Randy Hudson CLA 2006-05-30 11:57:11 EDT
I'm not on M6 yet, but unless a bug was fixed since M4 this should still be reproducible. I'll attach my formatter prefs.
Comment 3 Randy Hudson CLA 2006-05-30 11:57:49 EDT
Created attachment 42976 [details]
format prefs
Comment 4 Dani Megert CLA 2006-05-30 12:07:55 EDT
Can reproduce using 3.2 RC6 with your preferences.
Comment 5 Olivier Thomann CLA 2006-05-30 12:14:14 EDT
I'll investigate for 3.2.1 or 3.3. Too late for 3.2 anyway.
Comment 6 Olivier Thomann CLA 2006-05-30 14:53:01 EDT
When you say it is a regression, a regression compare to what version ?
Comment 7 Olivier Thomann CLA 2006-05-30 15:08:51 EDT
What is your indentation level when you format?
Comment 8 Olivier Thomann CLA 2006-05-30 15:13:44 EDT
I tried it and it worked fine.
What did you select to format?
I tried using the formatter directly using the K_STATEMENTS kind.
So please attach your test case.
I also tried from the java editor and I could not reproduce.
Comment 9 Randy Hudson CLA 2006-05-30 16:38:33 EDT
It is one more than I posted. I removed a tab from each line. I'm inside a method whose signature has a single TAB in front of it.
Comment 10 Olivier Thomann CLA 2006-05-30 21:17:33 EDT
No idea how you got it.
Please attach the compilation unit you formatted. This could help to reproduce the problem.
Comment 11 Randy Hudson CLA 2006-05-30 23:13:36 EDT
(In reply to comment #6)
> When you say it is a regression, a regression compare to what version ?

This is a regression because every release I have indentation issues, probably different bugs, but usually related to switch or simple if statements. I'm sure your do some testing, so it must be specific to my settings. I would guess that the class members not being indented is causing the problem. See bug 75488, and bug 74958.
Comment 12 Dani Megert CLA 2006-05-31 03:38:22 EDT
>I tried it and it worked fine.
Did you really use the attached formatter preferences? Out of the box I couldn't reproduce either but with the attached prefs I was able to reproduce by simply copying the code from comment 0 into an existing method.
Comment 13 Olivier Thomann CLA 2006-05-31 11:48:57 EDT
Yes, I imported the attached prefs. I'll keep trying to reproduce it.
Comment 14 Olivier Thomann CLA 2006-07-19 10:59:50 EDT
Can you reproduce on 3.2?
I tried again by creating a new project and importing the preferences you attached without better luck.
Closing as REMIND.
Please reopen with a complete unit as the test case if you can still reproduce it.
Comment 15 Randy Hudson CLA 2006-07-19 13:38:11 EDT
reproduced on R3.2.

public class Main {

	public static void main(String[] args) {
		System.out.println("foo");
		switch ((int) System.currentTimeMillis()) {
		case 10:
			return;
		case 20:
			return;
		default:
			break;
		}
	}

}

Notice that the member (method main) is indented, although my preferences are to not indent methods.
Comment 16 Olivier Thomann CLA 2006-07-19 13:48:41 EDT
In order to reproduce it, I have to select only the switch statements.
Daniel,
this is related to the indentation of the first line when selecting a statement. Nothing has changed from the code formatter stand point for more than a year. This is not related to your settings.
This is a known issue and I really don't see how this can be fixed.
If you format the unit, the indentation is right.
Comment 17 Randy Hudson CLA 2006-07-19 14:10:27 EDT
The selected code can be anything and doesn't have to be a switch statement. the first line is left alone, while the other lines are formatted as if the entire file had been formatted. So, it does not respect the context's indentation level, and it leaves the first line alone. Both of these seem like problems, probably reported previously.
Comment 18 Olivier Thomann CLA 2006-07-19 14:14:55 EDT
Daniel,
I get the identation level from the UI. Do you see a way to improve this?

Randy,
Beside the first line not being indented, I believe the other lines are formatted as expected.
Comment 19 Randy Hudson CLA 2006-07-19 14:37:44 EDT
I disagree. The code should be indented relative to the indentation level of the parent element's indentation. In comment 15, the user chose to indent the main method even though the formatter preferences specify to not indent class members.  If I indent the method 5 TABs, the method's body should be relative to that (+5).

This occurs in code because formatting prefs don't support all indentation styles (such as top level classes should not have members indented. Nested classes should have members indented).  Isn't the whole purpose of selective formatting to support cases where formatting the entire document would not satisfy the user?
Comment 20 Dani Megert CLA 2006-07-20 04:15:50 EDT
>I disagree. The code should be indented relative to the indentation level of
>the parent element's indentation.

Agreed. There are two bugs here:

1. when fully (i.e. full line selections) selecting the switch statement (or an 'if' or...) the first line is currently not correctly formatted (i.e. indented) but it should. JDT Text gives JDT Core the correct selection i.e. all lines starting at the beginning of the line and the full source code. JDT Core should  correctly format that block in respect to the surrounding code.

2. JDT Text should expand the start and end selection if there are just whitespaces before and afterwards and pass this to the formatter. This of course depends on 1. being fixed in JDT Core land.
Comment 21 Frederic Fusier CLA 2008-08-18 08:07:20 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 provide a patch if you definitely need the bug to be fixed in this version and I'll have a look at it...
TIA
Comment 22 Eclipse Genie CLA 2019-09-06 17:56:00 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.