Monday, September 24, 2012

Line in Xcode 4


Opening Brace Brackets on Their Own Line in Xcode 4

If you're like me, you like your brace backets on their own lines, like this:
if (condition)
{
    statements
}
If you're even more like me, you make heavy use of Xcode's autocomplete and snippets, like the following:
And finally, if you're just like me, you get really disappointed when this happens:
if (condition) {
    statements
}
What the hell, Xcode? Even the project templates have brace brackets on their own lines. What gives?
Thanks to some research, I found the file you need to edit. The problem is, Xcode is now distributed through the Mac App Store, so the steps are a little different. Here's what you do.
  1. Open the terminal.
  2. Type cd /Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resourcesand hit Enter.
  3. We're editing SystemCodeSnippets.codesnippets, but let's make a backup first. Type sudo cp SystemCodeSnippets.codesnippets SystemCodeSnippets.codesnippets.backup and hit Enter.
  4. Use your text editor of choice to open the file as the root user.
    • Using vim, the command is sudo vim SystemCodeSnippets.codesnippets
    • Using anything else, like TextMake, it's sudo open -a TextMate SystemCodeSnippets.codesnippets
  5. Do a "Find" for the word "condition" and you should find almost all the instance of the snippets you want to modify. It'll look like this:
        IDECodeSnippetVersion
        1
       IDECodeSnippetCompletionPrefix
        if
        IDECodeSnippetContents
        if (<#condition#>) {
    <#statements#>
    }
    
    Just put a new line before the opening brace bracket.
        IDECodeSnippetVersion
        1
        IDECodeSnippetCompletionPrefix
        if
        IDECodeSnippetContents
        if (<#condition#>)
    {
    <#statements#>
    }
    
    This will find all the if statements and most loops; search for the text "forin" to modify the fast enumeration snippet.
Save the file and restart Xcode. That's it!
Update: My friend Zev is maintaining an up-to-date version of SystemCodeSnippets.codesnippetsfor the latest Xcode version on GitHub. Go check it out.

No comments:

Post a Comment