Xcode: Adjusting indentation of auto-generated braces?
Asked Answered
E

6

39

Code auto-generated by Xcode seems to have the opening brace on the same line by default:

@interface Controller : NSObject {

}

I'd like the opening brace on a line of its own, like this:

@interface Controller : NSObject 
{

}

This applies in general to any method / class auto-generated by Xcode. In Xcode preferences I have "Indent solo { by" set to 0: alt text

How can I fix this?

Examine answered 25/12, 2008 at 11:21 Comment(1)
@PEZ - It may not be our style, but the guy's got right to his own standards.Ruddy
I
22

For Xcode 3.x, you can use the following: If you open up a terminal session and enter
defaults write com.apple.Xcode XCCodeSenseFormattingOptions '{ "BlockSeparator" = "\n" ; }'
it will start new blocks of code on a new line. Note that you will have to restart XCode if you have it opened in order for the new defaults to be read and used.

For Xcode 4.x to 6.x (current) I suggest you use Snippet Edit as a handy tool for editing the snippets that define the Xcode autocompletion. At the current time it's free and works great.

Inseparable answered 7/5, 2010 at 11:32 Comment(5)
worked a treat.. my particular interest was in making auto-generated code like for loops etc have the "right" brace style. How do you find out about these defaults?Traditional
j.mp/xcode_formatting_defaults (I got that from searching for "xcode user defaults". The first link will lead you there.Examine
This does not seem to work with Xcode 4.2, too bad. Thank you for the information.Rozier
Indeed it does not. I have tried a variety of different settings for XCCodeSenseFormattingOptions and none of them have had any affect whatsoever. According to the most current man page, I'm pretty sure it should though, so I personally feel it is a bug.Inseparable
For a thorough explanation of how to change the Xcode 4.2 code snippet library and do this, see the answer by Scott Forbes at #5120843Inseparable
L
8

The New Project and New File commands don't really generate any code; they fill in templates.

You'll need to create your own templates (probably based on Apple's) with the changes you want.

In Xcode 3.1, the stock templates are in /Developer/Library/Xcode. You'll put your modified copies in ~/Library/Application Support/Developer/Shared/Xcode. Pay attention to the subfolders of those folders.

Laine answered 25/12, 2008 at 18:51 Comment(0)
G
7

Read this: XCCodeSenseFormattingOptions

This document describes all the formatting options that auto-complete will use for brace and argument style. Here are mine:

    XCCodeSenseFormattingOptions =     {
    BlockSeparator = "\\n";
    PreMethodDeclSpacing = "";
};
Giltedged answered 25/12, 2008 at 18:8 Comment(4)
I think he's talking about the New Project and New File commands, not auto-completion. (At least, I can't imagine putting all my classes in one file using auto-completion to create each one.)Laine
new URL: developer.apple.com/mac/library/documentation/DeveloperTools/…Hoover
@Peter, I was referring to autocomplete and New File/Project, both.Examine
@WarrenP link is dead as wellSignalment
C
7

This is something I would like to see too, I don't like the "new school" way of curly braces on the same line as functions or loop control.

When editing and shuffling lines of code around, it's easier to keep everything relevant in one line. Curly braces should only define scope, they're not part of the function definition or program execution (like in if, for etc..).

Modifying all the templates is not really a good solution, but thanks for the info anyway.

Checkerberry answered 4/11, 2009 at 14:56 Comment(1)
Not really new school... It comes from the old ways of saving space when writing code in books. Cargo cult dictates everyone just copy what they have seen...Alms
T
3

If you want to have your preferred style in all situations, you need a combination of Laughing_Jack's answer and Peter Hosey's answer. In addition to what Peter Hosey said, you may also like to know that the Cocoa Touch file templates are in the following location. You can copy that directory to your custom templates too:

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Classes/

The Xcode User Defaults give you your style when typing out code by hand, the file templates provide it when auto-generating a new class etc.

Tramline answered 9/2, 2011 at 15:11 Comment(0)
W
0

I believe that "defaults write com.apple.Xcode" doesn't work on the latest versions of Xcode (7.x)

The workaround is to edit the default Code Snippets.

However, Xcode doesn't allow to modify the default Code Snippets.

In order to modify them, you can download a "Snippet Edit" program from here

Warren answered 10/12, 2015 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.