objective-c 如何为代码自动格式化设置 Xcode 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/561474/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:09:31  来源:igfitidea点击:

How to set Xcode plugin for code auto formatting

objective-cxcode

提问by Carlos Barbosa

I am searching for a plugin to auto format objective-c code within XCode, it would also be very helpful to have a set of optional styling formats,

我正在寻找一个插件来在 XCode 中自动格式化 Objective-c 代码,拥有一组可选的样式格式也非常有帮助,

I'm aiming to follow Google's code convention: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xmlas a start, any help is appreciated.

我的目标是遵循谷歌的代码约定: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml作为开始,任何帮助表示赞赏。

回答by mmorris

updated April 2013

2013 年 4 月更新

As Roger Nolan and rcw3 suggested, you can go a lot further with Uncrustify. The latest release version can be download here.

正如 Roger Nolan 和 rcw3 所建议的那样,您可以使用Uncrustify 走得更远。最新发布版本可以在这里下载。



Integration Into Xcode

集成到 Xcode

Integration into Xcode is dependent upon the version of Xcode that is installed.

与 Xcode 的集成取决于安装的 Xcode 版本。

  • Xcode 4.6 & newer
  • Xcode 4.6 及更新版本

Beno?t Bourdon's BBUncrustifyPlugin-Xcode

Beno?t Bourdon 的BBUncrustifyPlugin-Xcode

This plugin provides access to Uncrustify right from Xcode's "Edit" menu ("Uncrustify Selected Files", "Uncrustify Active File", and "Uncrustify Selected Lines"). Just build the project and the plugin is compiled and installed.

这个插件可以直接从 Xcode 的“编辑”菜单(“Uncrustify Selected Files”、“Uncrustify Active File”和“Uncrustify Selected Lines”)访问 Uncrustify。只需构建项目并编译和安装插件。

As of April 2, 2013, Uncrustify 0.60 and an Uncrustify configuration file are include with the plugin. To use a custom Uncrustify configuration file, make sure to read the section titled "How to customize the Uncrustify configuration?" of README.md.

截至 2013 年 4 月 2 日,插件中包含 Uncrustify 0.60 和 Uncrustify 配置文件。要使用自定义 Uncrustify 配置文件,请务必阅读标题为“如何自定义 Uncrustify 配置?”的部分。的README.md

Note: To use Uncrustify from the command line, for example as part of a git hook, I recommend building and installing Uncrustifyindependently. For consistency sake, I also recommend replacing the version of Uncrustify that comes with the plugin with a link to the version Uncrustify that was independently built and installed. The following is how to do that:

注意:要从命令行使用 Uncrustify,例如作为 git hook 的一部分,我建议独立构建和安装Uncrustify。为了保持一致性,我还建议将插件附带的 Uncrustify 版本替换为独立构建和安装的 Uncrustify 版本的链接。以下是如何做到这一点:

cd ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/UncrustifyPlugin.xcplugin/Contents/Resources
mv uncrustify uncrustify.orig
ln -s /usr/local/bin/uncrustify uncrustify
  • Xcode 4.x
  • Xcode 4.x

Jonah Williams' Code Formatting in Xcode 4

Jonah Williams在 Xcode 4 中代码格式

  • Xcode 3.x
  • Xcode 3.x

Jonah Williams' Code Formatting in Xcode

Jonah Williams在 Xcode 中代码格式



Example Uncrustify Configuration File

示例 Uncrustify 配置文件

The Uncrustify 0.60 configuration file that I use for Objective-C is located here.

我用于 Objective-C 的 Uncrustify 0.60 配置文件位于此处



Update the Uncrustify Configuration File for a New Version of Uncrustify

更新 Uncrustify 新版本的 Uncrustify 配置文件

To update the Uncrustify configuration file for a new version of Uncrustify, start a Terminal and type:

要为新版本的 Uncrustify 更新 Uncrustify 配置文件,请启动终端并键入:

mv uncrustify.cfg uncrustify.cfg.old
uncrustify -c uncrustify.cfg.old --update-config > uncrustify.cfg

To see the changes made to the Uncrustify configuration, in the terminal type:

要查看对 Uncrustify 配置所做的更改,请在终端中键入:

diff uncrustify.cfg.old uncrustify.cfg


To View a Documented Version of the Uncrustify Configuration File

查看 Uncrustify 配置文件的记录版本

To view a documented version of the Uncrustify configuration file, start a Terminal and type:

要查看 Uncrustify 配置文件的文档版本,请启动终端并键入:

uncrustify -c uncrustify.cfg --show-config

回答by Rog

XCode has a code formatter built in - it's a little basic but can re-indent your code. First set your indentation preferences in Xcode (as mouviciel suggests). Then select the source you want to reintend and choose Edit | Format | Re-indent.

XCode 有一个内置的代码格式化程序——它有点基础但可以重新缩进你的代码。首先在 Xcode 中设置您的缩进首选项(如 mouviciel 建议的那样)。然后选择您要重新指定的来源并选择 Edit | 格式 | 重新缩进。

If you want to go further, Hackertoys has instructions for adding uncrustify supportto Xcode. I have not tried this.

如果你想更进一步,Hackertoys 有向 Xcode添加 uncrustify 支持的说明。我没有试过这个。

回答by brainray

AppCode from Jetbrains has a very good code formatter. For my taste uncrustify feels much too hacky.

Jetbrains 的 AppCode 有一个非常好的代码格式化程序。对于我的口味 uncrustify 感觉太老套了。

回答by rcw3

After tinkering with multiple external formatters and the weak internal xcode formatter, I finally settled with uncrustify. Uncrustify has fairly good Objective-C support, can easily be integrated with xcode as a user script, and provides a centralized formatter for pretty much all languages that xcode natively supports.

在修改了多个外部格式化程序和弱的内部 xcode 格式化程序之后,我终于解决了 uncrustify。Uncrustify 具有相当好的 Objective-C 支持,可以轻松地作为用户脚本与 xcode 集成,并为几乎所有 xcode 原生支持的语言提供集中式格式化程序。

The biggest hurdle with xcode is the daunting configuration file. My recommendation, take one of the supplied sample configs (ben2.cfg is very good), merge in the objc.cfg sample, and tweak as necessary.

xcode 的最大障碍是令人生畏的配置文件。我的建议是,采用提供的示例配置之一(ben2.cfg 非常好),合并到 objc.cfg 示例中,并根据需要进行调整。

回答by mkb

Reportedly, bcppworks just fine on obj-c, and it has a TextMateplugin, too.

据报道,bcpp在 obj-c 上工作得很好,它也有一个TextMate插件。

回答by mouviciel

A partial answer is that some little things can be set from within Xcode, as 2-space indents. See Indentation Preferencesin Xcode Workspace Guide.

部分答案是可以从 Xcode 中设置一些小东西,如 2 空格缩进。请参阅Xcode 工作区指南中的缩进首选项