概述
简介
简单的代码格式化
void EXTI3_IRQHandler(void)
{
uint32_t err = 0;
/* DOWN button pushed => Decrease Volume/Headphone Enable -------------*/
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
/* Clear the interrupt source pending */
EXTI_ClearITPendingBit(EXTI_Line3);
/* If the Codec is PLAYING => Decrease Volume*/
if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING)
{
/* Increase the audio codec digital volume */
I2S_CODEC_ControlVolume(VolumeDirection_LOW, VOLStep);
/* Update the LCD display */
LCD_Update(VOL);
}
else /* If the Codec is PAUSED => Headphone Enable */
{
/* Update the LCD display */
LCD_Update(PLAY);
/* Enable the Headphone output and reinitialize all devices */
err = I2S_CODEC_SpeakerHeadphoneSwap(OutputDevice_HEADPHONE, AUDIO_FILE_ADDRESS);
/* Error message display if failure */
if (err != 0)
{
LCD_DisplayError(err);
}
}
}
}
这段代码是取自STM32官方的一个ADPCM编解码DEMO,首先声明,这段代码并不是不规范,只是我看了不爽而已,因为他的缩进只有两个空格,现在让他变成四个空格的缩进,注意这里是四个空格,不是TAB。
astyle --style=ansi main.c
void EXTI3_IRQHandler(void)
{
uint32_t err = 0;
/* DOWN button pushed => Decrease Volume/Headphone Enable -------------*/
if (EXTI_GetITStatus(EXTI_Line3) != RESET)
{
/* Clear the interrupt source pending */
EXTI_ClearITPendingBit(EXTI_Line3);
/* If the Codec is PLAYING => Decrease Volume*/
if (GetVar_AudioPlayStatus() == AudioPlayStatus_PLAYING)
{
/* Increase the audio codec digital volume */
I2S_CODEC_ControlVolume(VolumeDirection_LOW, VOLStep);
/* Update the LCD display */
LCD_Update(VOL);
}
else /* If the Codec is PAUSED => Headphone Enable */
{
/* Update the LCD display */
LCD_Update(PLAY);
/* Enable the Headphone output and reinitialize all devices */
err = I2S_CODEC_SpeakerHeadphoneSwap(OutputDevice_HEADPHONE, AUDIO_FILE_ADDRESS);
/* Error message display if failure */
if (err != 0)
{
LCD_DisplayError(err);
}
}
}
}
这样就变成了四个空格的缩进,其实这是ANSI格式规范,类似的还GNU规范,执行命令:
astyle --style=gnu stm32f10x_it.c
万一,格式化后代码编译出错了,你还可以恢复,每执行一次格式化的命令,都会生成一个对应.orgi的原始文件,这个文件就是原始代码,例如上面的例子就会生成一个stm32f10x_it.c.orig文件,我们compare一下看看:
缩进选项
astyle --indent=spaces=2 stm32f10x_it.c
同样,使用TAB缩进执行命令
astyle --indent=tab stm32f10x_it.c
其他比较有用的开关
(1) -f
在两行不相关的代码之间插入空行,如import和public class之间、public class和成员之间等;
(2) -p
在操作符两边插入空格,如=、+、-等。
如:int a=10*60;
处理后变成int a = 10 * 60;
(3) -P
在括号两边插入空格。另,-d只在括号外面插入空格,-D只在里面插入。
如:MessageBox.Show ("aaa");
处理后变成MessageBox.Show ( "aaa" );
(4) -U
移除括号两边不必要的空格。
如:MessageBox.Show ( "aaa" );
处理后变成MessageBox.Show ("aaa");
(5) -V
第一步:点击“工具”菜单
第二步:点击“外部工具”
第三步:配置并保存
在对话框中点击“添加”,如图填入各项。其中参数填写 --style=ansi $(ItemFileName)$(ItemExt)
可以勾选“使用输出窗口”,这样将不会显示黑色的命令窗口。相关信息都会显示在Visual Studio中。
经过上面设置之后,只需点击该菜单项就可以将当前文档格式化成ansi风格。如果你想要其它风格,可以自行设置参数。
值得注意的是在低版本的Visual Studio中,默认设置运行外部程序不会保存当前文档。这样的话如果在未保存的情况下运行该命令,未保存部分将会丢失。这个可以通过设置一个选项来解决。Visual Studio 6.0中:Options -> Editor -> Save Options -> Save before running tools 将该项勾选即可。我已经验证,在Visual Studio 2005中不用担心这类问题,可以放心使用。但是作为一个好习惯,我仍然建议你随时保存你的工作,尤其是做这种大幅度改动之前,甚至应该对源代码进行Check in操作。不知道Check in是什么?没关系,过几天我还会写一篇关于代码控制的文章,应该可以解决你的疑惑。
(2) 在source insight中使用,参考http://blog.csdn.net/gw_cs/article/details/7927735
Astyle完整的帮助说明
命令行中输入 astyle -h
E:My DocumentsDownloadsSTM32F10x_AN2931_FW_V2.0.0ProjectADPCMsrc>astyle -h
Artistic Style 2.03
Maintained by: Jim Pattee
Original Author: Tal Davidson
Usage : astyle [options] Source1.cpp Source2.cpp [...]
astyle [options] < Original > Beautified
When indenting a specific file, the resulting indented file RETAINS the
original file-name. The original pre-indented file is renamed, with a
suffix of ".orig" added to the original filename.
Wildcards (* and ?) may be used in the filename.
A 'recursive' option can process directories recursively.
By default, astyle is set up to indent C/C++/C#/Java files, with four
spaces per indent, a maximal indentation of 40 spaces inside continuous
statements, a minimum indentation of eight spaces inside conditional
statements, and NO formatting options.
Option's Format:
----------------
Long options (starting with '--') must be written one at a time.
Short options (starting with '-') may be appended together.
Thus, -bps4 is the same as -b -p -s4.
Default options file:
---------------------
Artistic Style looks for a default options file in the
following order:
1. The contents of the ARTISTIC_STYLE_OPTIONS environment
variable if it exists.
2. The file called .astylerc in the directory pointed to by the
HOME environment variable ( i.e. $HOME/.astylerc ).
3. The file called astylerc in the directory pointed to by the
USERPROFILE environment variable ( i.e. %USERPROFILE%astylerc ).
If a default options file is found, the options in this file
will be parsed BEFORE the command-line options.
Long options within the default option file may be written without
the preliminary '--'.
Bracket Style Options:
----------------------
--style=allman OR --style=ansi OR --style=bsd
OR --style=break OR -A1
Allman style formatting/indenting.
Broken brackets.
--style=java OR --style=attach OR -A2
Java style formatting/indenting.
Attached brackets.
--style=kr OR --style=k&r OR --style=k/r OR -A3
Kernighan & Ritchie style formatting/indenting.
Linux brackets.
--style=stroustrup OR -A4
Stroustrup style formatting/indenting.
Stroustrup brackets.
--style=whitesmith OR -A5
Whitesmith style formatting/indenting.
Broken, indented brackets.
Indented class blocks and switch blocks.
--style=banner OR -A6
Banner style formatting/indenting.
Attached, indented brackets.
Indented class blocks and switch blocks.
--style=gnu OR -A7
GNU style formatting/indenting.
Broken brackets, indented blocks.
--style=linux OR -A8
Linux style formatting/indenting.
Linux brackets, minimum conditional indent is one-half indent.
--style=horstmann OR -A9
Horstmann style formatting/indenting.
Run-in brackets, indented switches.
--style=1tbs OR --style=otbs OR -A10
One True Brace Style formatting/indenting.
Linux brackets, add brackets to all conditionals.
--style=pico OR -A11
Pico style formatting/indenting.
Run-in opening brackets and attached closing brackets.
Uses keep one line blocks and keep one line statements.
--style=lisp OR -A12
Lisp style formatting/indenting.
Attached opening brackets and attached closing brackets.
Uses keep one line statements.
Tab Options:
------------
default indent option
If no indentation option is set, the default
option of 4 spaces per indent will be used.
--indent=spaces=# OR -s#
Indent using # spaces per indent. Not specifying #
will result in a default of 4 spaces per indent.
--indent=tab OR --indent=tab=# OR -t OR -t#
Indent using tab characters, assuming that each
indent is # spaces long. Not specifying # will result
in a default assumption of 4 spaces per indent.
--indent=force-tab=# OR -T#
Indent using tab characters, assuming that each
indent is # spaces long. Force tabs to be used in areas
AStyle would prefer to use spaces.
--indent=force-tab-x=# OR -xT#
Allows the tab length to be set to a length that is different
from the indent length. This may cause the indentation to be
a mix of both spaces and tabs. This option sets the tab length.
Indentation options:
--------------------
--indent-classes OR -C
Indent 'class' blocks, so that the inner 'public:',
'protected:' and 'private: headers are indented in
relation to the class block.
--indent-switches OR -S
Indent 'switch' blocks, so that the inner 'case XXX:'
headers are indented in relation to the switch block.
--indent-cases OR -K
Indent case blocks from the 'case XXX:' headers.
Case statements not enclosed in blocks are NOT indented.
--indent-namespaces OR -N
Indent the contents of namespace blocks.
--indent-labels OR -L
Indent labels so that they appear one indent less than
the current indentation level, rather than being
flushed completely to the left (which is the default).
--indent-preprocessor OR -w
Indent multi-line #define statements.
--indent-col1-comments OR -Y
Indent line comments that start in column one.
--min-conditional-indent=# OR -m#
Indent a minimal # spaces in a continuous conditional
belonging to a conditional header.
The valid values are:
0 - no minimal indent.
1 - indent at least one additional indent.
2 - indent at least two additional indents.
3 - indent at least one-half an additional indent.
The default value is 2, two additional indents.
--max-instatement-indent=# OR -M#
Indent a maximal # spaces in a continuous statement,
relative to the previous line.
The valid values are 40 thru 120.
The default value is 40.
Padding options:
--------------------
--break-blocks OR -f
Insert empty lines around unrelated blocks, labels, classes, ...
--break-blocks=all OR -F
Like --break-blocks, except also insert empty lines
around closing headers (e.g. 'else', 'catch', ...).
--pad-oper OR -p
Insert space padding around operators.
--pad-paren OR -P
Insert space padding around parenthesis on both the outside
and the inside.
--pad-paren-out OR -d
Insert space padding around parenthesis on the outside only.
--pad-first-paren-out OR -xd
Insert space padding around first parenthesis in a series on
the outside only.
--pad-paren-in OR -D
Insert space padding around parenthesis on the inside only.
--pad-header OR -H
Insert space padding after paren headers (e.g. 'if', 'for'...).
--unpad-paren OR -U
Remove unnecessary space padding around parenthesis. This
can be used in combination with the 'pad' options above.
--delete-empty-lines OR -xd
Delete empty lines within a function or method.
It will NOT delete lines added by the break-blocks options.
--fill-empty-lines OR -E
Fill empty lines with the white space of their
previous lines.
--align-pointer=type OR -k1
--align-pointer=middle OR -k2
--align-pointer=name OR -k3
Attach a pointer or reference operator (*, &, or ^) to either
the operator type (left), middle, or operator name (right).
To align the reference separately use --align-reference.
--align-reference=none OR -W0
--align-reference=type OR -W1
--align-reference=middle OR -W2
--align-reference=name OR -W3
Attach a reference operator (&) to either
the operator type (left), middle, or operator name (right).
If not set, follow pointer alignment.
Formatting options:
-------------------
--break-closing-brackets OR -y
Break brackets before closing headers (e.g. 'else', 'catch', ...).
Use with --brackets=attach, --brackets=linux,
or --brackets=stroustrup.
--break-elseifs OR -e
Break 'else if()' statements into two different lines.
--add-brackets OR -j
Add brackets to unbracketed one line conditional statements.
--add-one-line-brackets OR -J
Add one line brackets to unbracketed one line conditional
statements.
--keep-one-line-blocks OR -O
Don't break blocks residing completely on one line.
--keep-one-line-statements OR -o
Don't break lines containing multiple statements into
multiple single-statement lines.
--convert-tabs OR -c
Convert tabs to the appropriate number of spaces.
--close-templates OR -xy
Close ending angle brackets on template definitions.
--max-code-length=# OR -xC#
--break-after-logical OR -xL
max-code-length=# will break the line if it exceeds more than
# characters. The valid values are 50 thru 200.
If the line contains logical conditionals they will be placed
first on the new line. The option break-after-logical will
cause the logical conditional to be placed last on the
previous line.
--mode=c
Indent a C or C++ source file (this is the default).
--mode=java
Indent a Java source file.
--mode=cs
Indent a C# source file.
Other options:
--------------
--suffix=####
Append the suffix #### instead of '.orig' to original filename.
--suffix=none OR -n
Do not retain a backup of the original file.
--recursive OR -r OR -R
Process subdirectories recursively.
--exclude=####
Specify a file or directory #### to be excluded from processing.
ignore-exclude-errors OR -i
Allow processing to continue if there are errors in the exclude=###
options. It will display the unmatched excludes.
ignore-exclude-errors-x OR -xi
Allow processing to continue if there are errors in the exclude=###
options. It will NOT display the unmatched excludes.
--errors-to-stdout OR -X
Print errors and help information to standard-output rather than
to standard-error.
--preserve-date OR -Z
The date and time modified will not be changed in the formatted file.
--verbose OR -v
Verbose mode. Extra informational messages will be displayed.
--formatted OR -Q
Formatted display mode. Display only the files that have been formatted.
--quiet OR -q
Quiet mode. Suppress all output except error messages.
--lineend=windows OR -z1
--lineend=linux OR -z2
--lineend=macold OR -z3
Force use of the specified line end style. Valid options
are windows (CRLF), linux (LF), and macold (CR).
Command Line Only:
------------------
--options=####
Specify an options file #### to read and use.
--options=none
Disable the default options file.
Only the command-line parameters will be used.
--ascii OR -I
The displayed output will be ascii characters only.
--version OR -V
Print version number.
--help OR -h OR -?
Print this help message.
最后
以上就是粗犷小甜瓜为你收集整理的代码格式化工具AStyle介绍的全部内容,希望文章能够帮你解决代码格式化工具AStyle介绍所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复