AStyle代码格式化工具使用
文章目录
1、说明
工具下载
- 直接下载下载“AStyle_3.1 可执行程序”即可使用,若想查看开源代码以及网官文档可下载“AStyle_3.1 源码、官方文档”。
其他
- 测试环境win10 + Source Insight4.0
- 文件类型*.c 、*.h
格式化完成后代码展示
未格式化前代码
#include<stdio.h>
#include<math.h>
#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
void main()
{
void shellSort(int array[],int n,int t);
char* foo1;
char & foo2;
string ^s1;
int array[MAXNUM],i;
for(i=0;i<MAXNUM;i++)
scanf("%d",&array[i]);
shellSort(array,MAXNUM,(int)(log(MAXNUM+1)/log(2)));
for(i=0;i<MAXNUM;i++)
printf("%d ",array[i]);
printf("\n");}
void shellInsert(int array[],int n,int dk)
{
int i,j,temp;
for(i=dk;i<n;i++)
{
temp=array[i];
for(j=i-dk;(j>=i%dk)&&array[j]>temp;j-=dk)
array[j+dk]=array[j];
if(j!=i-dk)
array[j+dk]=temp;
}
}
int dkHibbard(int t,int k)
{
return (int)(pow(2,t-k+1)-1);
}
void shellSort(int array[],int n,int t)
{
void shellInsert(int array[],int n,int dk);
int i=0;
do{i++;
shellInsert(array,n,dkHibbard(t,i));
}
while(i<t);
}
void seitch_test(){
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
}
void Foo(bool bar1,
bool bar2)
{
isLongFunction(BAR1,
BAR2);
isLongVariable = foo1
|| foo2的;
}
格式化后代码(linux风格)
#include<stdio.h>
#include<math.h>
#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
void main()
{
void shellSort(int array[], int n, int t);
char *foo1;
char &foo2;
string ^s1;
int array[MAXNUM], i;
for(i = 0; i < MAXNUM; i++) {
scanf("%d", &array[i]);
}
shellSort(array, MAXNUM, (int)(log(MAXNUM + 1) / log(2)));
for(i = 0; i < MAXNUM; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
void shellInsert(int array[], int n, int dk)
{
int i, j, temp;
for(i = dk; i < n; i++) {
temp = array[i];
for(j = i - dk; (j >= i % dk) && array[j] > temp; j -= dk) {
array[j + dk] = array[j];
}
if(j != i - dk) {
array[j + dk] = temp;
}
}
}
int dkHibbard(int t, int k)
{
return (int)(pow(2, t - k + 1) - 1);
}
void shellSort(int array[], int n, int t)
{
void shellInsert(int array[], int n, int dk);
int i = 0;
do {
i++;
shellInsert(array, n, dkHibbard(t, i));
} while(i < t);
}
void seitch_test()
{
switch (foo) {
case 1:
a += 1;
break;
case 2: {
a += 2;
break;
}
}
}
void Foo(bool bar1,
bool bar2)
{
isLongFunction(BAR1,
BAR2);
isLongVariable = foo1
|| foo2;
}
格式化后代码(allman风格)
#include<stdio.h>
#include<math.h>
#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
void main()
{
void shellSort(int array[], int n, int t);
char *foo1;
char &foo2;
string ^s1;
int array[MAXNUM], i;
for(i = 0; i < MAXNUM; i++)
{
scanf("%d", &array[i]);
}
shellSort(array, MAXNUM, (int)(log(MAXNUM + 1) / log(2)));
for(i = 0; i < MAXNUM; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
void shellInsert(int array[], int n, int dk)
{
int i, j, temp;
for(i = dk; i < n; i++)
{
temp = array[i];
for(j = i - dk; (j >= i % dk) && array[j] > temp; j -= dk)
{
array[j + dk] = array[j];
}
if(j != i - dk)
{
array[j + dk] = temp;
}
}
}
int dkHibbard(int t, int k)
{
return (int)(pow(2, t - k + 1) - 1);
}
void shellSort(int array[], int n, int t)
{
void shellInsert(int array[], int n, int dk);
int i = 0;
do
{
i++;
shellInsert(array, n, dkHibbard(t, i));
} while(i < t);
}
void seitch_test()
{
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
}
void Foo(bool bar1,
bool bar2)
{
isLongFunction(BAR1,
BAR2);
isLongVariable = foo1
|| foo2;
}
2、Source insight增加插件
步骤一:
- 下载AStyle工具,解压后,找到
AStyle.exe
可执行文件,将其复制到Source insight安装路径下(路径可任意)。
步骤二:
- 打开Source Insight,找到菜单栏中的
Tools -> Custiom Commands
并进入。(当前为Sourice Insight4.0,其余版本自行找到 Custiom Commands选项)
步骤三:
- 如下图
-
步骤四:
- 如下图
- 找到
AStyle.exe
可执行文件
步骤四:
- 往后添加选项:详情见3、推荐命令。
步骤五:
- 增加快捷键:点击
Key Assignments
- 点击
Assign New Key..
后会显示弹框,此时通过键盘输入自己想要的快捷键即可。
验收:
- 打开一份代码,直接输入刚才设置的快捷键,(例如我设置的为 ctrl+alt+f),因为Source Insighe检测变化有延迟,过一会后才会弹出文本变化是否读取弹框。点击yes 或yes all后可看到对应变化。
如图:
3、推荐命令
命令格式
- [OPTIONS]为扩展命令,SourceFilePath为文件地址加文件名
Astyle.exe [OPTIONS] SourceFilePath1 SourceFilePath2 SourceFilePath3
- [OPTIONS]选项推荐
- Linux风格(-n去掉旧文件)
-n -A10 -s4 -xV -S -xU -xW -f -p -xg -k3 -c
- allmain风格
-A1 -s4 -xV -S -xU -xW -f -p -xg -k3 -c
命令格式
- sourice insight 命令例举
%f :在source insight中表示当前文件
"E:\Program Files (x86)\Source Insight 4.0\AStyle.exe" -A10 -s4 -xV -S -xU -xW -f -p -xg -k3 -c %f
- windows终端 命令例举
进入到AStyle.exe 所在路径,直接运行,
AStyle.exe -A1 -s4 -xV -S -xU -xW -f -p -xg -k3 -c b.c
4、常用参数说明
注意事项
- 注意大小写。
- ‘/’标识‘或’的意思。
- 该参数说明只列举常用参数,想看详细参数请下载“Style_3.1 源码、官方文档”。
常用风格
- –style=1tbs / --style=otbs / -A10
linux风格,如果流程控制语句(if、for、while)底下只有一句,且没加大括号时,会自动补全。缩进格式如下:
int Foo(bool isBar)
{
if (isFoo) {
bar();
return 1;
} else {
return 0;
}
}
- –style=allman / --style=bsd / --style=break / -A1
风格如下所示,大括号在下方,不会加上流程控制语句(if、for、while)底下只有一句时的大括号。
int Foo(bool isBar)
{
if (isBar)
{
bar();
return 1;
}
else
return 0;
}
标签选项
- –indent=spaces=# / -s# (‘#’可为2-20,不设置时默认为4)
如下所示(‘.’表示空格)
void Foo() {
....if (isBar1
............&& isBar2)
........bar();
}
大括号选项
- –attach-closing-while / -xV
将’do-while’语句的结束’while’附加到右大括号。
do
{
bar();
++x;
}
while (x == 1);
变形为:
do
{
bar();
++x;
} while (x == 1);
缩进选项
- –indent-switches / -S
case X:从’ case X:‘标题 缩进’ '块。未包含在块中的case语句不缩进。
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
变形为:
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
- –indent-after-parens / -xU
缩进,而不是对齐,包含开头的行后面的连续行paren’(‘或赋值’=’。这包括函数定义和声明以及返回语句。可以使用以下缩进连续选项修改缩进。对于显示比例字体的编辑器,可能首选选项。
void Foo(bool bar1,
bool bar2)
{
isLongFunction(BAR1,
BAR2);
isLongVariable = foo1
|| foo2的;
}
变形为:
void Foo(bool bar1,
bool bar2)
{
isLongFunction(BAR1,
BAR2);
isLongVariable = foo1
|| foo2的;
}
- –indent-preproc-block / -xW
将括号级为0的预处理程序块缩进并立即在命名空间内。将缩进的内容有限制。方法,类,数组等中的块不会缩进。包含大括号或多行定义语句的块不会缩进。如果没有此选项,则预处理程序块不会缩进。
#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
变形为:
#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif
- –indent-preproc-define / -w
缩进以反斜杠结尾的多行预处理器定义。应与–convert-tabs一起使用以获得正确的结果。做得很好,但不能在混淆的预处理器定义中执行奇迹。如果没有此选项,预处理程序语句将保持不变。
#define Is_Bar(arg,a,b) \
(Is_Foo((arg), (a)) \
|| Is_Foo((arg), (b)))
变形为:
#define Is_Bar(arg,a,b) \
(Is_Foo((arg), (a)) \
|| Is_Foo((arg), (b)))
- max-continuation-indent=# / -M
设置最大的#缩进空格续行。的 #表示的列数,并且必须不小于40或大于120。如果未设置任何值,则将使用默认值40。此选项将阻止延续线向右延伸太远。设置较大的值将允许代码进一步向右扩展。
fooArray[] = { red,
green,
blue };
fooFunction(barArg1,
barArg2,
barArg3);
变形为:
fooArray[] = { red,
green,
blue };
fooFunction(barArg1,
barArg2,
barArg3);
填充选项
- –break-blocks / -f
在标题栏周围填空行(例如’if’,‘for’,‘while’…)
isFoo = true;
if (isFoo) {
bar();
} else {
anotherBar();
}
isBar = false;
变形为:
isFoo = true;
if (isFoo) {
bar();
} else {
anotherBar();
}
isBar = false;
- –pad-oper / -p
在运算符周围插入空格填充。这也将填充逗号。如果可能,任何行尾注释都将保留在原始列中。请注意,unpad没有选项。填充后,它们会保持填充状态。
if (foo==2)
a=bar((b-c)*a,d--);
变形为:
if (foo == 2)
a = bar((b - c) * a, d--);
- –pad-comma / -xg
逗号后插入空格填充。如果使用pad-oper,则不需要这样做。如果可能,任何行尾注释都将保留在原始列中。请注意,unpad没有选项。填充后,它们会保持填充状态。
if (isFoo(a,b))
bar(a,b);
变形为:
if (isFoo(a, b))
bar(a, b);
- –align-pointer=name / -k3
指针跟随变量(K1 跟随类型,k2在中间)。
char* foo1;
char & foo2;
string ^s1;
变形为:
char *foo1;
char &foo2;
string ^s1;
格式化选项
-
–convert-tabs / -c
tab转换成空格(每个tab转换空格数由 -s (缩进)控制) -
–max-code-length=# / -xC#
果代码超过# 字符,则选项将断开一行。有效值为50到200.没有逻辑条件的行将在逻辑条件(||,&&,+ ,- ,…),逗号,paren,分号或空格上中断。
if (thisVariable1 == thatVariable1 || thisVariable2 == thatVariable2 || thisVariable3 == thatVariable3)
bar();
变形为:
if (thisVariable1 == thatVariable1
|| thisVariable2 == thatVariable2
|| thisVariable3 == thatVariable3)
bar();