5

Our codebase has a "cuddled" curly brace style. That is, the { appears on the same line as the function (or if or while) statement that it goes with:

if (condition) {
    doThis();
}
else {
    doThat();
}

We have a coder who keeps committing stuff with the curly brace on its own blank next line, which drives me bonkers:

if (condition)
{
    doThis();
}
else
{
    doThat();
}

I don't want to start a debate on the relative merits of the two formats, but I do want to bring this programmer's code in line with our established style. Is there a Sublime Text plugin or macro that will convert between these styles? Doing it manually seems like such a waste of time.

4
  • 1
    This question is on topic on Super User, as it's more about text editor configuration and usage than programming. You can of course request migration, e.g. if you don't receive any answers here after a few days.
    – Daniel Beck
    Dec 26, 2012 at 18:27
  • Wouldn't it be a better approach to reject that developer's commits to the SCM, e.g. with Subversion pre-commit hooks?
    – Daniel Beck
    Dec 26, 2012 at 18:30
  • Going forward, that might be a viable option (and I've already told him to knock it off, so I hope that it doesn't happen again), but there's a lot of code that he's already got in our repo with the variant brace style. So we're talking about cleanup at this point. Dec 26, 2012 at 18:35
  • 1
    In that case, a multi-line regular expression might work for you, depending on programming language (side-effects e.g. on arrays, structs, and other curly-braced things). You could try to replace all (regular expression) " *\n\{" with " {", and all " *\}\n *else" with "} else" as a very low-tech option. Also depending on programming language, you could just use an editor like Visual Studio (free variants exist) or Eclipse once, reformat the whole code base, then do one "cleanup commit".
    – Daniel Beck
    Dec 26, 2012 at 18:40

2 Answers 2

5

As Daniel Beck suggested in his comments, this can be done with a regex find-and-replace. The regex that ended up working for me in Sublime Text was the following:

Find What: \n(\s)*\{ (that is, look for any newline character, followed by zero or more whitespace characters, followed by a left curly brace character)

Replace With: { (a space and a left curly brace)

I'm doing the replacements one by one, since sometimes my rogue programmer sometimes also stuck code on the "curly brace" line, like this...

if (condition)
{doThis();}

... and I have to double-check them before I do the replacement.

I still think that a plugin might be able to handle this kind of situation very gracefully, but this works for now.

2
  • I have the same problem, when they have also added comments { // comments after the {. It would be great to find a regex, that wold also catch the comments and move the brace in front.
    – not2qubit
    Jun 5, 2018 at 14:56
  • 1
    @not2qubit The CodeFormatter plugin in my other answer will fix that problem without a problem. Jun 6, 2018 at 15:20
2

I found an excellent Sublime Text plugin called CodeFormatter that will adjust the brace style in many different kinds of code, as well as doing several other "beautify" features.

Huzzah!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .