是否可以自定义 XCode 的缩进样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to customize the indent style of XCode?
提问by Paul Wicks
For example, I'd like to not indent namespaces in C++ code, but the prefpane doesn't seem to have any place to make a decision of this granularity. Is there some hidden config file or something? Or am I just out of luck?
例如,我不想在 C++ 代码中缩进命名空间,但是 prefpane 似乎没有任何地方可以决定这种粒度。有没有隐藏的配置文件之类的?还是我只是运气不好?
回答by Naaff
Apple's XCode documentation contains a full list of user preferences, many of them that don't have a corresponding UI. I'm not seeing anything that is namespace-specific however, so I think you might be out of luck.
Apple 的 XCode 文档包含完整的用户首选项列表,其中许多没有相应的 UI。但是,我没有看到任何特定于命名空间的内容,所以我认为您可能不走运。
However, I thought I'd pass along the preferences list in case it's useful.
但是,我想我会传递首选项列表,以防它有用。
回答by Tom Swirly
I've also attempted to do this.
我也尝试过这样做。
The answer is that whoever did the code formatting in XCode appears to be completely unaware that there are languages other than Objective C, or coding styles other than Apple's.
答案是,在 XCode 中进行代码格式化的人似乎完全不知道存在 Objective C 以外的语言,或 Apple 以外的编码风格。
Here's a list of things that one would want to do that can't be done in XCode.
这是一个人们想要做但在 XCode 中无法完成的事情的列表。
- Indent public: or private: just one space.
- Indent namespaces zero spaces.
- Alternate indentation for arguments NOT relative to the opening parenthesis.
- 缩进公共:或私人:只有一个空间。
- 缩进命名空间零空格。
- 与左括号无关的参数的替代缩进。
The last one needs a little discussion. Sometimes, a function or method name can be quite long, as well as its first argument, so you want also to be able to indent like this:
最后一个需要稍微讨论一下。有时,函数或方法名称及其第一个参数可能很长,因此您还希望能够像这样缩进:
someExcitingClass->AVeryLongMethodNameTraLaLaLaLa(
someLongExpressionOrVariableNameGoesHere,
anotherNameHere);
Of course, you might want to be extracting subexpressions to make the line shorter, but in real-world code this comes up all the time, and creating subexpressions just to fit everything into a reasonable line length is annoying.
当然,您可能希望提取子表达式以使行更短,但在现实世界的代码中,这种情况一直存在,而创建子表达式只是为了将所有内容放入合理的行长度中是很烦人的。
It's a terrible shame and I really have no idea what to do. I personally write in emacs and only dip into XCode as a build system but :-D that's not for everyone.
这是一个可怕的耻辱,我真的不知道该怎么办。我个人用 emacs 编写,并且只将 XCode 作为构建系统深入研究,但是 :-D 并不适合所有人。
回答by Whirliwig
As the Xcode indenter uses just the lexer, and not the AST, you can 'fool' the formatting by defining away the curly braces.
由于 Xcode 缩进器仅使用词法分析器,而不是 AST,因此您可以通过定义花括号来“欺骗”格式。
I have:
我有:
#define NAMESPACE_OPEN(_name) namespace _name {
#define NAMESPACE_CLOSE(_name) }
#define dsmsg_namespace_open NAMESPACE_OPEN(dsmsg)
#define dsmsg_namespace_close NAMESPACE_CLOSE(dsmsg)
i.e., a generic 'NAMESPACE_OPEN/CLOSE' define, and a define specific to my most-used namespace 'dsmsg'. Then, whenever I want to open the namespace, I use
即,一个通用的“NAMESPACE_OPEN/CLOSE”定义,以及一个特定于我最常用的命名空间“dsmsg”的定义。然后,每当我想打开命名空间时,我都会使用
dsmsg_namespace_open
... code ...
dsmsg_namespace_close
Ugly hack, but I quite like having a specific, named 'close'
丑陋的黑客,但我很喜欢有一个特定的,名为“关闭”
回答by mmc
I bypass Xcode's indenting altogether, and have a user script that calls uncrustifyon the currently displayed document.
我完全绕过了 Xcode 的缩进,并有一个用户脚本在当前显示的文档上调用uncrustify。
#!/bin/sh
#echo -n "%%%{PBXSelection}%%%"
uncrustify -q -c ~/.uncrustify/sample.cfg -l oc+
#echo -n "%%%{PBXSelection}%%%"
Notes:
笔记:
- uncrustify must be in your PATH
- you may need to adjust the location of your config file
- if you want to have the new code selected in Xcode, uncomment the two echo statements (this can also be used to make a "Format Selection" script, rather than "Format All"
- uncrustify 必须在你的 PATH 中
- 您可能需要调整配置文件的位置
- 如果要在 Xcode 中选择新代码,请取消注释两个 echo 语句(这也可用于制作“格式选择”脚本,而不是“全部格式化”
Script Settings:
脚本设置:
- Input:Entire Document
- Directory: Home directory
- Output: Replace Document Contents
- Errors: display in alert
- 输入:整个文档
- 目录:主目录
- 输出:替换文档内容
- 错误:显示在警报中
回答by Aequitas
As of Xcode 4.3.1 no custom namespace indent options are available, however I overcame this irritation by navigating to Preferences->Text Editing->Indentation and disabling "Syntax-aware indenting".
从 Xcode 4.3.1 开始,没有自定义命名空间缩进选项可用,但是我通过导航到 Preferences->Text Editing->Indentation 并禁用“Syntax-aware indenting”来克服这种烦恼。
回答by Gabriel Gatzsche
Another possibility is to use Articstic Style (astyle). A tutorial how to integrate astyle into XCode using automator and services can be found here: http://eatmyrandom.blogspot.com/2011/03/xcode-astyle-part-2-for-xcode-4x.htmland http://youtu.be/d8bbE6_OHGc
另一种可能性是使用艺术风格(astyle)。可以在此处找到如何使用自动机和服务将 astyle 集成到 XCode 中的教程:http: //eatmyrandom.blogspot.com/2011/03/xcode-astyle-part-2-for-xcode-4x.html和http:/ /youtu.be/d8bbE6_OHGc