ChatGPT解决这个技术问题 Extra ChatGPT

Eclipse: Set maximum line length for auto formatting?

I am working with Java. If I hit Ctrl+Shift+F in Eclipse Helios, it will auto format my code. At a certain point, it wraps lines. I would like to increase the maximum line length. How can I do this?

Note: starting Eclipse Mars 4.5 M1 (August 2014), the default line length in Eclipse is 120 (instead of 80). See my answer.

B
BeeOnRope

In preferences Java -> Code Style -> Formatter, edit the profile. Under the Line Wrapping tab is the primary option for line width (Maximum line width:). In the Comments tab you have a separate option Maximum line width for comments:, which will also need to be changed to affect comment wrapping.

You will need to make your own profile to make these changes in if you using one of the [Built-in] ones. Just click "New..." on the formatter preferences page.


This wasn't working for me so I disabled formatting of comment lines entirely. It's nearly exactly the same except instead of the 'Line Wrapping' tab you want 'Comments' tab and uncheck 'Enable line comment formatting'
This is the correct answer, althought it doesn't force the width on comments. Go to the "Comments" tab, and on the bottom there's the field "Line width" to change the maximum line width for comments.
Note: Eclipse (Kepler, at least) very obnoxiously allows you to edit the built-in profile to your heart's content, only to stop you from saving it with an unobtrusive notification that you CAN'T edit built-in profiles, and must create a copy and edit that instead.
"Line Wrapping tab" is not a complete answer. As Henrique Sousa and some other IDs mentioned, it still need to go to "Comments" tab. I was bugged by line-wrapping in comments. :)
Using Luna at least, all you have to do is change the text field at the top of the options tabs to something other than Eclipse [Default] and click Apply. It saved all my modifications I was trying to make to the default profile.
S
Sumit Singh

Take a look of following image:

Java->Code style->Formatter-> Edit

https://i.stack.imgur.com/dIG6J.png


You forgot steps showing how to get to the preferences window =) Window > Preferences
@rodrigo-silveira yeah you are write, but i think people who wants to change line width will definitely know how to open preferences window. By the way thanks for adding it.
@rodrigo-silveira That's platform dependent, actually. On a Mac it's Eclipse -> Preferences.
@ArtOfWarfare Thanks for adding comment.
You forgot the part where you have to create a new "Active profile". Otherwise you will not be able to change any settings.
S
Sumit Singh

Comments have their own line length setting at the bottom of the setting page java->code style->formatter-> Edit... ->comments


S
Sumit Singh

for XML line width, update preferences > XML > XML Files > Editor > Line width


R
ROMANIA_engineer

For HTML / PHP / JSP / JSPF: Web -> HTML Files -> Editor -> Line width


S
Sumit Singh

Click Project->preferences. Type format into the search - you should see java->code style->formatter. Click that, then edit - finally, the line wrapping tab - its there :)


P
PhantomReference

For XHTML files: Web -> HTML Files -> Editor -> Line width


R
Roo

Preferences -> Java -> Code Style -> Formatter / "Edit..."

Tabs: "Line wrapping" , "Comments"

Field: "Maximum line width"

Set both to make comments have the same length too. You might need to create a new profile if you don't have one already.


m
mannedear

I use the Eclipse version called Mars which works with Java 7.

Go to Preferences -> Java -> Code Style -> Formatter

Click on the Edit Button shown in the right side of "Active Profile" drop down

Tabs: "Line wrapping"

Field: "Maximum line width", Set the desired value (Default value set to 120) to increase/decrease the line length in the editor

Note: Remember to rename the Active profile to the name of your choice, as the default Eclipse profile won't accept your changes.


h
hariprasad

Line length formatter setup is blocked for annotations (Eclipse Photon checked). Therefore it is needed in Line Wrapping -> Wrapping settings -> Annotations. Setup line wrapping as appropriate for you. There is couple of possibilities, e.q. Enable wrap when necessary to use first icon list.

https://i.stack.imgur.com/4v5Ra.png