我正在我的文本编辑器 VS Code 中编写一些 C++ 代码。
因此,在某些时候,代码变得很难手动格式化/美化代码。
我尝试使用“格式化文档”,
但即使 C/C++ 扩展处于活动状态,也没有发生任何事情。
我想要类似 Prettier 扩展的东西,但是对于 C++,有可能吗?
注意:
Clang-Format
扩展
.clang-format
的文件(最好与 c++ 文件位于同一目录中)---
Language: Cpp
DisableFormat: false
RemoveSemicolon: false
RemoveBracesLLVM: false
SpaceInEmptyBlock: false
CompactNamespaces: false
SpaceBeforeCaseColon: false
SpaceAfterLogicalNot: false
SpaceBeforeSquareBrackets: false
SpacesInContainerLiterals: false
SpaceAfterTemplateKeyword: false
KeepEmptyLinesAtTheStartOfBlocks: false
InsertBraces: true
ReflowComments: true
IndentRequires: true
IndentCaseLabels: true
BreakStringLiterals: true
FixNamespaceComments: true
IndentRequiresClause: true
SpaceAfterCStyleCast: true
IndentAccessModifiers: true
IndentWrappedFunctionNames: true
SpaceBeforeInheritanceColon: true
AllowShortLoopsOnASingleLine: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
AllowShortCaseLabelsOnASingleLine: true
SpaceBeforeRangeBasedForLoopColon: true
SortIncludes: CaseInsensitive
IncludeBlocks: Regroup
AlignOperands: AlignAfterOperator
SpaceBeforeParens: Custom
RemoveParentheses: MultipleParentheses
IndentPPDirectives: AfterHash
NamespaceIndentation: All
BreakInheritanceList: AfterComma
SortUsingDeclarations: LexicographicNumeric
SpaceAroundPointerQualifiers: After
RequiresExpressionIndentation: Keyword
EmptyLineBeforeAccessModifier: LogicalBlock
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
PointerAlignment: Right
QualifierAlignment: Left
ReferenceAlignment: Right
AlignArrayOfStructures: Right
UseTab: Never
SpacesInAngles: Never
BreakBeforeBinaryOperators: None
EmptyLineAfterAccessModifier: Never
SeparateDefinitionBlocks: Always
AllowShortBlocksOnASingleLine: Always
IndentWidth: 2
ShortNamespaceLines: 0
MaxEmptyLinesToKeep: 1
ContinuationIndentWidth: 2
SpacesBeforeTrailingComments: 1
ColumnLimit: 60
SpaceBeforeParensOptions:
AfterPlacementOperator: false
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterControlStatements: true
BraceWrapping:
AfterEnum: false
AfterClass: false
AfterUnion: false
BeforeElse: false
BeforeWhile: false
AfterStruct: false
BeforeCatch: false
AfterFunction: false
AfterCaseLabel: false
AfterNamespace: false
AfterExternBlock: false
SplitEmptyRecord: false
SplitEmptyFunction: false
SplitEmptyNamespace: false
AfterControlStatement: Never
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossComments: true
AlignCaseColons: true
AcrossEmptyLines: true
AlignTrailingComments:
Kind: Always
OverEmptyLines: 2
---
既然你告诉我你想要格式类似于 Prettier 配置的代码(它在 js 中使用),
我尝试过,以下是一些主要优点:
{
和 }
/* bad */
void foo(int bar)
{
int a;
}
/* good */
void foo(int bar) {
int a;
};
/* bad */
bool arr[] = { true, true, true, false, true, false, true, true, false, true, true, true, true, true, true, true, true, false, false, false, true, true };
/* good */
bool arr[] = {
false, false, true, true, true, true, true,
true, true, true, true, true, true, true};
/* bad */
namespace Base
{
class MyClass
{
}
}
/* good */
namespace Base {
class MyClass {};
}; // namespace Base
/* bad */
class MyClass {
private:
int a;
int b;
public:
int c;
void bar() {};
}
/* good */
class MyClass {
private:
int a;
int b;
public:
int c;
void bar() {};
}
/* bad */
while(condition) {
i++;
}
/* good */
while(condition) { i++; };
/* bad */
int a = 10;
double b = 20;
long c = 30;
char d = "a"
int efg = 40;
/* good */
int a = 10;
double b = 20;
long c = 30;
char d = "a"
int efg = 40;
/* bad */
int* a;
int* b, * c, * d;
/* good */
int *a;
int *b, *c, *d;
空格而不是制表符
C++ 需要的地方都用分号。
删除无用的空白空间
/* bad */
void foo(int bar) {
std::cout << bar << std::endl;
std::cout << 123 << std::endl;
};
/* good */
void foo(int bar) {
std::cout << bar << std::endl;
std::cout << 123 << std::endl;
};
/* bad */
int foo[3][3] = {{1,2,33},{444,5555,6},{7,88,99999}};
/* good */
int foo[3][3] = {
{ 1, 2, 33},
{444, 5555, 6},
{ 7, 88, 99999},
};
还有更多,您甚至可以自定义它,
按照以下文档操作:https://clang.llvm.org/docs/ClangFormatStyleOptions.html
尝试一下,希望它能解决您的问题!