a great tool for automatically formating your code!

本文介绍如何在项目中配置并使用Clang格式化工具,包括创建.clang-format文件来定制代码风格,以及如何利用Vim和Xcode插件实现代码自动格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. make your own format file at your project's root or file's folder. (The clang-format will automatically use the latest format file to format your code)

vim .clang-format

IndentWidth: 8

UseTab: Always

BreakBeforeBraces: Linux

AllowShortIfStatementsOnASingleLine: false

IndentCaseLabels: false

2. Then use the clang-format plug-in to automatically format your code.

Edit-->Clang-format-->Format File in Focus

 

Most likely, you'll have a .clang-format file at the root of your project.

So easy! so neat!

 

ClangFormat-Xcode

An Xcode plug-in to format your code using Clang's format tools, by @travisjeffery.

With clang-format you can use Clang to format your code to styles such as LLVM, Google, Chromium, Mozilla, WebKit, or your own configuration.

usage

demo

Installation:

Install via Alcatraz.

OR

Clone this repo, build and run ClangFormat, restart Xcode.

Usage:

Format on save

I.e., you press command-s and the file is formatted and wrote to disk.

In the menu, open Edit > Clang Format > Click Format on save (a checkmark appears in this menu item indicicating that the feature is active.)

Assign keyboard shortcuts

You can assign your own keyboard shortcuts like so:

  • Open the System Preferences > Keyboard > Shortcuts > App Shortcuts > Click +
  • Set the application to be Xcode
  • Set the menu title to an action title, e.g. "Format File in Focus"
  • Set your shortcut

In this example, we'll format the active file when control-i is pressed.

assign keyboard shortcut

Using your own style configuration

By using Clang Format > File in the plug-in menu, Clang will look for the nearest .clang-format file from the input file. Most likely, you'll have a .clang-format file at the root of your project.

Here are the options for .clang-format and how they're configured.

If one of the built-in styles is close to what you want, you can bootstrap your own configuration with:

./bin/clang-format -style=llvm -dump-config > .clang-format

For example, this .clang-format is similar to the Linux Kernel style:

BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false

And this is similar to Visual Studio's style:

UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0

转载于:https://www.cnblogs.com/airwindow/p/4051259.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值