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

2023-05-16

在使用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

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu vscode 使用clang-format and editor 插件序列化代码 的相关文章

随机推荐

  • connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

    docker 守护进程未启动 xff0c 无法使用docker命令 span class token punctuation span root 64 grafana253 span class token punctuation span
  • Win10上Docker无法正常启动 出现install WSL2 kernel update的情况

    文章目录 一 情况描述 二 解决方法 2 1检查自己的Win10版本 2 2下载插件 三 最终效果 写在最后 一 情况描述 当装完docker之后 xff0c 系统提示Windows重新启动 xff0c 作者就重启系统准备使用docker
  • 【JVM规范】2.5.JVM运行时数据区 Run-Time Data Areas

    2 5 运行时数据区Run Time Data Areas JVM为程序执行定义了不同的运行时数据区 xff08 run time data areas xff09 一部分运行时数据区在JVM启动时创建 xff0c JVM退出时被销毁 另一
  • for in遇到的bug

    项目场景 xff1a 由于ie不兼容getElmentsByClassName获取元素 xff0c 所以我自己封装该方法 xff1a ie支持ID与TagName获取元素 问题描述 xff1a 提示 xff1a 这里描述项目中遇到的问题 x
  • 使用C#进行点对点通讯和文件传输(发送接收部分)

    上面介绍了通讯的基类 xff0c 下面就是使用那个类进行发送和接收的部分 xff1a 二 发送部分 xff1a 发送咱们使用了多线程 xff0c 可以同时进行多个任务 xff0c 比如发送文件 发送文本等 xff0c 互不影响 xff1a
  • 这些日子我读过的《java编程思想》

    来到这里实习的时候 xff0c 就准备多读几本书 xff0c 其中第一本就是 Java编程思想 xff0c 后续还会读一些 代码大全 算法导论 之类的书籍 不过这一次通过阅读 java编程思想 xff0c 我收获了实在太多了 xff0c 现
  • CMMI2.0和CMMI1.3有什么区别?

    CMMI资质认证想必已经有不少企业已经了解过了 xff0c 很多企业只了解到CMMI的基础知识 xff0c 却不明白CMMI也有不同的版本 xff0c CMMI1 3版本自动20年10月更改为2 0版本 xff0c 那CMMI的一个版本升级
  • CMMI2.0和1.3之间的区别有哪些?

    CMMI资质认证已经有很多年的历史了 xff0c CMMI的版本也发生了变化 xff0c 从最初的1 3版本演变成至今2 0版本 xff0c 现在的企业在办理的时候使用也都是2 0版本 xff0c 那么这两者之间的变化是什么 xff1f 区
  • Python str isalpha方法

    目录 描述 语法 举例 1 字符串中只包含字母 2 字符串包含数字 符号和字母 描述 isalpha函数检测字符串中是否只包含字母 如果全部是字母组成的字符串 xff0c 则返回True xff0c 否则返回False isalpha 函数
  • Caffe 完全安装指南(GPU) 上

    目录 0 写在前面1 Caffe依赖包安装1 1 ProtoBuffer1 2 Boost1 3 GFLAGS1 4 GLOG1 5 BLAS1 6 ZLIB1 7 HDF51 8 LMDB和LEVELDB1 9 Snappy1 10 Op
  • 质数因子的求解

    质因子 xff08 或质因数 xff09 在数论里是指能整除给定正整数的质数 根据算术基本定理 xff0c 不考虑排列顺序的情况 下 xff0c 每个正整数都能够以唯一的方式表示成它的质因数的乘积 下面求解某一个数的质因子的情况 1 分解为
  • 常见的液晶显示模块类型

    随着市场的发展 人们更加倾向于产品的人机交互对话功能 能够实现交互的方式很多 xff0c 作为对交互界面的各种显示器液晶液晶模块以其低功耗 易控制受到设计者的青睐 于是各种型号 功能的液晶模块涌入市场 xff0c 这就需要对其进行综合及比较
  • 如何在ubuntu系统中安装gnome界面

    首先 xff0c 你需要进入ubuntu系统 xff0c 然后打开终端 然后需要这样操作 xff1a 1 更新软件源 sudo apt get update sudo apt get upgrade y 2 安装Tasksel sudo a
  • 使用 podman 将容器作为 systemd 服务运行

    前置环境要求 xff1a 需要以 CRI O 作为容器运行时 安装 CRI O 及配置国内容器镜像加速器请参考 xff1a 使用 CRI O 容器引擎 本文介绍了如何使用 podman 初始化 systemd 服务 xff0c 以两种不同的
  • VNC远程桌面到linux,提示connection refused(10061)解决办法

    确认server端的VNC服务开启 xff0c service vncserver start xff0c 检测状态时ok的 ps ef grep vnc xff0c 来查看不是已经开启多个vnc连接 如果有多个vnc连接 xff0c 使用
  • nginx前端,tomcat后端服务器获取客户的真实IP,包括tomcat访问日志获取真实IP的配置

    在安装完以nginx 43 tomcat的WEB服务器 xff0c 使用默认的配置 xff0c 会导致服务器上的日志文件 xff0c 只有nginx日志能获取到客户的真实IP xff0c 而tomcat以及上面的JAVA WEB应用均不能正
  • 腾讯自研交换机系统优化之路

    一 Tencent NOS概述 SONiC is an open source network operating system based on Linux that runs on switches from multiple vend
  • vue-cli 插件开发补充

    官网地址 xff1a https cli vuejs org zh api地址 xff1a https cli vuejs org dev guide plugin api html Plugin API api version 64 vu
  • ubuntu 安装开发工具

    因为各种原因 又回到了Ubuntu系统 以后开始会陆续记些笔记 注意要点 选择ubuntu16 能自动识别新机型的物理硬件 集成了较新较全的驱动 比如网卡和声卡驱动都有 最主要是这两个 没有这两个就上不了网 听不到声音 解决很费时间 安装系
  • ubuntu vscode 使用clang-format and editor 插件序列化代码

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