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

Bug 349008

Summary: [1.7] Ugly formatting for try with resources
Product: [Eclipse Project] JDT Reporter: Deepak Azad <deepakazad>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, daniel_megert, jarthana, markus.kell.r, satyam.kandula
Version: 3.7   
Target Milestone: 3.7.1   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
proposed fix v1.0 + regression tests none

Description Deepak Azad CLA 2011-06-10 06:15:19 EDT
With the default formatter settings format the following code.
----------------------------------------------------------------------------
	void foo() throws Throwable {
		try (FileReader reader1 = new FileReader("file1");
				FileReader reader2 = new FileReader("file2");
						FileReader reader3 = new FileReader("file3");
								FileReader reader4 = new FileReader("file4")) {
							int ch;
							while ((ch = reader1.read()) != -1) {
								System.out.println(ch);
							}
						}
					}

----------------------------------------------------------------------------

2 things
- Closing { of the try block should be aligned with 'try'
- Indentation of reader3 and reader 4 should probably be same as reader 2

Indenter (Ctrl+I) is a bit better than the formatter in this case.
Comment 1 Ayushman Jain CLA 2011-06-10 06:46:22 EDT
(In reply to comment #0)
> [..]
> 2 things
> - Closing { of the try block should be aligned with 'try'
Yes.
> - Indentation of reader3 and reader 4 should probably be same as reader 2
Not really. Try a method declaration with each parameter defined in a new line.

Will investigate.
Comment 2 Deepak Azad CLA 2011-06-10 07:17:25 EDT
(In reply to comment #1)
> > - Indentation of reader3 and reader 4 should probably be same as reader 2
> Not really. Try a method declaration with each parameter defined in a new line.
Actually, the 'Line wrapping' tab in the formatter preferences defines the different ways a parameters in a method declaration should be wrapped. We should define similar settings for try with resources. 

Even then we will need to choose a default line wrapping policy, and I think it should correspond to 'Indentation of reader3 and reader 4 should probably be same as reader 2'.
Comment 3 Ayushman Jain CLA 2011-06-14 15:49:21 EDT
Created attachment 197976 [details]
proposed fix v1.0 + regression tests

This fix corrects the indentation issue. Resources are indented such that r3 and r4 have same indentation as r2, and the try block and following brackets are also correctly indented.

I will open a separate bug for line wrapping options
Comment 4 Ayushman Jain CLA 2011-06-15 01:23:51 EDT
Released in BETA_JAVA7 branch.

Opened bug 349396 for a new line wrapping option
Comment 5 Jay Arthanareeswaran CLA 2011-06-27 06:23:36 EDT
Verified