ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1131_使用clang-format进行代码格式排版

2022-02-07 21:59:18  阅读:243  来源: 互联网

标签:None false format true 1131 功能 clang 排版


使用clang-format进行代码格式排版

Grey

全部学习汇总: GitHub - GreyZhang/editors_skills: Summary for some common editor skills I used.

使用clang-format进行代码格式排版

前言

我自己写代码其实也经历了很多过程,前期的确是没有关注过什么代码风格或者排版。但是随着接触到的代码越来越多,发现没有一个统一良好的编码排版风格理解管理一个工程都是很有挑战的。于是,不仅仅从编码的定义风格上,从排版的布局上也开始有所注意。

经历

  • 其实很多编码工具本身就提供了自动对齐等功能,我用过的编辑器大部分都是有这样的功能的。
  • 有一段时间,曾经为究竟使用TAB还是space还纠结,但是其实这个解决方案很简单。用TAB,显示为space,万事大吉了。
  • 最初的排版,几乎是全部手动排版。简单的Source Insight或者Notepad++最基础的编辑功能就可以实现。
  • 继续往后,探索发现了emacs、VS Code、SI的自动排版功能。其中,默认配置之下的VS Code的效果以及风格最让我满意。但是,emacs的操作体验则是最好的。我简单看了VS Code的机理,使用了clang-format,因此到觉得这个功能很容易迁移到其他的编辑器。
  • 再后来,需要看大量的别人的代码,乱糟糟的风格很多时候让我觉得恶心(真的恶心,生理上想吐的那种),我觉得简单的编辑器进行一个文件的排版已经很难满足我的需要。探索了不容易找到的cb,用过不是特别满意的indent。虽说最大的问题点可能还是在我自己的使用上,但是我还是期待emacs或者VS Code上用到的功能做成脚本化。

探索

  • VS Code有多个排版工具,看了一下我自己的配置其实是clang的排版工具。之前知道这个还专门安装了clang,后来了解之后发现其实不必。
  1. CLANGFORMAT
  2. LIBFORMAT
  • 上面两个链接是这个工具的介绍文档,简单参考了一下,做了我自己的初步配置。关于最后的配置,我会在笔记最后做一个附加。几点小结以及功能特性倒是可以总结一下。

功能总结以及探索问题解决

  • 首先,先配置了一个缩进符号的使用。选择了空格而不是TAB,缩进统一为了4个空格。
  • 第二,大括号等,自动新换一行。
  • 第三,所有的函数都不折叠,在配置中对应的配置项目是短函数。
  • 第四,有一点很重要:大括号等配置的信息其实是需要有一个统一的配置项开启来支持的。条目为:BreakBeforeBraces: Custom
  • 第五,默认的处理,include的信息会有很大的变化。这个需要禁用include的排序功能。
  • 第六,可以采用配置文件的方式取代配置参数,这样的话其实一个文件可以对当前目录以及里面的子目录全都生效。这也是我期待的脚本功能的一个很重要的基础。

小结

这样,首先是先实现了一个基础的功能。后面,这个功能很容易进行校本化的处理。而针对emacs,其实可以直接把这个功能替代进去,相应的功能就可以做到完全统一了。

完整配置信息

— Language: Cpp

AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignConsecutiveMacros: None AlignConsecutiveAssignments: None AlignConsecutiveBitFields: None AlignConsecutiveDeclarations: None AlignEscapedNewlines: Right AlignOperands: Align AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortEnumsOnASingleLine: true AllowShortBlocksOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: None AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: Never AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: MultiLine AttributeMacros:

  • __capability

BinPackArguments: true BinPackParameters: true BraceWrapping: AfterCaseLabel: true AfterClass: true AfterControlStatement: true AfterEnum: true AfterFunction: true AfterNamespace: true AfterObjCDeclaration: true AfterStruct: true AfterUnion: true AfterExternBlock: false BeforeCatch: true BeforeElse: true BeforeLambdaBody: false BeforeWhile: true IndentBraces: false SplitEmptyFunction: true SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBinaryOperators: None BreakBeforeConceptDeclarations: true BreakBeforeBraces: Custom BreakBeforeInheritanceComma: false BreakInheritanceList: BeforeColon BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true ColumnLimit: 120 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false DisableFormat: false EmptyLineBeforeAccessModifier: LogicalBlock ExperimentalAutoDetectBinPacking: false FixNamespaceComments: true ForEachMacros:

  • foreach
  • Q_FOREACH
  • BOOST_FOREACH

StatementAttributeLikeMacros:

  • Q_EMIT

IncludeBlocks: Preserve IncludeCategories:

  • Regex: '^"(llvm|llvm-c|clang|clang-c)/' Priority: 2 SortPriority: 0 CaseSensitive: false
  • Regex: '(<|"(gtest|gmock|isl|json)/)' Priority: 3 SortPriority: 0 CaseSensitive: false
  • Regex: '.*' Priority: 1 SortPriority: 0 CaseSensitive: false

IncludeIsMainRegex: '(Test)?$' IncludeIsMainSourceRegex: '' IndentCaseLabels: false IndentCaseBlocks: false IndentGotoLabels: true IndentPPDirectives: None IndentExternBlock: AfterExternBlock IndentRequires: false 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 PenaltyIndentedWhitespace: 0 PointerAlignment: Right ReflowComments: true SortIncludes: false SortJavaStaticImport: Before SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements SpaceAroundPointerQualifiers: Default SpaceBeforeRangeBasedForLoopColon: true SpaceInEmptyBlock: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: false SpacesInConditionalStatement: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false SpaceBeforeSquareBrackets: false BitFieldColonSpacing: Both Standard: Latest StatementMacros:

  • Q_UNUSED
  • QT_REQUIRE_VERSION

TabWidth: 4 UseCRLF: false UseTab: Never WhitespaceSensitiveMacros:

  • STRINGIZE
  • PP_STRINGIZE
  • BOOST_PP_STRINGIZE
  • NS_SWIFT_NAME
  • CF_SWIFT_NAME

标签:None,false,format,true,1131,功能,clang,排版
来源: https://blog.csdn.net/grey_csdn/article/details/122815322

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有