Code Format
按编程语言分类
ClangFormat
ClangFormat 是一个统一代码格式的工具,可以通过命令行使用,也可以结合各种 IDE ,适用于 C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C#,参照官网。
不同 ClangFormat 的版本支持的可配置项不同,所以较新的配置文件在旧版本中会有不识别的现象,不能多版本兼容。
在线预览 clang format 效果:链接
个人用-C ClangFormat
个人用-C++ ClangFormat
Prettier
Prettier 是前端目前常用的代码格式化工具,适用于 JS/TS/CSS/HTML/JSON/MD/MDX/YAML,参照官网。
在线预览 Prettier 效果:链接
个人用 Prettier
按工程分类
Postgresql-C-pgindent
代码在pg发布之前通过pgindent进行格式化,工程中进行格式化如:
src/tools/pgindent/pgindent --indent=src/tools/pg_bsd_indent/pg_bsd_indent src/pl/plisql/src/pl_subproc_function.c
个人常用格式
ClangFormat-C
# Clang-Format approximation of PostgreSQL pgindent style
BasedOnStyle: LLVM
# Indentation and tabs
IndentWidth: 4
TabWidth: 4
UseTab: ForIndentation
ContinuationIndentWidth: 4
IndentCaseLabels: true
# Line length
ColumnLimit: 79
# Braces and control statements
BreakBeforeBraces: Allman
BraceWrapping:
BeforeElse: true
BeforeCatch: true
AfterFunction: true
AfterControlStatement: true
AfterNamespace: true
AfterClass: true
AfterEnum: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
# Spaces
SpaceAfterCStyleCast: true
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceInEmptyParentheses: false
SpaceBeforeSquareBrackets: false
SpacesBeforeTrailingComments: 4
# Alignment and wrapping
AlignAfterOpenBracket: Align
AlignOperands: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignTrailingComments: false
BinPackArguments: false
BinPackParameters: false
# Pointers and references
DerivePointerAlignment: false
PointerAlignment: Right
ReferenceAlignment: Right
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakAfterDefinitionReturnType: true
# Preprocessor
IndentPPDirectives: None
SpaceAfterLogicalNot: false
# Comments
ReflowComments: false
CommentPragmas: ''
# Misc C/C++-relevant toggles
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
KeepEmptyLinesAtTheStartOfBlocks: false
# Includes
SortIncludes: false
IncludeBlocks: Preserve
ClangFormat-C++
Prettier