Bug 16231 - formatter creates ugly array initializer expressions
Summary: formatter creates ugly array initializer expressions
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 24428 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-05-17 07:25 EDT by Stefan Matthias Aust CLA
Modified: 2003-10-14 09:23 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 Stefan Matthias Aust CLA 2002-05-17 07:25:17 EDT
The formatter doesn't do a good job on formatting array expressions. Instead of

 static String[] names =
         { "Afk kdhf ", "b fhs sf jhfgd", ... };

I'd prefer 

 static String[] names = {
   "ahsjahsa", ....
 };

with just one indent (not two as the formatter does) and the { in the same 
line as the = and the } dedented again.  For people who prefer the style

 while ()
 {
   ....
 }

the { might be placed differently, but never as the formatter does now.

If you add even more strings to the array, the formatter uses

    static String[] names =
        {
            "Afk kdhf ",
            "b fhs sf jhfgd",
            "ceh gfskg fgfds",
            "hhfkdshk",
            "fhfjkdhlkfdl",
            "hhfkdshk",
            "fhfjkdhlkfdl" };

which should be

 static String[] names = {
   "fjdsfjl",
   ....
 };

Here's a more complex expression (long strings skipped):

 protected static IPropertyDescriptor[] descriptors =
        {
            new PropertyDescriptor(
                ID_SIZE,
                OdinMessages.getString("...")),
            new PropertyDescriptor(
                ID_LOCATION,
                OdinMessages.getString("..."))};

This again should IMHO be 

 protected static IPropertyDescriptor[] descriptors = {
   new ....,
   new ....
 };

Basically, the open { should stay in the line before, the content should be 
indented by one not two levels and the } should be in at the same level than 
the expression itself.
Comment 1 Philipe Mulet CLA 2002-05-19 11:45:12 EDT
Will reconsider post 2.0.
Comment 2 Philipe Mulet CLA 2002-07-25 06:30:32 EDT
Resurrecting formatter issues in 2.1 stream.
Comment 3 Philipe Mulet CLA 2002-07-25 06:32:29 EDT
Clearing resolution
Comment 4 Colin Sharples CLA 2002-10-14 00:13:12 EDT
It gets even worse with two dimensional arrays:

	private static final Object[][] testData =
		{ { "3/4/2002", "Foo"}, {
			"2/4/2002", "Bar"
		}, {
			"1/4/2002", "Qux"
		}
	};

Yuk! 
My preference is for:

	private static final Object[][] testData = { 
		{ "3/4/2002", "Foo" },
		{ "2/4/2002", "Bar" },
		{ "1/4/2002", "Qux" }
	};
Comment 5 Olivier Thomann CLA 2002-10-15 07:41:18 EDT
We are investigating a brand new formatter to fix that kind of defects. Be sure 
that we will have tests to cover this area.
Comment 6 Olivier Thomann CLA 2002-11-05 17:02:28 EST
*** Bug 24428 has been marked as a duplicate of this bug. ***
Comment 7 Olivier Thomann CLA 2003-01-15 13:23:00 EST
We need to clear 2.1 bug reports that won't be addressed before 2.1. The new 
implementation is still in the works. Therefore we cannot include it for 2.1. 
Not enough testing and we need to polish the preferences. This will be address 
for 2.2 as stated in the JDT/Core plan.
Comment 8 Philipe Mulet CLA 2003-06-12 06:35:44 EDT
Resurrecting for 3.0
Comment 9 Olivier Thomann CLA 2003-06-12 15:58:59 EDT
Reopen for 3.0 consideration.
Comment 10 Olivier Thomann CLA 2003-10-03 12:05:40 EDT
This is improved with the new code formatter.
Please reopen if the changes are not good enough.
Fixed and released in HEAD.
Comment 11 David Audel CLA 2003-10-14 07:35:53 EDT
Verified.
Comment 12 Stefan Matthias Aust CLA 2003-10-14 07:47:11 EDT
I just tried the 2nd example with 3.0M4 and now, it emits

 static String[] names = {"Afk kdhf ", "b fhs sf jhfgd", ...
         "fhfjkdhlkfdl", "hhfkdshk", "fhfjkdhlkfdl"};

It looks like it tries to fill the lines and will continue the elements in the
next line, indented with 2 tabs. I'd still prefer this:

 static string[] names = {
  <1tab>"hkjfshdjkfd", ...
 };

but the new formatter is definitely an improvement.

Unfortunately, the formatter still doesn't honor the "insert a new line before
an opening brace" setting for these kind of array literals (or it's just a
problem related to the non-yet-implemented options page).
Comment 13 Olivier Thomann CLA 2003-10-14 09:23:55 EDT
I will open a new bug report for that. See bug 44813.
I open bug 44815 for the size of the continuation indent.