Bug 548910 - Wrong indentation after 'else if ' with newline in condition
Summary: Wrong indentation after 'else if ' with newline in condition
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.12   Edit
Hardware: All Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-07-03 08:26 EDT by Bernhard Seebass CLA
Modified: 2023-03-21 11:30 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bernhard Seebass CLA 2019-07-03 08:26:47 EDT
When there is a newline in the condition of an 'else if' statement, the indentation below is wrong until the closing brace.

Example:

		if (false
				|| true) {
			// good case
		} else if (false
				|| true) {
					// bad case
				} // fails to align 


The "bad case" comment and the closing braces should be 4 tabs further left.


Tested with Eclipse
Version: 2019-06 (4.12.0)
Build id: 20190614-1200
Comment 1 Bernhard Seebass CLA 2019-09-25 05:35:34 EDT
I just upgraded to latest Eclipse:

Version: 2019-09 R (4.13.0)
Build id: 20190917-1200

The bug is still active.
Comment 2 Eclipse Genie CLA 2021-09-15 01:07:04 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.
Comment 3 Bernhard Seebass CLA 2021-09-15 04:32:53 EDT
Bug is still active in Eclipse 2021-06
Comment 4 Kalyan Prasad Tatavarthi CLA 2022-01-19 02:59:30 EST
I have not been able to reproduce this issue. Can you please check with the latest release and update.
Comment 5 Bernhard Seebass CLA 2022-01-19 04:13:14 EST
I tested with

Version: 2021-12 (4.22.0)
Build id: 20211202-1639

Automatic indentation is still wrong (using any formatter profile, e.g. "Eclipse [built-in]")


		if (false
				|| true) {
			// good case
		} else if (false
				|| true) {
					// bad case
				} // fails to align


Did you copy/paste above code and applied automatic formatting? What was the outcome?
Comment 6 Kalyan Prasad Tatavarthi CLA 2022-01-19 05:43:57 EST
(In reply to Bernhard Seebass from comment #5)
> I tested with
> 
> Version: 2021-12 (4.22.0)
> Build id: 20211202-1639
> 
> Automatic indentation is still wrong (using any formatter profile, e.g.
> "Eclipse [built-in]")
> 
> 
> 		if (false
> 				|| true) {
> 			// good case
> 		} else if (false
> 				|| true) {
> 					// bad case
> 				} // fails to align
> 
> 
> Did you copy/paste above code and applied automatic formatting? What was the
> outcome?

Steps that I followed : 

 '|' - indicates cursor position. 

Copied the below code into java editor

		if (false
				|| true) {
			// good case
		} else if (false
				|| true) {|

Now press enter at the cursor position , the code changes to 

		if (false
				|| true) {
			// good case
		} else if (false
				|| true) {
			|                
		}
Here the indentation, cursor position is correct for the new line as well as the closing brace.

If the above use case is not the case you are referring to, please provide detailed steps to reproduce the issue that you are facing.
Comment 7 Bernhard Seebass CLA 2022-01-19 06:08:57 EST
Thanks for replying and sorry for me having been unclear about what I meant with "automatic formatting".

Indentation is correct while typing. However, as soon as I save, the source is automatically formatted (due to my "Save Actions" settings) and then the indentation is wrong.

Of course, for testing, you can just apply automatic formatting manually by pressing CTRL+SHIFT+F
Comment 8 Kalyan Prasad Tatavarthi CLA 2022-01-24 04:02:56 EST
(In reply to Bernhard Seebass from comment #7)
> Thanks for replying and sorry for me having been unclear about what I meant
> with "automatic formatting".
> 
> Indentation is correct while typing. However, as soon as I save, the source
> is automatically formatted (due to my "Save Actions" settings) and then the
> indentation is wrong.
> 
> Of course, for testing, you can just apply automatic formatting manually by
> pressing CTRL+SHIFT+F

When I select the code and press CTRL+SHIFT+F , the allignment is as below

		if (false || true) {
			// good case
		} else if (false || true) {
			// bad case
		} // fails to align

Can you please provide the details of your "Save Actions" settings, that is what changes have you made different from the default settings.
Comment 9 Bernhard Seebass CLA 2022-01-24 11:50:54 EST
It seems I did something wrong, when I tested with the built in Formatter profile as the problem is in fact not reproducible using the built-in profile.


To reproduce the misalignment I copied "Eclipse [built-in]" Profile and then changed the following:

- New Lines / 'if else'
  X Keep 'else' statement on same line
- Line Wrapping
  X Never join already wrapped lines



After I have disabled the option "Keep 'else' statement on same line" in my formatter settings, formatting works as desired (with "Keep 'else if' on one line" enabled).
Comment 10 Hylke van der Schaaf CLA 2023-03-21 11:30:28 EDT
The key part is the "newline in condition". Do not let the formatter remove the newlines from the if condition.
Here is a longer example:

        if (someconditon()) {
            // do stuff
        } else if (testLongCallOne()
                && testLongCallTwo()
                && testLongCallThree()) {
                    // do another thing
                } else {
                    // something else
                }

Clearly the last four lines should not be indented using the continuation-indent settings.