Xcode 4 with opening brace on new line
Asked Answered
R

6

24

It seems like the new Xcode 4 does not apply the XCCodeSenseFormattingOptions anymore. At least for me :(

Anyway, do you know how to put the opening brace to the new line for autocompletion in Xcode 4? I used to type this in terminal but it does not work for the new Xcode.

defaults write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator "\\n" PreMethodDeclSpacing ""
Roadster answered 25/2, 2011 at 17:12 Comment(2)
I answered a similar question here: (There's an app for that!) #4963534Mounts
Going to open myself up to a flame war here but why not just leave the opening brace on the same line. E.g. lots of Apple code I've seen has the opening brace for an if or switch statement on the same line. Opening braces for a method are often on a new line but that's more under your control.Heidt
S
29

XCode 4 uses "code snippets" to do autocompletion, and ships with a built-in library of them: You can view the Code Snippet Library by clicking on the { } icon in the Library Pane, which is probably on the lower right-hand side of your main XCode window.

All of XCode 4's built-in code snippets put the opening brace on the same line as the statement – this is XCode 4's code snippet for an if statement, for example:

if (<#condition#>) {
  <#statements#>
}

So if you wanted XCode 4 to autocomplete like so:

if (<#condition#>) 
{
  <#statements#>
}

...then you'd have to edit the code snippet accordingly. This, in turn, leads to two problems:

  1. There are 44 code snippets built into XCode 4, and you'd have to edit each one separately.
  2. XCode 4 won't allow you to edit the built-in code snippets.

These problems are more challenging than the simple defaults write command that worked in XCode 3 – but it is possible, if you're determined and you can edit property lists, to delve into the guts of XCode 4 and change these code snippets one by one.

/Developer/Library/Xcode/PrivatePlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets contains XCode 4's library of built-in code snippets. This probably goes without saying, but you should make a backup of this file before charging in and making edits – and afterwards you should make another backup, and set aside a copy of the file with your new and improved code snippets, because you'll almost certainly overwrite the contents of /Developer/Library/Xcode when you install the next release of XCode 4. (It's also possible that Apple will change the format of this file, add new code snippets, or do any number of other things that could render this answer ineffective.)

If you have Xcode 4.3 or later installed directly from the App Store, everything is inside the Xcode.app bundle. The path to SystemCodeSnippets.codesnippets is /Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets.

Anyhow, you'll find the above file contains several entries like this one:

<dict>
    <key>IDECodeSnippetVersion</key>
    <integer>1</integer>
    <key>IDECodeSnippetCompletionPrefix</key>
    <string>if</string>
    <key>IDECodeSnippetContents</key>
    <string>if (&lt;#condition#&gt;) {
  &lt;#statements#&gt;
}</string>
    <key>IDECodeSnippetIdentifier</key>
    <string>D70E6D11-0297-4BAB-88AA-86D5D5CBBC5D</string>
    <key>IDECodeSnippetLanguage</key>
    <string>Xcode.SourceCodeLanguage.C</string>
    <key>IDECodeSnippetSummary</key>
    <string>Used for executing code only when a certain condition is true.</string>
    <key>IDECodeSnippetTitle</key>
    <string>If Statement</string>
    <key>IDECodeSnippetCompletionScopes</key>
    <array>
        <string>CodeBlock</string>
    </array>
</dict>

This is the code snippet for autocompleting an if statement. Edit the IDECodeSnippetContents to put the opening brace on a new line, save your work, and then restart XCode 4; if all goes well, you should be able to type an if statement and see the results.

You'll need to make at least half a dozen more edits to cover the most common autocompletes (for, while, etc.), and if you want to be thorough it'll take somewhere around 40 separate edits. It's a lot of work, but if you really, really want XCode 4's autocompletion to put your opening braces on a separate line, it can be done.

Scowl answered 23/4, 2011 at 9:19 Comment(5)
There's an app that you can use to manage and edit the code snippets, called Snippet Edit. Takes a lot of the pain out of snippet management.Donnelly
It's also possible to quickly bang out a script that will create fully editable "user" scripts that override the set-in-stone defaults: #4963534Opalescent
Note that Snippet Edit is now free.Medovich
This is frustrating and awful.Gaseous
@Deco Don't know, havn't used it in a whileMedovich
M
4

There’s no way to do it in Xcode 4. Please do file a bug.

Merited answered 22/4, 2011 at 17:2 Comment(1)
Couldn't find it in the bug reporter so I did file a bug -- 10613842Cherie
W
2

Just make a user code snipped which overrides Apple's version. Do this by entering in the Completion Shortcut field the same name as in the default snipped. Look at this video for a howto: http://s3.amazonaws.com/screencasts.pragmaticstudio.com/017_custom_code_snippets.mov

Whirl answered 21/6, 2011 at 14:37 Comment(0)
E
2

Check out my modified (system wide) snippets for Xcode 4.2 here:

http://forrst.com/posts/Put_that_where_it_belongs_Xcode-PNL

It should take care of all the relevant opening curly braces for iOS development..

Excitant answered 12/10, 2011 at 18:16 Comment(5)
Thanks for providing the modified file Sveinung! Copy paste and looks like its all working well from what I have tested. (xCode 4.5b)Pelagi
Cool, nice to know that 4.5 still supports this "hack".Excitant
I had to put it in /Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets, but it works in xcode 4.4Barron
This no longer worked for me, Added the file to the same place as @Barron and now no braces :( (xCode 4.4, Mountain Lion)Pelagi
Did you re-open Xcode? I just did this on 4.4 & Mountain Lion. Let me check the path for you tomorrow.Excitant
P
2

In Xcode 4.3.1, if you edit the following file as sudo from the terminal, as Scott Forbes described above, you can change where the opening brace appears. It would go away with new installations of Xcode, I would imagine, so I would vote that this is a bug with Apple too.

/Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets

Leslie

Polyzoic answered 9/3, 2012 at 17:49 Comment(0)
T
2

I used SnippetEdit for Xcode4 and it works amazingly. It basically lets you replace old snippets given by xcode with the new ones defined by yourself. See more here: https://www.macupdate.com/app/mac/43352/snippet-edit

Thousand answered 24/1, 2014 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.