vscode clang-format插件的使用

摘要:
对于代码的格式,不同的人有不同的风格,在clang-format插件的基础上,可以自定义自己的代码风格。

对于代码的格式,不同的人有不同的风格,在clang-format插件的基础上,可以自定义自己的代码风格。

clang-format.exe位置(~/.vscode/extensions/ms-vscode.cpptools-0.26.1/LLVM/bin/clang-format.exe)

vscode下使用clang-format:

1.将.clang-format文件拷贝到对应的工程目录下:

vscode clang-format插件的使用第1张

2.在打开的源代码下执行Alt + shift + F 即可格式化源代码

vscode clang-format插件的使用第2张

命令行下格式化指定文件:

./clang-format.exe --assume-filename=d:/ojenv/.clang-format -i d:/ojenv/src/main.c #用自定义配置格式化代码

./clang-format.exe -style=Webkit -i d:/ojenv/src/main.c #用内置配置格式化代码,这里是 Webkit

Clang-format内置的几种基础风格:

LLVM、Google、Chromium、Mozilla、WebKit、Microsoft

命令行导出特定风格的配置文件(以Webkit为例)

./clang-format.exe -dump-config -style=Webkit >.clang-format

以下是我导出的配置文件(以Webkit为基础,修改了行宽120swith语句缩进函数参数自动对齐三个特性):

---
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: WebKit
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
RawStringFormats:
- Delimiter: pb
Language: TextProto
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

免责声明:文章转载自《vscode clang-format插件的使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇elementui table 表格前三行第三列数据标红Vue 获取自定义属性的值下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

VSCode编译CMake工程报错c++: internal compiler error: Killed (program cc1plus)

现象 使用VSCode+CMake构建C++工程时只能编译Release版本,无法编译Debug版本。 报错提示c++: internal compiler error: Killed (program cc1plus) 解决 编译过程中内存不足 方案1:增加内存 如果在虚拟机运行Linux可以增加虚拟机的分配内存或增大swap空间 具体方法可以参考这篇文...

Auto.js的初次使用——在VSCode中使用

最近双十一大家都在集猫币,盖楼,但是每天刷任务太浪费时间了。被推荐了一个脚本可以自动刷任务,很是好奇。于是想要了解一下Auto.js 一、vscode启动Auto.js 1.vscode里安装auto.js插件 2.ctrl + shift + p调出命令面板      二、连接手机操作 1、ipconfig查看自己本机ip 2、手机打开Auto.js侧...

移动端css、Js优处理

CSS 篇 0.5px细线 移动端 H5 项目越来越多,设计师对于 UI 的要求也越来越高,比如 1px 的边框。在高清屏下,移动端的 1px 会很粗。 那么为什么会产生这个问题呢?主要是跟一个东西有关,DPR(devicePixelRatio) 设备像素比,它是默认缩放为 100%的情况下,设备像素和 CSS 像素的比值。目前主流的屏幕 DPR=2(...

display:box,按比列划分,水平均分,及垂直等高

一、按比例划分 <div class="test"> <p id="p1">Hello</p> <p id="p2">W3School</p> </div> <style> .test{ display:-moz-box; display:-webkit-box; di...

移动端页面隐藏滚动条

h5解决移动端滚动条问题: html,body{ 100%; height: 100%; overflow: scroll;} html::-webkit-scrollbar, body::-webkit-scrollbar{0px;height:0px;} body{margin:0;}...

Vscode 无法加载 pip 下载好的模块

设置 vscode 配置文件 vscode 工作目录下的配置文件 /.vscode/settings.json 修改为: { "python.pythonPath": "/usr/local/opt/python@3.9/bin/python3.9", "python.autoComplete.extraPaths": ["/usr/local...