Bug 544373 - [typing] Correct Indentation indents wrongly after ternary operator
Summary: [typing] Correct Indentation indents wrongly after ternary operator
Status: CLOSED DUPLICATE of bug 65463
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.10   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-02-12 07:30 EST by Heiko Selber CLA
Modified: 2019-02-12 10:14 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Heiko Selber CLA 2019-02-12 07:30:32 EST
When "Correct Indentation" is active as a save action, lines following a ternary operator (with line breaks) are indented one level too much (aligned with the ternary operator).

To reproduce:

Start Eclipse 4.10.0 with a new workspace.

Configure save action:

Open Window -> Preferences
Select Java -> Editor -> Save Actions

Tick "Perform the selected actions on save"
Tick "Additional actions"
Click "Configure..."
Tick "Correct Indentation"

Create a new Java project and add this class:

// begin class
public class TernaryFormatter {

	public int doSomething(String foo, String bar) {

		int retval = "".equals(bar)
				? foo.length()
				: foo.hashCode();

		System.out.println("Why am I indented?");

		return retval; 
	}
}
// end class

Edit the file (e.g. add one space somewhere).
Save the file.

Result: The line 'System.out.println("Why am I indented?");' and following are indented too far right, aligned with the question mark.
Comment 1 Heiko Selber CLA 2019-02-12 07:33:31 EST
A workaround is to wrap the entire ternary expression in parentheses:

        int retval = ("".equals(bar)
                ? foo.length()
                : foo.hashCode());
Comment 2 Dani Megert CLA 2019-02-12 10:14:02 EST

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