Clang-Format 不缩进头文件中的模板函数
Clang-Format doesn't indent template functions in header files
我遇到了这个问题,clang 格式没有正确缩进我的头文件。例如,我在 class 中有一个模板函数,我希望它的格式如下:
template <class T>
class Thing {
public:
T function() {
stuff();
return T;
}
}
我觉得这种缩进会自动发生,但事实并非如此。当我 运行 Clang-format 时,我的函数代码是这样缩进的:
template <class T>
class Thing {
public:
T function() {
stuff();
return T;
}
}
如何让我的头文件中的代码像顶部文件一样格式化,而不是底部文件?
下面是我的 .clang 格式文件:
BasedOnStyle: LLVM
AccessModifierOffset: -4
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
IndentCaseLabels: false
IndentWidth: 4
Language: Cpp
NamespaceIndentation: All
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 2000
PenaltyBreakString: 3000
PenaltyBreakFirstLessLess: 1000
PenaltyExcessCharacter: 100000
PenaltyReturnTypeOnItsOwnLine: 10000
PointerAlignment: Left
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInParentheses: false
Standard: Cpp11
UseTab: ForIndentation
我在 this online clang format generator 中打开了您的配置文件并查找了适用于您的示例代码的设置。
这个看起来怎么样?
BasedOnStyle: LLVM
AccessModifierOffset: '-2'
AllowShortFunctionsOnASingleLine: Inline
BreakBeforeBraces: Custom
BreakConstructorInitializers: AfterColon
ColumnLimit: '80'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '2'
IndentWidth: '2'
Language: Cpp
NamespaceIndentation: All
PenaltyBreakBeforeFirstCallParameter: '0'
PenaltyBreakComment: '2000'
PenaltyBreakFirstLessLess: '1000'
PenaltyBreakString: '3000'
PenaltyExcessCharacter: '100000'
PenaltyReturnTypeOnItsOwnLine: '10000'
PointerAlignment: Left
SpaceBeforeParens: ControlStatements
Standard: Cpp11
UseTab: Never
我想也许 UseTab: Never
或 AccessModifierOffset: '-2'
是相关设置。 IndentWidth:
和 AccessModifierOffset
之间似乎也有关系,后者比前者大,缩进搞砸了。
如果这些不是您要查找的设置,zed0 是一种易于使用的资源,可用于创建人们可以接受的 clang 格式文件。
我遇到了这个问题,clang 格式没有正确缩进我的头文件。例如,我在 class 中有一个模板函数,我希望它的格式如下:
template <class T>
class Thing {
public:
T function() {
stuff();
return T;
}
}
我觉得这种缩进会自动发生,但事实并非如此。当我 运行 Clang-format 时,我的函数代码是这样缩进的:
template <class T>
class Thing {
public:
T function() {
stuff();
return T;
}
}
如何让我的头文件中的代码像顶部文件一样格式化,而不是底部文件?
下面是我的 .clang 格式文件:
BasedOnStyle: LLVM
AccessModifierOffset: -4
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerAlignment: false
IndentCaseLabels: false
IndentWidth: 4
Language: Cpp
NamespaceIndentation: All
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 2000
PenaltyBreakString: 3000
PenaltyBreakFirstLessLess: 1000
PenaltyExcessCharacter: 100000
PenaltyReturnTypeOnItsOwnLine: 10000
PointerAlignment: Left
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInParentheses: false
Standard: Cpp11
UseTab: ForIndentation
我在 this online clang format generator 中打开了您的配置文件并查找了适用于您的示例代码的设置。
这个看起来怎么样?
BasedOnStyle: LLVM
AccessModifierOffset: '-2'
AllowShortFunctionsOnASingleLine: Inline
BreakBeforeBraces: Custom
BreakConstructorInitializers: AfterColon
ColumnLimit: '80'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '2'
IndentWidth: '2'
Language: Cpp
NamespaceIndentation: All
PenaltyBreakBeforeFirstCallParameter: '0'
PenaltyBreakComment: '2000'
PenaltyBreakFirstLessLess: '1000'
PenaltyBreakString: '3000'
PenaltyExcessCharacter: '100000'
PenaltyReturnTypeOnItsOwnLine: '10000'
PointerAlignment: Left
SpaceBeforeParens: ControlStatements
Standard: Cpp11
UseTab: Never
我想也许 UseTab: Never
或 AccessModifierOffset: '-2'
是相关设置。 IndentWidth:
和 AccessModifierOffset
之间似乎也有关系,后者比前者大,缩进搞砸了。
如果这些不是您要查找的设置,zed0 是一种易于使用的资源,可用于创建人们可以接受的 clang 格式文件。