文章目录
自带格式化工具(单个文件格式化)
Source Insight 4.0已自带格式化代码的功能,在菜单Tools下有Reformat Source Code两个选项,一个是格式化代码命令,一个是格式化代码设置。
Reformat Source Code Options官方介绍:
https://www.sourceinsight.com/doc/v4/userguide/Manual/Command_Reference/Reformat_Source_Code_Options.htm
默认携带4组预设格式化配置、可根据上面的介绍修改/新增。
注:貌似对同一个文件连续使用此方式格式化代码,会连续出现多组空格。
调用Artistic Style格式化(单个文件格式化)
Artistic Style是C、c++、c++ /CLI、Objective - C、c#和Java编程语言的源代码缩进器、格式化器和美化器。也就是代码格式工具。
Artistic Style官方网站:http://astyle.sourceforge.net
下载地址:http://sourceforge.net/projects/astyle/files/astyle/
Source Insight 配置Artistic Style:
https://blog.csdn.net/shentz/article/details/80512751
注意使用我如下配置参数
--style=allman -p -s4 -k3 -W3 -S -L -M120 -y -xb -j -xq -xS -U %f
Artistic Style官网配置说明文档:http://astyle.sourceforge.net/astyle.html
注:
1. 通过这种方式格式化代码后,source insight不能通过Ctrl+z撤回操作。
2. 上面方式只能对单个文件进行代码格式化。
Astyle 一键格式化项目代码(windows下)
Astyle 一键格式化项目代码:https://www.cnblogs.com/kybs0/p/11310473.html
C/C++代码格式优化工具----astyle:https://www.cnblogs.com/zhaoshixin/archive/2011/12/02/2272076.html
以下bat文件,一键格式化,项目中所有c和cpp文件:
echo off & color 0A
::指定起始文件夹
set DIR="C:\Users\mayue6\Desktop\test_c"
echo DIR=%DIR%
for /R %DIR% %%f in (*.c;*.cpp;*.h) do (
C:\Users\mayue6\Desktop\AStyle_3.1_windows\AStyle\bin\Astyle.exe --style=allman -p -s4 -k3 -W3 -S -M120 -y -xb -j -xq -xS -U %%f
)
pause
配置:1.起始文件夹;2.Astyle.exe安装路径,注意不能文件夹路径不能有空格。
注意:1.windows下操作多个文件时速度慢;2.上面.bat文件运行若格式化文件会将原文件改为“原文件名.orig” 的格式,若需要删除 .orig文件可使用下面的脚本:
echo off & color 0A
::指定起始文件夹
set DIR="C:\Users\mayue6\Desktop\test_c"
echo DIR=%DIR%
for /R %DIR% %%f in (*.c;*.cpp;*.h) do (
C:\Users\mayue6\Desktop\AStyle_3.1_windows\AStyle\bin\Astyle.exe --style=allman -p -s4 -k3 -W3 -S -L -M120 -y -xb -j -xq -xS -U %%f
)
pause
Astyle 一键格式化项目代码(linux下)
用astyle格式化代码(linux):https://www.cnblogs.com/MikeZhang/archive/2012/07/27/useAstyleInLinux.html
linux Astyle 安装:http://astyle.sourceforge.net/install.html
1.下载AStyle_3.1_windows
2.解压进入AStyle_3.1_windows/AStyle目录
3.cmake ./
4.make
注:AStyle_3.1_windows编译使用的cmake版本要大于3.0以上版本
执行成功的话会在当前目录下生成 astyle
可执行文件。
命令批量格式化—当前目录及子目录:
for f in $(find . -name '*.c' -or -name '*.cpp' -type f); do /data1/mayue6/usr/bin/astyle --style=allman -p -s4 -k3 -W3 -S -M120 -y -xb -j -xq -xS -U $f; done
脚本格式化—codeFormatting.sh:
#! /bin/bash
for f in $(find . -name '*.c' -or -name '*.cpp' -or -name '*.h' -type f)
do
astyle --style=allman -p -s4 -k3 -W3 -S -M120 -y -xb -j -xq -xS -U $f
done
参考资料
https://blog.csdn.net/XieWinter/article/details/89888154
https://blog.csdn.net/Janepen/article/details/7022180?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-11.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-11.nonecase