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

Bug 85401

Summary: [quick assist] join if sequence in if-else-if forces braces one me
Product: [Eclipse Project] JDT Reporter: Dani Megert <daniel_megert>
Component: UIAssignee: Martin Aeschlimann <martinae>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: Konstantin.Scheglov
Version: 3.1   
Target Milestone: 3.1 M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Dani Megert CLA 2005-02-16 08:42:18 EST
I20050215-2300

The join if sequence in if-else-if Quick Fix adds unwanted braces:
	if (a == 1)
		System.out.println("a == 1");
	if (a == 2)
		System.out.println("a == 2");

becomes:

	if (a == 1) {
		System.out.println("a == 1");
	} else if (a == 2) {
		System.out.println("a == 2");
	}

instead of:
	if (a == 1)
		System.out.println("a == 1");
	else if (a == 2)
		System.out.println("a == 2");
Comment 1 Martin Aeschlimann CLA 2005-02-16 09:26:39 EST
brackets are good! And I think they are even required in the case of
	if (a == 1)
		System.out.println("a == 1");
	else if (a == 2) {
		System.out.println("a == 2");
        } else
                System.out.println("a == 3");

To make sure that the else does not bind to the inner if.
Comment 2 Konstantin Scheglov CLA 2005-02-17 00:56:54 EST
  Hm... I don't know. In general yes, this is not very good, that quick assist
changes style of code, but I also think that brackets are good.
  
  Possible solution here is to keep existing style of code, where possible, i.e.
always, except case when 'then' statement is 'if' statement without 'else'. In
this case we have choice: convert all 'then' statements in block, or convert
only this one. Your decision?

  Plus, I think that I could add two more "deep search" (i.e. for selected part
of compilation unit) quick assists for converting all single statements in
control statements ('if', 'for', 'while') in blocks, and reverse - convert all
such blocks with single statement in such single statement. Something like
"Convert control statement bodies in blocks" and "Convert single-statement
blocks in single statement".
Comment 3 Dani Megert CLA 2005-02-17 02:13:03 EST
>  Hm... I don't know. In general yes, this is not very good, that quick assist
>changes style of code, but I also think that brackets are good.
OK, then assume, Quick Fix removes all your braces when applied - would you be
happy and use such Quick Fixes? ;-)
  
>  Possible solution here is to keep existing style of code, where possible, i.e.
>always, except case when 'then' statement is 'if' statement without 'else'. In
>this case we have choice: convert all 'then' statements in block, or convert
>only this one. Your decision?
Yes, sounds good.
Comment 4 Martin Aeschlimann CLA 2005-02-17 03:41:18 EST
-1 one for the deep search quick assists. I want to solve these 'code style
trasformation' in an own action. Other candidates would be

Replace all fully qualified types in code with imports
Qualify all field references with this.
Force naming conventions (field -> fFieled)
Introduce / remove brackets
...

Similar to code formatting but not just whitespace.
Help of course welcome. I'm not yet sure if I will have time for this new action.
Comment 5 Martin Aeschlimann CLA 2005-02-27 21:41:00 EST
fixed with patch in 82220 > 20050228
Comment 6 Dani Megert CLA 2005-03-07 05:31:42 EST
correcting target milestone