Xcode: Adjusting indentation of auto-generated braces?

浪子不回头ぞ 提交于 2019-11-27 05:12:36

问题


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:

How can I fix this?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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 = "";
};



回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/392749/xcode-adjusting-indentation-of-auto-generated-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!