1

A colleague's Eclipse formatting rules cause extreme indentation, and wraps comments to look like this:

                                        // this
                                        // is
                                        // indented
                                        // several
                                        // tabs

They're wrapped at every word boundary because they're indented all the way past the line wrap width. I can reformat them to look only slightly more sane:

// this
// is
// indented
// several
// tabs

but the wrapping remains. Is there any way I can automagically undo this wrapping so that I won't have to spend 30 minutes every time I commit to manually reformat these comments and make the comments readable again? I don't care if other line breaks are not preserved; that would be a reasonable trade. Target result:

// this is indented several tabs
Travis Well
  • 947
  • 10
  • 32
  • I know there is a shortcut to format code, does it do the same for comments? You could also write a program to read a file and rewrite it without the white space if it is commented. – Riley Carney Feb 11 '16 at 22:22
  • @RileyCarney the shortcut is command-shift-f on Mac or control-shift-f on Windblows/Linux. If no one gives a better answer, then yes, I'm going to have to pull out my regular expressions (now I've got two problems) and write a script to detect and correct these. – Travis Well Feb 11 '16 at 22:25
  • Alrighty, make sure to send your colleague some badly formatted code next time. Just kidding. – Riley Carney Feb 11 '16 at 22:29

3 Answers3

1

You are not the first to be frustrated by this (me included) I have never found an answer on how to undo this kind of formatting in a great way.

To prevent it happening again, there is of course the option of fixing the formatting rules and then applying them to the project instead of to the workspace. That ensures that if your colleague does format it won't be ruined like this. I recommend setting (in Comments tab of formatter):

  • turn on Never indent line comments on first column --> this prevents commented out code from being indented and lost
  • turn off Enable line comment formatting --> this fixes the wrapping problem

Take those settings, followed by using block /* */ comments for actual block comments (instead of what I often see of using line // comments for block comments).

Some other SO users who have posted similar questions with no full resolution, but some suggestions that may help. Such as using a third party formatter (perhaps only once to recover your code state and then continuing as above?)

Community
  • 1
  • 1
Jonah Graham
  • 7,890
  • 23
  • 55
  • Joining lines produces the unfortunate result: // this // is // indented... which I think is even less readable. But I can combine that with a regex find/replace... will edit your answer. This is good. – Travis Well Feb 11 '16 at 23:00
0

Try going to Windows -> Preferences -> Java -> Code Style -> Formatter and then click the Edit... button towards the top right under the Active Profile header.

Mess around with the settings in there, specifically the Line Wrapping and Comments tab.

jlars62
  • 7,183
  • 7
  • 39
  • 60
0

I don't think this will be possible with standard IDEs; whilst they're usually good at reflowing lines over the margin, most are reticent to reflow legitimate lines (for good reasons, generally).

That said, I know you do this semi-automatically in IntelliJ IDEA at (sorry haven't used Eclipse for a while but I think it's now possible):

Select the lines with comments and type Ctrl-Shift-J (or equivalent mapping to join lines). This immediately pulls them into one line. Ctrl-Alt-L to reformat that selection if you need to.

It's that or regex I guess.. but be warned...

declension
  • 4,110
  • 22
  • 25