Giter Site home page Giter Site logo

clangformat's Introduction

#代码格式化插件教程与配置

前言

统一团队内部编程风格,提高程序的可读性以及团队的编码效率,避免团队在开发过程中因为编码风格的差异可能带来的混乱。所以说代码规范对于团队的好处想必大家应该都知道。

当然,不仅对团队很重要,个人也很重要,如果你现在去了一家新的公司,上个人留下的代码风格跟你的差异很大,你看着也很难受。

有了这个插件,你就可以定制自己的代码风格,一键格式化,再也不用担心看别人的代码觉得难受,大大节约了你的时间。

正文

ClangFormat-Xcode:是一款Xcode的代码格式化插件 (github下载链接),非常方便好用,可以极大减少花费调整代码规范的时间,提高编码效率。用了之后,妈妈再也用不担心我的编码规范了。 下面教大家怎么去配置ClangFormat-Xcode,打造一个属于自己的团队的代码规范。

安装插件

  • 1.手动安装
    和其他的插件一样,先去github下载工程,运行程程序安装。
  • 2.自动安装
    如果安装过Alcatraz的同学,可以去可视化窗口搜索ClangFormat,点击Install自动安装。

配置参数

安装完成之后重启Xcode,点击Load Bundle,选择Xcode ->Edit,看到ClangFormat的选项,说明安装成功了。 ClangFormat菜单

我们可以看到有LLVM,Google,Chromium,Mozilla,WebKit,File这几种格式化风格,File就是我们自定义的风格。 下面我来教大家怎么去自定义风格。 首先创建一个".clang-format"文件,创建这个文件有两种方式:

  • 1.终端创建:这边以DemoTest项目为例,打开终端,cd到工程目录下(一定要工程目录哦),创建一个".clang-format"文件。

$  cd /Users/xxx/DemoTest
$  vim .clang-format

然后将下面这些配置参数粘贴进去,这些参数是我根据我们团队的代码规范整理出来的(仅供参考),可能并不适合你们的团队,所有参数的含义下面都有说明:

# 基础样式
BasedOnStyle: LLVM

# 缩进宽度
IndentWidth: 4

# 花括号的换行方式(Attach,Stroustrup, Allman-所有大括号都另起一行)
BreakBeforeBraces: Allman

# 支持一行的if
AllowShortIfStatementsOnASingleLine: false

# 是否允许循环单行
AllowShortLoopsOnASingleLine: false

# switchcase缩进
IndentCaseLabels: true

# 针对OC的block的缩进宽度
ObjCBlockIndentWidth: 4

# 针对OC,属性名后加空格
ObjCSpaceAfterProperty: true

# 每行字符的宽度限制,0不限制
ColumnLimit: 0

# 注释对齐
AlignTrailingComments: true

# 括号后加空格
SpaceAfterCStyleCast: true

# 不在小括号里加空格
SpacesInParentheses: false

# 不在中括号里加空格
SpacesInSquareBrackets: false

# 指针对准
# DerivePointerAlignment: true

# @[]里面两边空格,默认true
SpacesInContainerLiterals: false

# 赋值前(=)的空格 默认true
#SpaceBeforeAssignmentOperators: true

# 指针的位置
PointerAlignment: Right

# 最大空的行数
MaxEmptyLinesToKeep: 1
  • 2.手动拖入:如果你觉得上面太麻烦,你也可以用比较简单粗暴的方法,把配置好的.clang-format文件直接拖到工程目录下,这边附上配置好的.clang-format

基本使用

前面的配置成功之后,后面的使用就更简单了,打开你刚刚配置的工程,然后点击Xcode ->Edit->ClangFormat->Format Select Files,格式化整个文件中的代码。 下面附上两张格式化前和格式化后的截图: 格式化前:

格式化后:

!

是不是感觉一下子从凤姐变成了AngelaBaby,代码是是我们程序员的脸面,大家理应重视。

快捷键

快捷键的使用:可以给常用的操作设置快捷键,我们一般常用的操作是Format Select Files和Format Select Text两个功能。

  • 1.Format Select Files是格式化当前文件的代码。设置快捷键:点击下面的Enable Format on Save(当它是Disable Format on Save状态就不用点了),插件会自动帮我们生成(Command+S)快捷键。

  • 2.Format Select Text是格式化选中的代码。这个快捷键需要手动设置。

(PS:第二个快捷键可以不用设置的,因为Format Select Files格式化当前文件已经够用了,感兴趣的同学可以试试),打开设置->键盘->快捷键->点击右侧应用快捷键->添加。

ok,大功告成!

最后

文中如果有错误的地方,还请大神指正。

如果你觉得看完后对你有所帮助,还望点个star。赠人玫瑰,手有余香。

clangformat's People

Contributors

leejayid avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.