使用clang-format
2023年11月11日 2024年2月13日
说明
- 需要安装clang-format程序
- 对所有项目生效
安装clang-format
macOS
1where clang-format 2brew search clang-format 3brew install clang-format
查看clang-format路径
1where clang-format
/usr/local/bin/clang-format
Sublime安装clang-format插件
可以这么理解, 在Sublime和clang-format之间建立桥梁
-
包管理器安装clang-format
Command-Shift-P
-
配置路径
Sublime Text > Preferences > Package Settings > Clang Format
参照 CustomStyle - Default
clang_format_custom.sublime-settings – Clang Format Settings - Default
clang_format.sublime-settings – Clang Format 用户设置 CustomStyle - User
clang_format_custom.sublime-settings – User Settings - User
clang_format.sublime-settings – User 将参照拷贝到用户设置
设置clang-format
-
使用默认配置
打开Settings - User
- binary 程序路径 默认 style 样式 Custom format_on_save 保存时对文件格式化 true languages 格式化语言 默认 1{ 2 // This is the path to the binary for clang-format. If it is in your path, 3 // it should just work out-of-the-box. Otherwise, you can set the full path, 4 // which will look like this: 5 // "binary": "/path/to/clang/bin/clang-format" 6 // Note that you can set this from within ST directly through the Command 7 // Palette. 8 9 "binary": "clang-format", 10 11 // We use the Google style by default. This can be selected from ST using 12 // the Command Palette. Choosing 'Custom' means that the settings will 13 // be loaded from the Sublime Text settings file (which is accessed 14 // from within ST through preferences. Choosing 'File' will look in the 15 // local directories from a clang-format settings file. See the clang-format 16 // documentation to see how this works. 17 18 "style": "Custom", 19 20 // Setting this to true will run the formatter on every save. If you want to 21 // only enable this for a given project, try checking out the package 22 // "Project-Specific". 23 24 "format_on_save": true, 25 26 // If format_on_save is set to true, ClangFormat checks if the current file 27 // has its syntax set to a language in the list below. If it is in the list, 28 // then the file will be formatted by ClangFormat. 29 30 "languages": ["C", "C++", "C++11", "JavaScript", "Objective-C", "Objective-C++"] 31}
-
自定义样式
屏蔽所有内容, 并添加以下内容1{ 2 "BasedOnStyle": "Microsoft", 3 "IndentWidth": "4", 4 "UseTab": "Never", 5 "BreakBeforeBraces": "Allman", 6 "ColumnLimit": "140", 7 "AccessModifierOffset": "-4", 8 "SortIncludes": false, 9 "AllowShortBlocksOnASingleLine": true, 10 "AllowShortIfStatementsOnASingleLine": true, 11 "AlignAfterOpenBracket": "Align", 12 "AllowShortFunctionsOnASingleLine": "Inline", 13 "PointerAlignment": "Right", 14}