Question

The way eclipse formats array initializers is just horrible. I know you can adjust the way it formats in the preferences but it will always put a newline between the '=' and the "{ ... }'.

This looks very ugly to me, especially when you have an empty initializer.

Is there a way to further adjust these settings so it never puts a newline, Or perhaps that it just leaves array initializers alone and only puts the right amount of spaces between the commas and elements of the initializer?

As an example this is how I would want it:

string shortArray[15] = { };
string notVeryLong[] = { "Some words", "More words", "and more", "etc" };
string TooLongForOneLine[] = 
{
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc", 
    "Some words", "More words", "and more", "etc" 
};
Was it helpful?

Solution

I'm not sure which version of eclipse you are using but I will answer the question for Eclipse 3.7 since that is what I use.

  1. Open the "Windows" drop down menu and select preferences.
  2. Then navigate to "C++", then "Code Style".
  3. Select the "New..." button. Provide a name and select one of the built in profiles.
  4. Go to the "Braces" tab. At the bottom of the list there is a drop down for "Initializer list". Select "Same line" from the drop down and click the "OK" button to close all dialogs.

Note: In the same window area you will be able to customize braces for classes, functions, namespaces, if statements, switch statements, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top