13

I'd like for my lines, especially within comments, to be automatically managed so they don't get too long.

I remember once I had a configuration for vim which automatically moved the word I was typing to the next line once I reached 72 characters. It wasn't smart enough to rearrange the paragraph if I edit it, but it was a start.

Is there something that can manage these for me? I have a tendency to write really long comments in my code, and it helps to make them look neat by having consistent width, but it's always a pain to do this because oftentimes editing a sentence requires editing the entire rest of the paragraph.

I have just recently discovered the Ctrl+Shift+F feature. It is amazing and superior to Ctrl+I which is what I was using up till now, but I noticed that it does not do anything to clean up my comments.

Update: The answers are correct when working with Java in Eclipse. It seems like I have to wait for the CDT to incorporate this feature.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • 1
    possible duplicate of [Does Eclipse have line-wrap](http://stackoverflow.com/questions/2846002/does-eclipse-have-line-wrap). Based on his accepted answer, the OP there wanted hard wrap like you. – Ciro Santilli OurBigBook.com Jan 16 '15 at 10:09

3 Answers3

14

In "Windows -> Preferences", go to "Java -> Code style -> Formatter" to customize the formatter (called when you click Ctrl+Shift+F). In the tab "comment", you can set the maximum line width for comments (it can be different then the line width for code).

Tip: in the preferences, "Java -> Editor -> Save actions", you can make Eclipse to automatically format your file when you save it, so your code is always correctly indented !

apricot
  • 3,599
  • 3
  • 19
  • 20
Baldrick
  • 23,882
  • 6
  • 74
  • 79
0

If you want to format your header comment, you have to check Enable header comment formatting - that was the trick for me.

Obviously, to use this, you must create new formatter profile.

Line
  • 1,529
  • 3
  • 18
  • 42
0

The automatic formatting of Eclipse great no question.

If your comments are reformatted depends on what comment type and how you already have inserted line breaks.

Writing for example one very long line comment starting with // will be broken down by the formatter into multiple lines.

However you later edit the formatted lines - e.g. delete parts of it the formatter will leave them as they are. Only over-long lines will be changed.

Just in difference to block comments like this: /* comment */ Those comments will always be re-formatted in case the line is too short or too long.

Robert
  • 39,162
  • 17
  • 99
  • 152