By Craig P. Motlin


2009-05-20 23:53:55 8 Comments

Is there a way to get eclipse to stop erasing existing line breaks? If I have a method signature like this, I can't figure out how to get eclipse to leave it alone:

void foo(
    int arg1,
    int arg2,
    int arg3,
    int arg4)
{
    // ...
}

With various settings it will either collapse the arguments down to one line, or wrap them at some margin like this:

void foo(
    int arg1, int arg2,
    int arg3, int arg4)
{
    // ...
}

With "Wrap all elements, every element on a new line" it does preserve this whitespace, but it will ALWAYS wrap, which isn't what I want. I'd like eclipse to apply formatting for indentation and braces and such, just without ever deleting (or inserting) any line breaks.

4 comments

@Thorbjørn Ravn Andersen 2010-11-15 10:59:29

You can use comments to guide the formatter.

void foo( //
  int arg1, //
  int arg2, //
  int arg3, //
  int arg4)

will preserve the line breaks at the comments.

@VonC 2009-05-21 08:13:39

The formatting of arguments is an old subject, but the one new formatting feature introduced in 3.5 is:

  • eclipse3.5M4 "Formatter option to preserve user line breaks"

The Java code formatter can now optionally preserve user line breaks by not joining lines in code or comments.

For example, the wrapped lines of the return statement in following test case:

before
Example of Code to Format

will be preserved by the formatter when the "Never Join Lines" preference is used, and now produces the following result:

after
Coded Formatted with Never Join Lines

This preference can be configured on the Java > Code Style > Formatter preference page. See the Never join lines option on the Line Wrapping and Comments tab.

That may help, but otherwise there is not much new features on that front in 3.5.

@VonC 2013-06-03 13:56:30

@ThorbjørnRavnAndersen Thank you for mentioning it. Fortunately, those pictures were easy enough to find back (in ftp.ntua.gr/eclipse/eclipse/downloads/drops/R-3.5-2009061115‌​40/… for instance)

@Brian Headlee 2011-03-22 22:49:43

When I used the 'format' feature in Eclipse on my Java code, it tended split or break my lines of code in various places to make sure the content would always fit to a certain width. I found this to be less than optimal if you have a wide monitor. I worked around this issue by doing the following:

In eclipse 3.5, you can open preferences and search for "Format" or select Java->Code Style->Formatter from the left menu. The default profile is called "Eclipse [built-in]" and if you want to make changes to the Java Formatter you will need to copy it and create your own profile. This can be done with the "New" button right below the profile name.

Once you have created your own Java Formatter in eclipse, make sure it's selected and hit the edit button. There a lot of features in here you can customize, but to fix the wrapping issue I selected the "Line Wrapping" tab. Under general settings you will see "Maximum line width:". This numerical value is the max line length the formatter will work with. I bumped my up to a more reasonable size for larger monitors such as 180 characters. Save and apply that and return to your code.

When you next try to format a java file, the line wrapping should only happen if you are above the new max size.

Happy Coding!

@Craig P. Motlin 2011-03-23 22:27:39

I think that will cause Eclipse to combine a few short lines into one long line if they add up to less than 180 characters.

@infografnet 2013-03-27 21:34:47

well, also on tab Comments inside properties of Formatter, can be useful to change Maximum line width for comments to the same value as Maximum line width.

@Pushkar 2014-07-31 07:42:23

Thanks, Its help me to come out of the over headach

@tunaranch 2009-05-21 00:22:00

Eclipse 3.5 supports this. (It hit release candidate a a few days ago, so could be worth checking out)

Related Questions

Sponsored Content

1 Answered Questions

3 Answered Questions

0 Answered Questions

Eclipse formatter break on dot

4 Answered Questions

[SOLVED] How do I setup Eclipse to stop on the line an exception occurred?

5 Answered Questions

[SOLVED] Eclipse formatter settings for the Builder pattern

2 Answered Questions

2 Answered Questions

2 Answered Questions

How to prevent Eclipse from wrapping around sections of comments

1 Answered Questions

[SOLVED] Is there any way to get such type of line wrapping in Eclipse?

2 Answered Questions

[SOLVED] Eclipse line wrapping format not getting applied to code

  • 2010-10-29 17:10:11
  • Spangen
  • 3048 View
  • 3 Score
  • 2 Answer
  • Tags:   eclipse

Sponsored Content