当前位置: 首页 > 工具软件 > CP Editor > 使用案例 >

ubuntu vscode 使用clang-format and editor 插件序列化代码

毋举
2023-12-01

在使用vscode时, 可以加入插件,在写代码的时候自动格式化代码,对编码风格做一个自动化的处理,这样会使同一个部门使用同一种规格编码,在review代码时会很轻松。

这里使用一键化的shell脚本, 注意:以下三个文件需要在同一个目录下执行, 实际行为就是把.clang-format 和.editorconfig两个配置脚本放置到“~/”下。

code_format_vscode.sh :

#!/bin/bash

user_setting_path="$HOME/.config/Code/User/"
setting_name="settings.json"

whole_setting_path=$user_setting_path$setting_name
echo $whole_setting_path

# used to store .clang-format and .editorconfig
user_path="$HOME/"

#get the extension list on the computer
cmd_code_extension_list="code --list-extensions"
extension_list=`${cmd_code_extension_list}`
echo $extension_list

if [[ ! $extension_list =~ .*ms-vscode.cpptools.* ]]
then
    #install c/c++ extension
    cmd="code --install-extension ms-vscode.cpptools"
    ${cmd}
fi

if [[ ! $extension_list =~ .*EditorConfig.EditorConfig.* ]]
then
    #install EditorConfig.EditorConfig extension
    cmd="code --install-extension EditorConfig.EditorConfig"
    ${cmd}
fi

if [ ! -f "${user_path}.clang-format" ];then
    #copy clang-format configuration to ~/ directory, 
    #so that all vscode workspace could use it
    echo "copy clang-format to ${user_path}"
    cmd="cp .clang-format ${user_path}"
    ${cmd}
else
    echo "you already had a .clang-format file in $user_path directory."
    echo "please check if it is valid"

fi

if [ ! -f "${user_path}.editorconfig" ];then
    #copy editorconfig configuration to ~/ directory, 
    #so that all vscode workspace could use it
    echo "copy editorconfig to ${user_path}"
    cmd="cp .editorconfig ${user_path}"
    ${cmd}
else
    echo "you already had a .editorconfig file in $user_path directory."
    echo "please check if it is valid"
fi

# c/c++ clang-format
setting_prefix="{"
line1='"editor.formatOnType": true,'
line2='"editor.formatOnSave": true,'
line3='"C_Cpp.clang_format_fallbackStyle": "Google",'
line4='"C_Cpp.clang_format_style": "file",'
line5='"C_Cpp.clang_format_sortIncludes": true,'
line6='"C_Cpp.formatting": "Default",'
setting_postfix="}"

if [ ! -f "$whole_setting_path" ];then
    echo "new user setting"
    if [[ ! -d "$user_setting_path" ]]
    then
        echo "the user setting directory does not exist, building..."
        cmd="mkdir -p $user_setting_path"
        ${cmd}
    fi
    echo -e ${setting_prefix} >> ${whole_setting_path}
    echo -e ${line1} >> ${whole_setting_path}
    echo -e ${line2} >> ${whole_setting_path}
    echo -e ${line3} >> ${whole_setting_path}
    echo -e ${line4} >> ${whole_setting_path}
    echo -e ${line5} >> ${whole_setting_path}
    echo -e ${line6} >> ${whole_setting_path}
    echo -e ${setting_postfix} >> ${whole_setting_path}
else
    while read line
    do
        if [[ $line =~ '"C_Cpp.clang_format_fallbackStyle": "Google"' ]] ; then    
            echo "you already had a clang format configuration."
            echo "please check if the configuration file is what you want."
            return
        fi
    done < "$whole_setting_path";
    
    echo "add clang-format configuration into setting.json"
    sed -i "2i$line1" ${whole_setting_path}
    sed -i "3i$line2" ${whole_setting_path}
    sed -i "4i$line3" ${whole_setting_path}
    sed -i "5i$line4" ${whole_setting_path}
    sed -i "6i$line5" ${whole_setting_path}
    sed -i "7i$line6" ${whole_setting_path}
fi

echo "Done"

    
    
    

.clang-format:

{
BasedOnStyle: Google,
AccessModifierOffset: -4,
ConstructorInitializerIndentWidth: 2,
AlignEscapedNewlinesLeft: false,
AlignTrailingComments: true,
AllowAllParametersOfDeclarationOnNextLine: false,
AllowShortIfStatementsOnASingleLine: false,
AllowShortLoopsOnASingleLine: false,
AllowShortFunctionsOnASingleLine: None,
AllowShortLoopsOnASingleLine: false,
AlwaysBreakTemplateDeclarations: true,
AlwaysBreakBeforeMultilineStrings: false,
BreakBeforeBinaryOperators: false,
BreakBeforeTernaryOperators: false,
BreakConstructorInitializersBeforeComma: true,
BinPackParameters: true,
ColumnLimit: 80,
ConstructorInitializerAllOnOneLineOrOnePerLine: true,
DerivePointerBinding: false,
PointerBindsToType: true,
ExperimentalAutoDetectBinPacking: false,
IndentCaseLabels: true,
MaxEmptyLinesToKeep: 1,
NamespaceIndentation: None,
ObjCSpaceBeforeProtocolList: true,
PenaltyBreakBeforeFirstCallParameter: 19,
PenaltyBreakComment: 60,
PenaltyBreakString: 1,
PenaltyBreakFirstLessLess: 1000,
PenaltyExcessCharacter: 1000,
PenaltyReturnTypeOnItsOwnLine: 90,
SpacesBeforeTrailingComments: 2,
Cpp11BracedListStyle: true,
Standard:        Auto,
IndentWidth:     4,
TabWidth:        4,
UseTab:          Never,
IndentFunctionDeclarationAfterType: false,
SpacesInParentheses: false,
SpacesInAngles:  false,
SpaceInEmptyParentheses: false,
SpacesInCStyleCastParentheses: false,
SpaceAfterControlStatementKeyword: true,
SpaceBeforeAssignmentOperators: true,
ContinuationIndentWidth: 4,
SortIncludes: true,
SpaceAfterCStyleCast: false,
BreakBeforeBraces: Allman,
}

 

.editorconfig

# Configuration file for EditorConfig
# More information is available under http://EditorConfig.org

# Ignore any other files further up in the file system
root = true

# Configuration for all files
[*.{cc,cpp,h,hpp,yaml,cmake}]
# Enforce Unix style line endings (\n only)
end_of_line = lf
charset = utf-8
# Always end files with a blank line
insert_final_newline = true
# Force space characters for indentation
indent_style = space
# Always indent by 2 characters
indent_size = 4
# Remove whitespace characters at the end of line
trim_trailing_whitespace = true

 

 类似资料: