Clang-Format是一个通用的代码排版插件,可用于多种语言,多种IDE

Clang-Format完整样式说明可查看官方文档:ClangFormatStyleOptions

Clang-Format常用样式如下


	#基于的样式模版: Google,Microsoft
	BasedOnStyle: Google
	
	
	#最大保留的空行数: Number
	MaxEmptyLinesToKeep: 1
	
	#每行最大显示的字符数: Number
	ColumnLimit: 120
	
	
	#大括号前是否换行: Attach,Allman
	BreakBeforeBraces: Attach
	
	#三目运算符前是否换行: Bool
	#true意味着运算符将换行显示在句首,否则显示在句尾
	BreakBeforeTernaryOperators: false
	
	#短函数是否换行: None,Empty,All
	AllowShortFunctionsOnASingleLine: Empty
	
	#简单的IF语句是否换行: Never,Always
	AllowShortIfStatementsOnASingleLine: Always
	
	#简单的Lambda语句是否换行: None,All,Empty
	AllowShortLambdasOnASingleLine: None
	
	#简单的循环语句是否显示在同一行: Bool
	AllowShortLoopsOnASingleLine: true
	
	#允许简单的CASE语句显示在同一行: Bool
	AllowShortCaseLabelsOnASingleLine: false
	
	#短语句块显示在同一行: Never,Always,Empty
	#此属性尚未支持,在Qt中是Bool型的,和官方文档有冲突
	AllowShortBlocksOnASingleLine: Always
	
	#函数声明参数过长时,全部参数显示到下一行: Bool
	#true意味着第一行将只显示括号,不显示任何参数
	AllowAllParametersOfDeclarationOnNextLine: true
	
	#函数调用参数过长时,全部参数显示到下一行: Bool
	#true意味着第一行将只显示括号,不显示任何参数
	AllowAllArgumentsOnNextLine: true
	
	#函数初始化列表过长时,全部初始化语句显示到下一行: Bool
	AllowAllConstructorInitializersOnNextLine: true
	
	#return语句后面的花括号是否换行: None,All
	AlwaysBreakAfterReturnType: None
	
	#语句过长时,运算符是否换行: None,All,NonAssignment
	#运算符换行意味这换行时,运算符放在句首,否则放在句尾
	BreakBeforeBinaryOperators: None
	
	#构造函数初始化列表换行风格: AfterColon
	BreakConstructorInitializers: AfterColon
	
	#继承类列表换行风格: AfterColon
	#只有当类列表超出单行显示字符数时,才会换行
	BreakInheritanceList: AfterColon
	
	#允许长字符串换行: Bool
	BreakStringLiterals: false
	
	#模板声明和方法列表是否同行: No,Yes,MultiLine
	AlwaysBreakTemplateDeclarations: No
	
	#函数声明参数列表,要么全部单行显示,要么全部同行显示: Bool
	BinPackParameters: true
	
	#函数调用参数列表,要么全部单行显示,要么全部同行显示: Bool
	BinPackArguments: true
	
	#构造函数初始化列表,要么全部单行显示,要么全部同行显示: Bool
	ConstructorInitializerAllOnOneLineOrOnePerLine: true
	
	#Java注解是否换行显示: Bool
	BreakAfterJavaFieldAnnotations: true
	
	#多级嵌套的命名空间,是否压缩至同行显示: Bool
	CompactNamespaces: false
	
	
	#不同行参数对齐方式: Align,AlwaysBreak,DontAlign
	AlignAfterOpenBracket: AlwaysBreak
	
	#是否对齐句尾的注释: Bool
	AlignTrailingComments: false
	
	#是否对齐句尾换行符: Left,Right,DontAlign
	AlignEscapedNewlines: Left
	
	#是否对齐操作数: Bool
	AlignOperands: true
	
	#指针运算符对齐方式: Left,Right,Middles
	PointerAlignment: Right
	
	
	#缩进占的空格数: Number
	IndentWidth: 4
	
	#访问权限控制符偏移量: Number
	#默认权限控制符和字段,都是离类定义一个缩进的偏移量
	AccessModifierOffset: -2
	
	#逐级调用时的缩进量: Number
	ContinuationIndentWidth: 4
	
	#构造函数初始化列表的缩进量: Number
	ConstructorInitializerIndentWidth: 0
	
	#是否缩进CASE关键字: Bool
	IndentCaseLabels: true
	
	#是否缩进GOTO标签: Bool
	IndentGotoLabels: false
	
	#预处理语句缩进规则: BeforeHash,AfterHash,None
	IndentPPDirectives: BeforeHash
	
	#行数声明过长时,下一行语句是否缩进: Bool
	IndentWrappedFunctionNames: true
	
	#命名空间多级嵌套时的缩进方式: All,None,Inner
	NamespaceIndentation: All
	
	
	#赋值运算符前后是否显示空格: Bool
	SpaceBeforeAssignmentOperators: true
	
	#行注释后和代码之间的空格数: Number
	SpacesBeforeTrailingComments: 2
	
	#重新调整注释: Bool
	ReflowComments: false
	
	#列表初始化时,列表前后是否显示空格: Bool
	Cpp11BracedListStyle: false
	
	#列表初始化时,大括号前是否显示空格: Bool
	SpaceBeforeCpp11BracedList: true
	
	#类型强转的括号后面显示空格: Bool
	SpaceAfterCStyleCast: true
	
	#template关键字后面显示空格: Bool
	SpaceAfterTemplateKeyword: true
	
	#空代码块中间是否显示空格: Bool
	SpaceInEmptyBlock: false
	
	#空括号中间是否显示空格: Bool
	SpaceInEmptyParentheses: false
	
	#泛型尖括号中间是否显示空格: Bool
	SpacesInAngles: false
	
	
	#include语句排序规则: Merge,Regroup,Preserve
	IncludeBlocks: Merge
	
	#是否对include语句进行排序: Bool
	SortIncludes: true
	
	#是否对using语句进行排序: Bool
	SortUsingDeclarations: true
	
	#是否使用Tab: Never,Always,ForIndentation,ForContinuationAndIndentation
	#如果设置为Never,将总是用空格来替换Tab
	UseTab: Never

实际使用中,由于插件版本和IDE不同,部分属性可能会不支持或选项类型不一致,需要自行调整

这里是一份现成的,供QtCreator使用的Clang-Format样式表,使用时记得手动去除换行和缩进


	BasedOnStyle: Google
	
	MaxEmptyLinesToKeep: 1
	
	ColumnLimit: 120
	
	BreakBeforeBraces: Attach
	
	BreakBeforeTernaryOperators: false
	
	AllowShortFunctionsOnASingleLine: Empty
	
	AllowShortIfStatementsOnASingleLine: Always
	
	AllowShortLambdasOnASingleLine: None
	
	AllowShortLoopsOnASingleLine: true
	
	AllowShortCaseLabelsOnASingleLine: false
	
	AllowShortBlocksOnASingleLine: true
	
	AllowAllParametersOfDeclarationOnNextLine: true
	
	AllowAllArgumentsOnNextLine: true
	
	AllowAllConstructorInitializersOnNextLine: true
	
	AlwaysBreakAfterReturnType: None
	
	BreakBeforeBinaryOperators: None
	
	BreakConstructorInitializers: AfterColon
	
	BreakInheritanceList: AfterColon
	
	BreakStringLiterals: false
	
	AlwaysBreakTemplateDeclarations: No
	
	BinPackParameters: true
	
	BinPackArguments: true
	
	ConstructorInitializerAllOnOneLineOrOnePerLine: true
	
	BreakAfterJavaFieldAnnotations: true
	
	CompactNamespaces: false
	
	AlignAfterOpenBracket: AlwaysBreak
	
	AlignTrailingComments: false
	
	AlignEscapedNewlines: Left
	
	AlignOperands: true
	
	PointerAlignment: Right
	
	IndentWidth: 4
	
	AccessModifierOffset: -2
	
	ContinuationIndentWidth: 4
	
	ConstructorInitializerIndentWidth: 0
	
	IndentCaseLabels: true
	
	IndentPPDirectives: BeforeHash
	
	IndentWrappedFunctionNames: true
	
	NamespaceIndentation: All
	
	SpaceBeforeAssignmentOperators: true
	
	SpacesBeforeTrailingComments: 2
	
	ReflowComments: false
	
	Cpp11BracedListStyle: false
	
	SpaceBeforeCpp11BracedList: true
	
	SpaceAfterCStyleCast: true
	
	SpaceAfterTemplateKeyword: true
	
	SpaceInEmptyParentheses: false
	
	SpacesInAngles: false
	
	IncludeBlocks: Merge
	
	SortIncludes: true
	
	SortUsingDeclarations: true
	
	UseTab: Never

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐