以不区分大小写的方式包含 clang 格式的订单

问题描述 投票:0回答:1

我正在尝试配置 .clang-format。我的目的是让它保留我的包含组,因为它们在代码中,但按字母顺序对它们进行排序。我设法接近我想要的

IncludeBlocks: Preserve
,但它以区分大小写的方式包含在每个块中:

这是我应用格式后得到的结果

#include "A1.h"
#include "B2.h"
#include "a2.h"
#include "b1.h"

这就是我想要实现的目标

#include "A1.h"
#include "a2.h"
#include "b1.h"
#include "B2.h"

我正在使用 clang-format 版本 10.0。我的 .clang 格式文件,以防相关:

---
# Brompton's Clang Format file v0.0.1
#
# Changelog:
# v0.0.1 (25/9/2020):
# - First version of this file


# Base it on Google's Standard, with 4 spaces as indentation
BasedOnStyle: Google
IndentWidth: '4'
Standard: Cpp11
UseTab: Never

# Settings for C/C++
Language: Cpp
# 120 chars per line, reflow comments so they stay within limits
ColumnLimit: '120'
ReflowComments: 'true'

# Short includes alphabetically. Will respect "include groups"
SortIncludes: 'true'
IncludeBlocks: Preserve

# These control when to align text under certain conditions
# Align arguments after an open bracket ( '(', '[', '{'), on statements that are too long for a single line
AlignAfterOpenBracket: Align
# Align adjacent macros and variables, for enhanced readability.
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
# Align the '\' on scaped newlines, because it looks neater
AlignEscapedNewlines: Left

# These keep statements that could go on a single line to be collapsed. They take more space, but are more readable
# ... Code Blocks { ... }
AllowShortBlocksOnASingleLine: 'false'
# ... Switch cases
AllowShortCaseLabelsOnASingleLine: 'false'
# ... short functions, like getters and setters
AllowShortFunctionsOnASingleLine: Empty
# ... single line ifs
AllowShortIfStatementsOnASingleLine: Never
# ... single line loops
AllowShortLoopsOnASingleLine: 'false'

# Provides a more compact view when a function parameters or arguments take more than one line
BinPackArguments: 'true'
BinPackParameters: 'true'

# Indent cases on a switch, instead of leaving them at the same level than the switch statement.
IndentCaseLabels: 'true'
# Indent preprocessor the same way than code
IndentPPDirectives: BeforeHash
IndentWrappedFunctionNames: 'true'
NamespaceIndentation: All

# Put the pointer operator next to the type, instead of next to the variable name:
PointerAlignment: Left

# All about those extra spaces...
# ... remove spaces after an open-curly brace and before a close-curly brace
Cpp11BracedListStyle: 'true'
# ... space before a list, when used to initialise an object
SpaceBeforeCpp11BracedList: 'true'
# ... logical not (!) next to expression
SpaceAfterLogicalNot: 'false'
# ... space between the '=' on assignments
SpaceBeforeAssignmentOperators: 'true'
# ... space after and before the parentheses in a C-style cast.
SpacesInCStyleCastParentheses: 'false'
# ... spaces inside () and []
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
# ... spaces before a ( or a [, but only on control statements.
SpaceBeforeParens: 'ControlStatements'

...

谢谢!

c++ clang-format
1个回答
3
投票

这是从 LLVM 12 开始添加的,并随 Clang 13 一起发布,有点令人困惑。引用 LLVM 12/Clang 13 的发行说明 [强调我的]:

clang 格式

选项

SortIncludes
已从
bool
更新为
enum
,其中 向后兼容性。除了前面的
true
/
false
状态(现在
CaseSensitive
/
Never
),添加了第三个状态 (
CaseInsensitive
) 这会导致按字母顺序排序,并将大小写用作 决胜局。

// Never (previously false)
#include "B/A.h"
#include "A/B.h"
#include "a/b.h"
#include "A/b.h"
#include "B/a.h"

// CaseSensitive (previously true)
#include "A/B.h"
#include "A/b.h"
#include "B/A.h"
#include "B/a.h"
#include "a/b.h"

// CaseInsensitive
#include "A/B.h"
#include "A/b.h"
#include "a/b.h"
#include "B/A.h"
#include "B/a.h"

最新的 Clang 格式文档已更新,并包含

CaseSensitive
的示例使用:

IncludeCategories (std::vector<IncludeCategory>)

[...]

每个正则表达式都可以使用字段标记为区分大小写

CaseSensitive
,默认情况下不是。

要在 .clang-format 文件中进行配置,请使用:

IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
    SortPriority:    2
    CaseSensitive:   true
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
  - Regex:           '<[[:alnum:].]+>'
    Priority:        4
  - Regex:           '.*'
    Priority:        1
    SortPriority:    0
© www.soinside.com 2019 - 2024. All rights reserved.