如何在xcode中安装配置clang-format

clang-format是很好用的插件,所以我们也想在xcode中继续使用。

install

brew install clang-format

check version:

clang-format --version
clang-format version 9.0.0 (tags/google/stable/2019-05-14)

add automator service

创建automator action
automator
在左侧搜索“运行shell脚本”,并拖入右侧,在脚本本输入:

export PATH=/usr/local/bin:$PATH
clang-format

automator
将服务保存为clang-format(或者任意你喜欢的),后面还有用到这个名字。

setting clang-format

在当前用户的根目录 ~ 放置一个 .clang-format 文件。参考内容如下:

BasedOnStyle: LLVM
BreakBeforeBraces: Linux
Language: Cpp
ColumnLimit: 120
ReflowComments: false
SortIncludes: false

#括号风格
BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum: false
  AfterStruct: false
  SplitEmptyFunction: false

注意,这个只是参考,你需要按照你实际的风格进行设置。

add keyboard shortcuts

在系统设置页面找到“键盘”,选择“快捷键”的tab,在左侧选择 “app快捷键”,然后选择+号,创建用于xcode的快捷键,选择clang-format服务,然后选择一个你准备用户代码格式化的快捷键组合,比如 ctrl+i。
automator
在xcode中,选择我们的目标代码,然后ctrl+i,就触发clang-format格式化了。

done!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注