Vim Code Formatting plugin Clang-format

Source: Internet
Author: User
Tags class definition function definition



Title:vim Code Formatting Plugin Clang-format
Date:2017-12-12 20:28:26
Tags:vim
Categories: Development Tools


Installing the Vim-clang-format Plugin


Project Address:
Vim-clang-format



If you are using Vundle to manage the Vim plugin, add a line directly to the.vimrc


Plugin 'vim-clang-format'


Run:PluginInstallto install plug-ins automatically


"Configuration
Let g:clang_format#auto_format_on_insert_leave=1 "Automatically format when exiting insert mode


Supported languages


    • C
    • C++
    • Objective-c
    • Javascript
    • Java
    • TypeScript
    • Protobuf


Need to install Clang-format beforehand


Clang-format
pacman -S clang-format


Configuration information


BasedOnStyle (string)
This style is used for all options without a specific configuration.
 
This option is only supported in the clang-format configuration (in the -style='{...}' and .clang-format files).
 
Possible values are:
 
LLVM: Styles that conform to the LLVM code standard
Google: Styles that match Google’s c++ style guide
Chromium: Styles that match the Chromium style guide
Mozilla: Styles that match the Mozilla Style Guide
WebKit: Styles that conform to WebKit's style guide
AccessModifierOffset (int)
Access the indentation of the modifier or stretch outward, such as public:.
AlignAfterOpenBracket (BracketAlignmentStyle)
If true, the parameters are aligned horizontally after a left parenthesis
This applies to parentheses, angle brackets, and medium (square) brackets.
Possible values are:
BAS_Align (in configuration: Align) Adjust parameters after the left parenthesis, for example:
someLongFunction(argument1,
                 Argument2);
 
BAS_DontAlign (in configuration: DontAlign) does not adjust, use ContinuationIndentWidth, for example:
someLongFunction(argument1,
    Argument2);
 
BAS_AlwaysBreak (in configuration: AlwaysBreak) always wraps after the left parenthesis, if the argument does not fit in a single line, for example:
someLongFunction(
    Argument1, argument2);
 
AlignConsecutiveAssignments (bool)
If true (true), adjust multiple lines continuously
This will adjust the allocation operator in successive lines. This will result in a format like this:
Int aaaa = 12;
Int b = 23;
Int ccc = 23;
 
AlignConsecutiveDeclarations (bool)
If true, calibrate consecutive statements.
 
This will calibrate the name of the statement for successive lines. This will result in a format like this:
Int aaaa = 12;
Float b = 23;
Std::string ccc = 23;
 
AlignEscapedNewlinesLeft (bool)
If true, the calibration is ignored as far as possible on the left side of the new line. Or put them on the far right of the column
 
AlignOperands (bool)
If true, the operands of the binary and ternary expressions are aligned horizontally.
Specifically, this aligns the operand of an expression with an operand of multiple rows that need to be split, for example:
Int aaa = bbbbbbbbbbbbbbb +
          Ccccccccccccccc;
 
AlignTrailingComments (bool)
If true, align the comments.
 
AllowAllParametersOfDeclarationOnNextLine (bool)
Even if BinPackParameters is false, it allows all parameters of a function declaration to be moved to the next line.
 
AllowShortBlocksOnASingleLine (bool)
Allows simple statement blocks to be placed in a single line.
For example, this allows the statement "if (a) { return; }" to be placed in a single line.
 
AllowShortCaseLabelsOnASingleLine (bool)
If true, the label will be placed on a separate line in a very short case.
 
AllowShortFunctionsOnASingleLine (ShortFunctionStyle)
Depending on the value, the statement "int f() { return 0; }" can be placed on a single line.
Possible values are:
SFS_None (in configuration: None) Never merge methods or functions into a single line.
SFS_Empty (in configuration: Empty) merges only empty functions.
SFS_Inline (in configuration: Inline) only merges methods or functions defined in the class. It means "empty".
SFS_All (in configuration: All) Merge all methods to accommodate a single line.
 
AllowShortIfStatementsOnASingleLine (bool)
If true, the statement "if (a) return;" can be placed on a single line.
 
AllowShortLoopsOnASingleLine (bool)
If true, the statement "while (true) continue;" can be placed on a single line.
 
AlwaysBreakAfterDefinitionReturnType (DefinitionReturnTypeBreakingStyle)
Used for function definition return type newline styles. This option is obsolete and is reserved for backward compatibility.
Possible values are:
DRTBS_None (in configuration: None) Automatically wraps after returning the type. PenaltyReturnTypeOnItsOwnLine will be taken into account.
DRTBS_All (in configuration: All) always wraps after the return type.
DRTBS_TopLevel (in configuration: TopLevel) always wraps after returning the top-level function of the type.
 
AlwaysBreakAfterReturnType (ReturnTypeBreakingStyle)
Used for function declaration return type newline styles.
Possible values are:
 
RTBS_None (in configuration: None) automatically wraps after returning the type. "PenaltyReturnTypeOnItsOwnLine" will be considered.
RTBS_All (in configuration: All) Always wraps after returning the type.
RTBS_TopLevel (in configuration: TopLevel) Always wraps after the return type at the top of the method.
RTBS_AllDefinitions (in configuration: AllDefinitions) always wraps after the return type in the method definition.
RTBS_TopLevelDefinitions (in configuration: TopLevelDefinitions) Always wraps after the return type defined at the top level.
 
AlwaysBreakBeforeMultilineStrings (bool)
If true, it always wraps before the multi-line literal string.
This flag means that it makes the case of multiple lines of strings in the file look more consistent. Therefore, if the string is wrapped by a "ContinuationIndentWidth" space, it will take effect at the beginning of the line.
 
AlwaysBreakTemplateDeclarations (bool)
If true, always wraps after the template declares "template<...>"
 
BinPackArguments (bool)
If false (false), the arguments to the function call are either on the same line or there will be one row on the same line.
 
BinPackParameters (bool)
If false (false), the function declaration or function-defined parameters will all be on the same line, or each line.
 
BraceWrapping (BraceWrappingFlags)
Control individual braces for line break events.
If "BreakBeforeBraces" is set to "BS_Custom", use this to specify how to handle each individual parenthesis. Otherwise, this is ignored.
The logo of the nested structure:
Bool AfterClass wraps the class definition.
Bool AfterControlStatement wraps the control statement (if/for/while/switch/..).
Bool AfterEnum wraps the enumeration definition.
Bool AfterFunction wraps the method definition.
Bool AfterNamespace wraps the namespace definition.
Bool AfterObjCDeclaration wraps the OC definition (@autoreleasepool, interfaces, ..).
Bool AfterStruct wraps the structure definition.
Bool AfterUnion wraps the community definition.
Bool BeforeCatch wraps before catch.
Bool BeforeElse Wraps before else.
Bool IndentBraces Indented braces.
 
BreakAfterJavaFieldAnnotations (bool)
Wrap after each comment in the JAVA file.
 
BreakBeforeBinaryOperators (BinaryOperatorStyle)
The method of wrapping a binary operator.
Possible values are:
BOS_None (in configuration: None) Wraps after the operator.
BOS_NonAssignment (in configuration: NonAssignment) Wraps before the operator is specified.
BOS_All (in configuration: All) Wraps before the operator.
 
BreakBeforeBraces (BraceBreakingStyle)
Used for braces to wrap styles.
Possible values are:
BS_Attach (in the configuration: Attach) always concatenates the braces with the context.
BS_Linux (in configuration: Linux) Like Attach, but wraps before a method, namespace, or a class-defined braces
BS_Mozilla (in configuration: Mozilla) Like Attach, but wraps before an enumeration, method, or record definition.
BS_Stroustrup (in configuration: Stroustrup) Like Attach, but wraps before method definition, catch, and else
BS_Allman (in configuration: Allman) always wraps before the braces.
BS_GNU (in configuration: GNU) always breaks before the parentheses and adds an extra level of indentation to the parentheses of the control statement, not the parentheses of classes, functions, or other definitions.
BS_WebKit (in configuration: WebKit) Like Attach, but wraps before the method.
BS_Custom (in configuration: Custom) Configure each individual braces in "BraceWrapping".
 
BreakBeforeTernaryOperators (bool)
If true, the ternary operator will be placed after the newline.
 
BreakConstructorInitializersBeforeComma (bool)
Always wrap the constructor initializer before the comma and the comma and colon.
 
BreakStringLiterals (bool)
When formatting, always wrap the literal string.
ColumnLimit (unsigned)
Limit the column.
A column with a limit of 0 means there is no column limit. In this case, clang-format will be cautious about the newline decision of the input line in the declaration, unless it contradicts other rules.
 
CommentPragmas (std::string)
A fixed expression that describes comments that have special meaning and should not be split into lines or otherwise changed.
 
ConstructorInitializerAllOnOneLineOrOnePerLine (bool)
If the constructor initializer does not fit on one line, put each initialization on a separate line.
 
ConstructorInitializerIndentWidth (unsigned)
Use the constructor to initialize the number of characters indented by the list.
 
ContinuationIndentWidth (unsigned)
The new line is indented in width.
 
Cpp11BracedListStyle (bool)
If true, the format braces list is best suited for the c++11 list.
 
Important differences: - There are no braces in the spaces. - There is no line break before the braces are closed. Indentation with continuation indentation, not with block indentation.
Fundamentally, the C++ 11 braces list is exactly the same as the function call format. If the braces list is followed by a name (such as a type or variable name), the format of clang-format is like the "{}" of the parentheses of a function that calls that name. If there is no name, a zero-length name is assumed.
 
DerivePointerAliGnment (bool)
If true, analyzes the alignment of "&" and "\*" in the most common formatted files. The pointeralignment is only reserved.
 
DisableFormat (bool)
Formatting is completely prohibited.
 
ExperimentalAutoDetectBinPacking (bool)
If true, clang-format detects function calls and definitions as one parameter per line.
Each call can be wrapped, one per line or undefined. If you are unsure, for example, on a single line, but need to make a decision, clang-format analyzes the file for other wrapped cases and corresponding actions.
Note: This is an experimental sign that may disappear or be renamed. Do not use in a configuration file. You are responsible for your own use.
 
ForEachMacros (std::vector<std::string>)
A macro should be interpreted as a foreach loop instead of as a function call vector.
These are macros of the expected form:
 
FOREACH(<variable-declaration>, ...)
  <loop-body>
In the .clang-format configuration file, this can be set to:
ForEachMacros: ['RANGES_FOR', 'FOREACH']
For example: BOOST_FOREACH.
 
IncludeCategories (std::vector<IncludeCategory>)
Regular expressions indicate that different #include categories are used for the #includes command.
These regular expressions match the file name of a file containing (including < > or "). The values belonging to the first matching regular expression are assigned, and #include is first based on increasing the number of categories and then alphabetizing in each category. Sort in order.
 
If the regular expressions do not match, int_max assigns a category. The main header reference of the source file automatically gets category 0. Therefore, it usually stays at the beginning of #include (http://llvm.org/docs/CodingStandards.html#include-style). However, if you have a head quote that always needs to be ranked first, you can also assign negative priorities.
In order to configure this in the .clang-format file, please use:
IncludeCategories:
  - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
    Priority: 2
  - Regex: '^(<|"(gtest|isl|json)/)'
    Priority: 3
  - Regex: '.\*'
    Priority: 1
 
IncludeIsMainRegex (std::string)
Specifies a commonly used expression that can include a regular expression in the file.
After guessing if #include is "main" include (specifying category 0, see above), use this regular expression to allow the header of the suffix to reference the source. Partial matching is complete, so say: - "" means any suffix, - "$" means no suffix
For example, if you configure "(test)?$", then .h will be treated as "main" included in a.cc and a_test.ccde.
 
IndentCaseLabels (bool)
Indent the case label level from the switch statement.
When an error occurs, the same indentation level is used as the switching statement. The body of the switch statement always shrinks the case label above one level.
 
IndentWidth (unsigned)
The number of columns used for indentation.
 
IndentWrappedFunctionNames (bool)
Indent if the function defines or declares the type of the package.
 
JavaScriptQuotes (JavaScriptQuoteStyle)
JavaScriptQuoteStyle uses a JavaScript string.
Possible values are:
JSQS_Leave (in the configuration: Leave) leaves the original parentheses of the string
JSQS_Single (in configuration: Single) always uses single brackets
JSQS_Double (in configuration: Double) always uses double brackets.
 
KeepEmptyLinesAtTheStartOfBlocks (bool)
If true, keep the starting blank line of the block.
 
Language (LanguageKind)
This format is for languages.
Possible values are:
LK_None (in configuration: None) not used
LK_Cpp (in configuration: Cpp) should be used in C, C++, ObjectiveC, ObjectiveC++.
LK_Java (in configuration: Java) should be used in Java.
LK_JavaScript (in configuration: JavaScript) should be used for JavaScript.
LK_Proto (in configuration: Proto) should be used for Protocol buffering (https://developers.google.com/protocol-buffers/).
LK_TableGen (in configuration: TableGen) should be used for TableGen code.
 
MacroBlockBegin (std::string)
An expression that matches a macro that is commonly used to start a block.
 
MacroBlockEnd (std::string)
An expression that matches a macro that is often used to end a block.
 
MaxEmptyLinesToKeep (unsigned)
The maximum number of consecutive blank lines.
 
NamespaceIndentation (NamespaceIndentationKind)
Used for indentation of namespaces.
Possible values are:
NI_None (in configuration: None) is not indented in the namespace.
NI_Inner (in configuration: Inner) is only indented in the internal namespace (nested in other namespaces).
NI_All (in configuration: All) is indented in all namespaces.
 
ObjCBlockIndentWidth (unsigned)
The number of characters taken in the OC block.
 
ObjCSpaceAfterProperty (bool)
Add a space after the @property in the OC. For example: use "@property (readonly)" instead of "@property(readonly)".
 
ObjCSpaceBeforeProtocolList (bool)
Add a space before the OC protocol list, for example: Use <Protocol> instead of <Protocol>.
 
PenaltyBreakBeforeFirstCallParameter (unsigned)
After calling the parenthesis "(", give a method call for a newline penalty.
 
PenaltyBreakComment (unsigned)
Penalties for each newline included in a comment.
 
PenaltyBreakFirstLessLess (unsigned)
Penalties for newlines before the first "<<".
 
PenaltyBreakString (unsigned)
Penalties for each newline in a string containing a literal.
 
PenaltyExcessCharacter (unsigned)
Penalties for each character's column limit.
 
PenaltyReturnTypeOnItsOwnLine (unsigned)
Put a method return type on the same line of the function.
 
PointerAlignment (PointerAlignmentStyle)
Pointer and reference to its way.
Possible values are:
PAS_Left (in configuration: Left) The pointer is left aligned.
PAS_Right (in configuration: Right) The pointer is right aligned.
PAS_Middle (in configuration: Middle) The pointer is aligned in the middle.
 
ReflowComments (bool)
If true, clang-format will attempt to reflow the layout.
 
SortIncludes (bool)
If true, clang-format will classify #includes.
 
SpaceAfterCStyleCast (bool)
If true, a space may be inserted after a C style description.
 
SpaceAfterTemplateKeyword (bool)
If true, insert a space after the "template" keyword.
 
SpaceBeforeAssignmentOperators (bool)
If false (false), remove the space before the assignment operator (=).
 
SpaceBeforeParens (SpaceBeforeParensOptions)
The case where spaces are defined before braces.
Possible values are:
SBPO_Never (in configuration: Never) Never precedes parentheses with spaces.
SBPO_ControlStatements (in configuration: ControlStatements) Add spaces only before the parentheses that control the declaration keyword (for/if/while···).
SBPO_Always (in configuration: Always) always precedes the parentheses with spaces, except those that are prohibited by grammar rules (in methods - like macro definitions) or when other style rules are defined (after unary operators, parentheses, etc. )
 
SpaceInEmptyParentheses (bool)
If true, a space may be inserted in "()".
 
SpacesBeforeTrailingComments (unsigned)
The number of spaces before a single line comment (// - comments).
This does not affect block comments (/* - comments) because they usually have different usage patterns and some special cases.
 
SpacesInAngles (bool)
If true, a space will be inserted in the middle of the <> of the template alignment column.
 
SpacesInCStyleCastParentheses (bool)
If true, a space will be inserted in the C style description.
 
SpacesInContainerLiterals (bool)
If true, spaces will be inserted in the literal container (for example, OC and Javascript arrays and dictionary literals).
 
SpacesInParentheses (bool)
If true, a space will be inserted before "(" and after ").
 
SpacesInSquareBrackets (bool)
If true, a space will be inserted after "[" and before "]".
 
Standard (LanguageStandard)
Formatted with this standard: for example: use A<A<int> > instead of A<A<int>> in LS_Cpp03
Possible values are:
LS_Cpp03 (in configuration: Cpp03) Use the Use C++03 unified syntax.
LS_Cpp11 (in configuration: Cpp11) uses features of C++11 (eg A<A<int>> instead of A<A<int> >).
LS_Auto (in configuration: Auto) Automatically checks based on input.
TabWidth (unsigned)
The number of columns used for tab stops.
 
UseTab (UseTabStyle)
The way tab characters are used in the result file.
Possible values are:
UT_Never (in configuration: Never) Never uses tabs.
UT_ForIndentation (in configuration: ForIndentation) Use tabs only for indentation.
UT_Always (in configuration: Always) When using tags, we need to fill in the blanks, at least from one tab stop to the next.


Configuration file Location~/.clang-format


My configuration file

#based on that configuration file
BasedOnStyle: LLVM
#指的的*的一个边
PointerAlignment: Right
#缩进Width
IndentWidth: 4
#Continuous blank lines keep a few lines
MaxEmptyLinesToKeep: 1
# Add a space after @property, \@property (readonly) instead of \@property(readonly).
ObjCSpaceAfterProperty: true
Indentation behind # OC block
ObjCBlockIndentWidth: 4
# Whether to allow short method single line
AllowShortFunctionsOnASingleLine: true
# Whether to allow short if a single line If true, if (a) return; can be placed on the same line
AllowShortIfStatementsOnASingleLine: true
#注对齐
AlignTrailingComments: true
# Alignment operator when wrapping
#AlignOperands: true
# brackets on both sides of the space []
SpacesInSquareBrackets: true
# Add spaces between the parentheses
SpacesInParentheses : false
#Multi-line declaration statement according to = alignment
AlignConsecutiveDeclarations: true
#Continuous assignment statements aligned with =
AlignConsecutiveAssignments: true
#等号的空间
SpaceBeforeAssignmentOperators: true
#容器的空间 such as OC dictionary
SpacesInContainerLiterals: true
#indentation
IndentWrappedFunctionNames: true
#在块从空线
KeepEmptyLinesAtTheStartOfBlocks: true
# Break the line by comma when the constructor is initialized, and align it with a colon
BreakConstructorInitializersBeforeComma: true
#function parameter wrap
AllowAllParametersOfDeclarationOnNextLine: true
#) Add spaces after the brackets
SpaceAfterCStyleCast: true
#tabKeyboard width
TabWidth: 4
UseTab: Never 


The vim is turned on and the code is automatically formatted when exiting edit mode



Vim Code Formatting plugin Clang-format


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.