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

Bug 400670

Summary: [Save actions] Correct indentation conflicts with formatter causing staircase effect
Product: [Eclipse Project] JDT Reporter: Steven Post <redalert.commander>
Component: TextAssignee: Noopur Gupta <noopur_gupta>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, marcel.bruch, noopur_gupta, redalert.commander
Version: 3.7Flags: daniel_megert: review+
Target Milestone: 4.5 M7   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 463596    
Bug Blocks:    
Attachments:
Description Flags
formatter settings to reproduce
none
Fix + tests
none
Patch - to prevent toggle on save when formatter is applied on all lines
none
Fix + tests
none
Fix + tests
none
Fix + tests none

Description Steven Post CLA 2013-02-13 06:18:05 EST
Created attachment 226996 [details]
formatter settings to reproduce

Hi,

Version: Juno Service Release 1
Build id: 20121004-1855

I noticed that the Save Action 'correct indentation' conflicts with the formatter 'line wrapping'.
I attached my formatter configuration as reference.

The issue can be seen when using arguments with enums.
The formatter uses indentation so that all types are indented on the same column, as well as the arguments for the enums and arguments to those arguments (think arrays).

Desired result (which the formatter does correctly):
<pre>
public enum TestEnum {

	FIRST_ENUM("first type",
	           new SomeClass(),
	           new OtherEnumType[] { OtherEnumType.FOO}),

	SECOND_ENUM("second type",
	            new SomeClassOtherClass(),
	            new OtherEnumType[] { OtherEnumType.BAR}),

	THIRD_ENUM("third type",
	           new SomeThirdClass(),
	           new OtherEnumType[] { OtherEnumType.BAZ}),

	FOURTH_ENUM("fourth type",
	            new YetAnotherClass(),
	            new OtherEnumType[] { OtherEnumType.FOOBAR,
	                                  OtherEnumType.FOO,
	                                  OtherEnumType.FOOBARBAZ,
	                                  OtherEnumType.LONGERFOOBARBAZ,
	                                  OtherEnumType.REALLYLONGFOOBARBAZ,
	                                  OtherEnumType.MORELETTERSINHERE});

/* data members and methods go here */

}
</pre>

However when using the save action 'correct indentation', the 'SECOND_ENUM' start on the column of the arguments for 'FIRST_ENUM', the 'THIRD_ENUM' start on the column for arguments to the second one, and so on.
This creates the dreaded staircase effect.
Comment 1 Dani Megert CLA 2015-02-19 05:45:11 EST
Noopur, can you check whether your fix also covers this issue?
Comment 2 Noopur Gupta CLA 2015-02-19 08:43:40 EST
(In reply to Dani Megert from comment #1)
> Noopur, can you check whether your fix also covers this issue?
This is a different case, not covered in the fix for bug 439582.
Even this example will result in toggle on save when save actions are applied.
Comment 3 Noopur Gupta CLA 2015-03-25 08:23:05 EDT
Created attachment 251894 [details]
Fix + tests

Attached patch fixes this issue. Also, fixed the issue where 'correct indentation' was not taking the "Indent on column" policy for enum constants into account. This patch fix bug 458763 also for enum.
Added new tests and all existing tests are green. Dani, please review.
Comment 4 Noopur Gupta CLA 2015-03-25 08:34:30 EDT
Created attachment 251896 [details]
Patch - to prevent toggle on save when formatter is applied on all lines

Attached patch adds a check so that 'correct indentation' is applied when either the formatter is not active in save actions or when the formatter is applied only on edited lines. It will prevent the toggle on save issue in cases where the formatter is active on all lines in save actions.
Dani, please have a look.
Comment 5 Noopur Gupta CLA 2015-03-30 11:29:17 EDT
Created attachment 252004 [details]
Fix + tests

(In reply to Noopur Gupta from comment #3)
> Created attachment 251894 [details] [diff]
> Fix + tests
> 
> Attached patch fixes this issue. Also, fixed the issue where 'correct
> indentation' was not taking the "Indent on column" policy for enum constants
> into account. This patch fix bug 458763 also for enum.
> Added new tests and all existing tests are green. Dani, please review.

Updated patch to remove newline at end of file from test file of bug 458763.
Comment 6 Noopur Gupta CLA 2015-03-31 05:08:44 EDT
(In reply to Noopur Gupta from comment #5)
> Created attachment 252004 [details] [diff]
> Fix + tests

> Updated patch to remove newline at end of file from test file of bug 458763.
This test (IndentActionTest#testBug458763) is green when IndentActionTest is run separately. Also, the test case behaves properly when checked with the example in an editor. However, on running the JdtTextTestSuite, it fails. I am looking into the issue.
Comment 7 Noopur Gupta CLA 2015-03-31 07:16:27 EDT
Created attachment 252035 [details]
Fix + tests

(In reply to Noopur Gupta from comment #6)
> This test (IndentActionTest#testBug458763) is green when IndentActionTest is
> run separately. Also, the test case behaves properly when checked with the
> example in an editor. However, on running the JdtTextTestSuite, it fails. I
> am looking into the issue.

It happens as BreakContinueTargetFinderTest in JdtTextTestSuite uses ProjectTestSetup.setUp() that changes the default value of DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH to 'true'. 

Modified the test to set the above indent value to 'false' for this test.
Comment 8 Dani Megert CLA 2015-03-31 09:14:36 EDT
(In reply to Noopur Gupta from comment #7)
> Created attachment 252035 [details] [diff]
> Fix + tests


+		} finally {
+			project.setOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, value);
+		}

This is using the wrong constant.


The patch goes into the right direction but it indents too much using the example in comment 0.
Comment 9 Noopur Gupta CLA 2015-03-31 10:47:51 EDT
(In reply to Dani Megert from comment #8)
> (In reply to Noopur Gupta from comment #7)
> > Created attachment 252035 [details] [diff] [diff]
> > Fix + tests
> 
> 
> +		} finally {
> +		
> project.setOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER,
> value);
> +		}
> 
> This is using the wrong constant.
This should be corrected in the next patch.


> The patch goes into the right direction but it indents too much using the
> example in comment 0.

This patch was implemented based on the current formatter behavior and expected changes in the formatter based on the comments in bug 458208.
I have opened bug 463596 to discuss the correct behavior of formatter. This patch will be updated based on the result to follow the formatter.
Comment 10 Noopur Gupta CLA 2015-04-22 10:46:38 EDT
Created attachment 252631 [details]
Fix + tests

Updated patch based on bug 463596 comment #1.
Dani, please have a look.
Comment 11 Dani Megert CLA 2015-04-24 07:07:26 EDT
(In reply to Noopur Gupta from comment #10)
> Created attachment 252631 [details] [diff]
> Fix + tests
> 
> Updated patch based on bug 463596 comment #1.
> Dani, please have a look.

Looks good. Committed with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=40eb8fc501f8fa2424809ab7a59ea58807fc5787


I suggest we keep the bug open since the staircase issue can still occur until the formatter has been adjusted.
Comment 13 Noopur Gupta CLA 2015-05-12 05:14:43 EDT
After the fix, correct indentation works as expected as per the examples in bug 463596 and bug 463596 comment #1. Hence, closing this bug.