3

I started out coding using the Allman style, with aligned braces:

void foobar()
{
  if(foo)
  {
    bar();
  }
}

After decades I've decided I want that extra screen space; and besides, my client uses non-matched braces, so it's hard to switch back and forth from work to my personal code. So I want to convert all my existing code to K&R:

void foobar() {
  if(foo) {
    bar();
  }
}

Eclipse 4.4 has a sufficient code formatter, and if I select my source tree I can even format files in bulk. The problem is that if I have a line comment where a K&R brace would be, Eclipse will refuse to move the brace up a line, leaving me with a hodgepodge of coding styles:

void foobar() {
  if(foo) //if foo
  {
    bar();
  }
}

How can I tell Eclipse to move the brace up, even if there is a comment on the line where the brace goes?

void foobar() {
  if(foo) { //if foo
    bar();
  }
}
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272

1 Answers1

1

A regular expression replacement over files; replace

\)\s*(//.*)(\r?\n)(\s*)\{
     1     2      3

with:

) {\2\3    \1\2

Update (Garret Wilson): After much experimentation, the ideal regular expression replacement that addressed all the variations and met my needs was:

^([^/]*(?:\)|\>|do|else|Exception|static|try))\s*(//.*)(\r?\n)(\s*)\{

replaced with:

\1 {    \2
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I was thinking along the same lines. But... can we make Eclipse do that regex replacement as part of save actions or formatter? In case he runs into "old format" code in the future or accidentally creates it again himself. – The111 Jun 08 '14 at 23:06
  • Sorry, I would need to try too. – Joop Eggen Jun 08 '14 at 23:27
  • Formatting adjusts white space. What you're asking for is different. – nitind Jun 09 '14 at 02:20
  • I think the replacement expression I'm wanting is `) { \1`. That seems to work nicely---thanks a lot! – Garret Wilson Jul 24 '14 at 17:53
  • Unfortunately there are some tricky places where an `if(...)` is followed by a commented out variation (another `if(...)`), so that the actual brace on the third line is pulled up into the comment, which throws off the block structure and causes compile errors... – Garret Wilson Jul 24 '14 at 18:18
  • In other words I need a way for it to ignore commented-out lines. – Garret Wilson Jul 24 '14 at 18:37
  • I think I have it! I needed to restrict the first part of the match to a single line with no comments: `^([^/]*\))\s*(//.*)(\r?\n)(\s*)\{` replaced with `\1 { \2`. So far so good! – Garret Wilson Jul 24 '14 at 18:46
  • Looks like I'll have to do it for `else //` as well. – Garret Wilson Jul 24 '14 at 18:55
  • OK, so far I have `^([^/]*(?:\)|else))\s*(//.*)(\r?\n)(\s*)\{` replaced with `\1 { \2`. I'll keep looking over the results to see if that does everything I need. – Garret Wilson Jul 24 '14 at 19:01
  • Thanks for feedback: reminds of the usefulness of regex replaces im editing. You might also consider `*?` / `+?` and (negative) look-behind / look-ahead. One conclusion: regex is the _useful_ kind of sudoku. – Joop Eggen Jul 25 '14 at 08:52
  • This is even more complicated than it first appears. You have to account for classes with generic parameters. You have to account for methods with lists of thrown exceptions. You have to account for static blocks. Whew! After several conversions and going over the output, I think I have what I want. I'll update the answer accordingly. – Garret Wilson Aug 03 '14 at 15:46