Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 77081 - [formatter] Code Formatter indents array initializer braces "incorrect"
Summary: [formatter] Code Formatter indents array initializer braces "incorrect"
Status: RESOLVED DUPLICATE of bug 303519
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P5 normal (vote)
Target Milestone: 4.5 M6   Edit
Assignee: Mateusz Matela CLA
QA Contact:
URL:
Whiteboard: To be verified for 4.6 M5
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-27 03:50 EDT by Nils Hammar CLA
Modified: 2016-01-23 10:44 EST (History)
3 users (show)

See Also:


Attachments
Current formatting rules as requested. (26.02 KB, application/octet-stream)
2005-08-31 12:50 EDT, Nils Hammar CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nils Hammar CLA 2004-10-27 03:50:10 EDT
The code formatter seems to align the braces with the '=' sign and not the left 
edge of the previous line.

Current formatting:
    private final static String[] defaultKeyStoresWindows =
                                                          {
        "C:\\Program Files\\Java\\jre1.5.0\\lib\\security\\cacerts",
        "C:\\Documents and Settings\\nils\\.keystore"
                                                          };

Expected Formatting:
    private final static String[] defaultKeyStoresWindows =
    {
        "C:\\Program Files\\Java\\jre1.5.0\\lib\\security\\cacerts",
        "C:\\Documents and Settings\\nils\\.keystore"
    };

Additionally, when an array has a single initializer the value is indented 
related to the braces, like the following example:

    private final static String[] defaultKeyStoresUnix    =
                                                          {
                                                              "/usr/java/jdk1.5.
0/jre/lib/security/cacerts"
                                                          };

It should be possible to select if the braces should be aligned with the '=' 
sign or to the left if '='-alignment is used.
Comment 1 Christof Marti CLA 2004-10-27 06:14:50 EDT
Moving to JDT/Core.
Comment 2 Philipe Mulet CLA 2005-04-07 09:26:17 EDT
Deferring post 3.1
Comment 3 Nils Hammar CLA 2005-07-29 12:52:21 EDT
Bug is still in 3.1, and is annoying. Please consider for 3.2!
Comment 4 Olivier Thomann CLA 2005-08-31 12:41:32 EDT
Could you please provide your code formatter settings?
Comment 5 Nils Hammar CLA 2005-08-31 12:50:33 EDT
Created attachment 26716 [details]
Current formatting rules as requested.
Comment 6 Olivier Thomann CLA 2005-08-31 13:44:38 EDT
Thanks for the info.
To clarify your problem, you expect to get the '{' and '}' at the same
indentation level than the private, because you set that you want the braces for
an array initializer to be on the next line.
You expect the expressions within the array initializer to be align on column
relative to the positions of the brace.
Am I right?
Thanks for your feedback.
Comment 7 Nils Hammar CLA 2005-09-01 01:29:10 EDT
It's correct that I want the braces to be left-aligned with the private keyword
and to have the data indented relative to the braces.

A secondary issue is that currently the data is indented differently if there is
a single value and if there are multiple values.
Comment 8 Olivier Thomann CLA 2005-09-01 09:40:10 EDT
The problems comes from a conflict between the type members alignment and the
array initializer alignment.
I am investigating.
Comment 9 Steve Ray CLA 2008-06-02 11:20:10 EDT
I have a similar problem with 3.4 rc4.  Is it correct if I say no new line after brace for array initializer to get a new line after the brace.  Currently in the example code I get :
   static int[] fArray    = {
                             1, 2, 3, 4, 5 };

I expect to get:

   static int[] fArray    = { 1, 2, 3, 4, 5 };
Comment 10 Frederic Fusier CLA 2008-08-18 08:06:43 EDT
Ownership has changed for the formatter, but I surely will not have enough time to fix your bug during the 3.5 development process, hence set its priority to P5.
Please provide a patch if you definitely need the bug to be fixed in this version and I'll have a look at it...
TIA
Comment 11 Ladar Levison CLA 2010-05-27 14:23:30 EDT
With array initializers, the formatting rules are not applied reliably to sub-structure initializers. For example:

typedef struct {
  int a;
  int b;
} CHILD_T;

typedef struct {
  int a;
  CHILD_T b;
} PARENT_T;

PARENT_T sample = {
  .a = 1,
  .b = {
    .a = 1,
    .b = 2,
  }
};
  
If you start out with the above format, you can get it to stay that way. But if the .b initializer is already on one line, it will stay that way even if the format rules dictate a new line should be inserted.
Comment 12 Frederic Fusier CLA 2010-05-28 03:09:52 EDT
(In reply to comment #11)

The code you're showing looks like C++, doesn't it? Hence, the Java formatter couldn't be involved in the problem you're describing in this comment. Please open a bug against the right product (I guess it should be CDT...)
Comment 13 Ladar Levison CLA 2010-05-28 14:56:11 EDT
My apologies, you are correct. It was my first Eclipse bug report and I was trying to avoid a duplicate.
Comment 14 Mateusz Matela CLA 2016-01-23 10:44:32 EST
This problem no longer occurs after the formatter redesign.

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