How to prettify C++ in VS Code?

106 Views Asked by At

I am writing some C++ code inside my text editor VS Code.

So at some point, the code became hard to format/prettify the code manually.

I tried to use "format document",
But nothing happened, even with the C/C++ extension active.


I want something like Prettier extension but for C++, is it possible?


  • Is it possible that it follows almost the same principles/config of prettier (prettier only works in Javascript)?

note that:

  • I am using all sorts of capabilities of C++ like:
    • classes
    • templates
    • namespace
    • variables
    • includes
    • loops
    • pointers
1

There are 1 best solutions below

8
Laaouatni Anas On BEST ANSWER

You maybe searching about clang-format

Instructions (VS Code)

  1. install C/C++ extension (if you don't have it)

  2. create a file called .clang-format

Preferably should be inside the same directory as the C++ file

  1. copy and paste this code inside that file
---

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

---

since you told me that you want code that is formatted similarly to the configs of Prettier (that it used in js instead),
I tried, and here are some of the main advantages:

Configs inspired by Prettier

  1. positioning of brackets { and }
/* bad */
void foo(int bar)
{
int a;
}

/* good */
void foo(int bar) {
  int a;
};
  1. line length and wrapping
/* 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. namespace nesting
/* bad */
namespace Base 
{
class MyClass 
{
}
}

/* good */
namespace Base {
  class MyClass {};
}; // namespace Base
  1. class nesting
/* 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. short loops/statements written in only one line.
/* bad */
while(condition) {
  i++;
}

/* good */
while(condition) { i++; };
  1. consecutive variables prettified
/* 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. pointers
/* bad */
int* a;
int* b, * c, * d;

/* good */
int *a;
int *b, *c, *d;
  1. space instead of tabs

  2. semicolons everywhere C++ needs.

  3. remove useless empty spaces

/* 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. nested arrays formatting
/* 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},
};

and much more, and you can even customize it,
by following these docs: https://clang.llvm.org/docs/ClangFormatStyleOptions.html

try it, and hopefully it will solve your problem!