vscode 配置代码格式化工具calng-format,windows/linux都支持

news/2024/7/24 14:28:17

1、安装clang-format工具

linux (ubuntu)
sudo apt-get install clang-format-11
windows

下载clang-format.exe,并将此文件路径加入环境变量
下载地址或从官网:
https://download.csdn.net/download/niu_88/14946310

本人安装是clang-format 11版本,下面的内容也是针对11版本。

2、vscode安装插件

从插件扩展中安装clang-format,不然,vscode无法识别calng-format

在这里插入图片描述

3、格式化文件

创建 .clang-format 文件,并将此文件放入项目根目录
使用自己喜欢的代码风格格式或者使用下面格式(将内容添加到.clang-format 中)

---
Language:        Cpp
# BasedOnStyle:  LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands:   Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterCaseLabel:  false
  AfterClass:      true
  AfterControlStatement: Never
  AfterEnum:       false
  AfterFunction:   true
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  BeforeLambdaBody: false
  BeforeWhile:     false
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     0
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
  - LL_FOREACH
  - LL_FOREACH_SAFE
IncludeBlocks:   Preserve
IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
    SortPriority:    0
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
    SortPriority:    0
  - Regex:           '.*'
    Priority:        1
    SortPriority:    0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentWidth:     4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments:  true
SortIncludes:    false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard:        Latest
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
TabWidth:        4
UseCRLF:         false
UseTab:          Never
WhitespaceSensitiveMacros:
  - STRINGIZE
  - PP_STRINGIZE
  - BOOST_PP_STRINGIZE
...

4、vscode设置

链接clang-format工具,使用自定义的.clang-format文件
进入设置界面,按照以下方式操作,来选择Clang Format

在这里插入图片描述

设置自动保存时格式化

在这里插入图片描述

设置格式化文件还是格式化更改部分代码,按需设置

在这里插入图片描述

5、自此设置完毕

赶快去试试吧,可能可以。

http://www.niftyadmin.cn/n/1903677.html

相关文章

ListView+CheckBox实现全选 单击效果

在网上也找了一些案例,但都是用Map来实现的.我的是把对象绑定到当前控件上.代码稍微简洁. main布局文件:main.xml <?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android&quo…

如何在mac上安装gitlab

2019独角兽企业重金招聘Python工程师标准>>> 安装docker 下载地址&#xff1a; https://docs.docker.com/docker-for-mac/install/ 下载下来是一个dmg的安装包&#xff0c;直接安装就可以了。 网络问题 嗯&#xff0c;在国内做开发必须谈网络问题&#xff0c;安装好…

团队-象棋游戏-最终程序

托管平台地址:https://gitee.com/546840006/11111/blob/master/ChineseChess.zip 小组名称:Narcissu 小组成员合照:无 程序运行方法:右键点击使用浏览器运行 程序运行示例及运行结果: 其他附加内容:无转载于:https://www.cnblogs.com/qihao10086/p/7966665.html

ubuntu 20在虚拟机桥接模式下设置固定ip

设置配置文件&#xff0c;不同系统文件名可能不同&#xff1a; /etc/netplan/01-network-manager-all.yaml 修改文件内容&#xff0c;自己修改对应ip地址和网关&#xff1a; # Let NetworkManager manage all devices on this system #network: network:ethernets:enp0s3:dhcp…

【互动出版网】10月新书榜活动

【互动出版网】10月新书榜活动参加互动出版网写书评&#xff0c;得积分&#xff0c;换赠书活动。现在即可参加活动哦。活动详细方案以及规则请点击此链接详细了解》》》 来自 “ ITPUB博客 ” &#xff0c;链接&#xff1a;http://blog.itpub.net/16566727/viewspace-748112/&a…

NIO学习二、NIO的基本使用

这是作为学习NIO的总结&#xff0c;如有不对&#xff0c;请大佬指出。 一、基本操作&#xff08;这些操作不会的时候查文档就行&#xff09; 从一个buffer中读写到另一个bufferTestpublic void bufferTest1(){try {RandomAccessFile readAccessFilenew RandomAccessFile("…

iOS 5编程入门经典(第3版)

《iOS 5编程入门经典(第3版)》基本信息原书名&#xff1a;Beginning iOS 5 Application Development作者&#xff1a; [美] Wei-Meng Lee [作译者介绍]译者&#xff1a; 宋梅出版社&#xff1a;清华大学出版社ISBN&#xff1a;9787302299943上架时间&#xff1a;2012-10-31出版…

cmake例程学习

代码结构 代码 外层CMakeLists.txt cmake_minimum_required(VERSION 3.5)project(test)option(FANGFA1 "fangfa 1" ON)if (FANGFA1 STREQUAL "ON")# 方法1add_library(MyMath INTERFACE)add_subdirectory(math)message("方法1") else()# 方法2…