Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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 '12 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 '12 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. –  Curtis Gibby Dec 26 '12 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 '12 at 18:40

2 Answers 2

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.

share|improve this answer
up vote 2 down vote accepted

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.