如何在 VS Code 中美化 C++?

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

我正在我的文本编辑器 VS Code 中编写一些 C++ 代码。

因此,在某些时候,代码变得很难手动格式化/美化代码。

我尝试使用“格式化文档”,
但即使 C/C++ 扩展处于活动状态,也没有发生任何事情。


我想要类似 Prettier 扩展的东西,但是对于 C++,有可能吗?


  • 它是否有可能遵循与 prettier 几乎相同的原理/配置(prettier 仅适用于 Javascript)?

注意:

  • 我正在使用 C++ 的各种功能,例如:
    • 课程
    • 模板
    • 命名空间
    • 变量
    • 包括
    • 循环
    • 指针
c++ format clang-format
1个回答
1
投票

使用说明(VS代码)

  1. 安装C/C++扩展(如果你没有)
  2. 安装
    Clang-Format
    扩展
  3. 创建一个名为
    .clang-format
    的文件(最好与 c++ 文件位于同一目录中)
  4. 将此代码复制并粘贴到该文件中(或直接从此 GitHub 存储库克隆它https://github.com/Laaouatni/CppFormatClangConfig
---

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 中使用),
我尝试过,以下是一些主要优点:

受 Prettier 启发的配置

  1. 支架
    {
    }
  2. 的定位
/* bad */
void foo(int bar)
{
int a;
}

/* good */
void foo(int bar) {
  int a;
};
  1. 线长和绕线
/* 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};
  1. 命名空间嵌套
/* bad */
namespace Base 
{
class MyClass 
{
}
}

/* good */
namespace Base {
  class MyClass {};
}; // namespace Base
  1. 类嵌套
/* 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() {};
}
  1. 仅用一行编写的短循环/语句。
/* bad */
while(condition) {
  i++;
}

/* good */
while(condition) { i++; };
  1. 连续变量美化
/* 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;
  1. 指针
/* bad */
int* a;
int* b, * c, * d;

/* good */
int *a;
int *b, *c, *d;
  1. 空格而不是制表符

  2. C++ 需要的地方都用分号。

  3. 删除无用的空白空间

/* 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;
};
  1. 嵌套数组格式化
/* 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

尝试一下,希望它能解决您的问题!

© www.soinside.com 2019 - 2024. All rights reserved.