`
啸笑天
  • 浏览: 3430864 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

使用Uncrustify在Xcode中格式化Objective-C代码

 
阅读更多

from:tinyfool的新杂志

Xcode是开发iOS应用和Mac OS应用的必备工具,但这个工具的辅助功能相对于Eclipse之类的IDE来说,还是比较少,在Eclipse里只要按Ctrl+Shift+F,就可以将Java代码格式化得非常好看。而在Xcode里,只有Re-Indent(Ctrl+I)这一个调整缩进的功能,显然不够用。

如果想要完成完备的代码格式化,得要借助Uncrustify这样的工具。更进一步,我们用Automate配置一个针对Text的Service,再加上快捷键,就可以在Xcode里间接实现和Eclipse同样效果的代码格式化功能了。

首先安装Uncrustify工具。安装的方法很多,推荐用Homebrew来安装。如果机器上没有Homebrew,先用这条命令安装:

/usr/bin/ruby -e "$(curl -fksSL http://u.aodaren.com/homebrew)"

安装好Homebrew后,用这条命令安装Uncrustify:

brew install uncrustify

详细的Automate配置步骤可以参考这篇文章,如果不想自己配置,也可以直接下载已经配置好的workflow包,复制到~/Library/Services/,并解压就可以了:

mv Uncrustify-Objective-C.workflow.tar.gz ~/Library/Services/
cd ~/Library/Services/
tar zxvf Uncrustify-Objective-C.workflow.tar.gz

下载针对Objective-C语言的Uncrustify配置文件,移动到 ~/ 目录,并更名为 .uncrustify_obj_c.cfg,注意目标文件名前面有个 .:

mv uncrustify_obj_c.txt ~/.uncrustify_obj_c.cfg

如果上面已经顺利完成,那么现在到Xcode里选中一段代码,点右键,在Services里面应该已经有一个Uncrustify Objective-C服务了。选择后,即可将代码格式化。当然,如果每次都点鼠标显然太麻烦,下面来配置快捷键。

点屏幕左上角的苹果图标,选 System Preferences – Keyboard – Keyboard Shortcuts – Services,在Text分区里,找到刚添加的服务Uncrustify Objective-C,给这个服务加上快捷键。注意不要和Xcode里其它快捷键冲突,推荐设置为:Opt+Cmd+字母O。

这样以后在Xcode里,先用Cmd+A全选代码,再用Opt+Cmd+O来格式化代码。整个效果和Eclipse基本上差不多。

最后,如果对格式有不同的需求,还可以修改cfg文件里的设置,每个选项都有详细的说明,这里就不再赘述了。

分享到:
评论
1 楼 啸笑天 2013-06-14  
Code Formatting in Xcode 4

Automatic indentation and cleanup of code seems to have improved in Xcode 4 (Editor menu – Structure – Re-Indent) but it still doesn’t offer full code reformatting or the flexibility of a tool like Uncrustify. If you’re used to having external code formatting in Xcode available you might be disappointed to find the User Scripts menu missing in Xcode 4.

Thankfully Tony Arnold demonstrated one possible solution with his Xcode 4 Uncrustify Automator Services. Here’s how you can get your external code formatting tool up and running again with Xcode 4.

Install Uncrustify (port install uncrustify, brew install uncrustify, or directly from http://uncrustify.sourceforge.net/)

Open Automator and create a new Service.
Creating a Service in Automator
Creating a new Automator Service

Inputs to Automator Services are more limited than the options available in Xcode 3′s User Scripts. To reformat selected text we can still pass the input from Xcode to a Run Shell Script action.

1
uncrustify -l OC -q -c ~/.uncrustify/uncrustify_obj_c.cfg
The "uncrustify selected text" service in Automator
Uncrustify Selected Text

To reformat open files we need to use AppleScript to get the paths to those files so that we can pass them to Uncrustify. I wasn’t able to find a reliable way to select only the currently visible source file so I settled on the following script to reformat all open and modified source files instead.

1
2
3
4
5
6
7
8
tell application id "com.apple.dt.Xcode"
    repeat with current_document in (source documents whose modified is true)
        set current_document_path to path of current_document
        set raw_source to text of current_document
        set formatted_source to do shell script "uncrustify -l OC -q -c ~/.uncrustify/uncrustify_obj_c.cfg -f " & current_document_path
        set text of current_document to formatted_source
    end repeat
end tell
The "uncrustify modified documents" service in Automator
Uncrustify Modified Documents

Open Xcode, find the new Services available in the “Xcode” menu. Use the Services Preferences to bind these Services to keyboard shortcuts to your liking.
The Xcode and Services menus
Services available in Xcode

Enjoy cleaner code.

相关推荐

Global site tag (gtag.js) - Google Analytics