one installation
1.1 Installation under windows
astyle is a code formatting tool, which can be conveniently formatted with the plug-in of vs code, and can also be run as a command line tool alone: Download address: https://sourceforge.net/projects/astyle/files/
1.2 VS plug-in installation
Configuration format format:
/* AStyle */
"astyle.additional_languages": [
"c",
"cpp",
"*.h",
],
"astyle.cmd_options": [
"--style=stroustrup", //GNU 风格格式和缩进
"--indent=spaces=4", // 缩进4个空格
"--indent-preproc-block", // 条件编译预处理缩进
"--pad-oper", //操作符两端插入一个空格
"--pad-header",
"--unpad-paren", // 刪除括号外多余的空格
"--max-continuation-indent=80 ",//最大行长度80
"--indent-col1-comments", // 注释和代码一起缩进
"--suffix=none",
"--align-pointer=name", // 指针符号等附在变量上 如 char *foo1;
"--lineend=linux", // 以linux话的格式显示
"--indent=tab=4",// TAB 转4个空格
"--convert-tabs", //TAB转换为空格
"--indent-switches",// switch case 缩进-S
"--break-one-line-headers",// if 语句一行分两行写
"--add-braces",// if 一行没有带括号的加括号
"--attach-return-type-decl", // 函数返回类型和函数名搞成一行
"--verbose",
"--unpad-paren", //移除括号两端多余空格
],
"astyle.executable": "D:\\AStyle_3.1_windows\\AStyle\\bin\\AStyle.exe",
"astyle.objective-c.enable": true,
1.3 Configuration under source insight
Select custom commands under Tools:
Create a new style command and paste the command into it:
Just click close. Order:
"D:\\AStyle_3.1_windows\\AStyle\\\bin\\AStyle.exe" -A10 -p -U -k3 -t4 -S -M80 -Y -xb -j -xf -xh -xW -n -H -L -c %f
Asytle.exe comes according to its own installation path.
set shortcut key
two run
2.1 Run under the plug-in
In Vscode, you can use the shortcut key shift+alt+f to format the code
2.2 Run under the command line
Command line execution parameter configuration:
"D:\\AStyle_3.1_windows\\AStyle\\\bin\\AStyle.exe" -A10 -p -U -k3 -t4 -S -M80 -Y -xb -j -xf -xh -xW -n -H -L
illustrate:
说明:
--pad-oper / -p 操作符前后插入空格
--unpad-paren / -U 删除括号内外额外空格。
--align-pointer=name / -k3 将指针或引用运算符(*、&或^)附加到变量名(右)
--indent=tab=4 / -t4 缩进设置,使用tab缩进,tab等于4空格
--indent-switches / -S 缩进“Switch”块
--max-continuation-indent=120 / -M120 设置最大缩进续行为120空格。最大值120
--indent-col1-comments / -Y 注释与代码一起缩进。
--break-closing-braces / -y 右页眉大括号始终与其他样式断开
--break-one-line-headers / -xb 将一行代码if (isFoo1) bar1();分成两行
--add-braces / -j 将大括号添加到未带括号的一行条件语句
--attach-return-type / -xf 和下面的一起用,将返回类型附加到函数定义
--attach-return-type-decl / -xh 将返回类型附加到函数声明
--indent-preproc-block / -xW #if一类的预处理缩进
2.3 Batch script running
Batch script under windows:
@echo off
for /R .\trunk %%f in (*.cpp;*.c;*.h) do "D:\\AStyle_3.1_windows\\AStyle\\\bin\\AStyle.exe" -A10 -p -U -k3 -t4 -S -M80 -Y -xb -j -xf -xh -xW -n -H -L "%%f"
echo "format ok"
echo. & pause
illustrate:
说明:
/R:表明遍历一个目录树,后面紧跟的路径,缺省为当前目录。
.\:当前目录
%f:找到的文件名的通配符
(*.c):表示所有以.c结尾的文件
do: 执行后面的语句,其中的%f会被替换成找到的*.c的文件名
-n 不要备份(默认有.orig 结尾的原文件)
-R 递归
作用:
从当前目录开始,查找所有以.c结尾的文件,包含子目录中的文件;然后交给astyle处理。
Batch script running under Linux:
dir=`pwd`
filelist=`find $dir -type f -name "*.c" -or -name "*.h"`
for file in $filelist
do
astyle -A10 -p -U -k3 -t4 -S -M80 -Y -xb -j -xf -xh -xW -n -H -L $file
done