使用astyle格式化iar keil工程代码

在进行嵌入式代码开发中,格式化的代码会减少代码提交时候的diff的区别,而且也更加便于阅读代码,通过在iar 或者keil中配置astyle,从而保证代码的一致性,是很好的编程习惯。

下边从两个方面分别描述如何在keil中,或者iar中设置从而进行代码格式化。

keil的设置

keil的代码格式化的设置

keil的astyle格式化的设置

按照下面进行设置。

keil的快捷键的设置

C:\Program Files\Astyle\astyle.exe

--style=allman --indent=spaces=4 --indent-switches --indent-namespaces --pad-oper --indent-labels --min-conditional-indent=0 --max-continuation-indent=40 --convert-tabs --indent-preproc-block --delete-empty-lines --squeeze-lines=2 --suffix=none !E

快捷键的设置

在这里我习惯于使用快捷键,Ctrl + Alt +W 进行设置。按照下面的步骤,进行设置即可。

在keil中直接使用vscode打开当前的.c文件

我们可以在keil中方便的进行打开vscode的设置,现在普通的ide,没有ai的辅助,使用快捷键进行打开vscode,然后使用mars 或者github的 copilet进行编程, 对于代码的编写是事半功倍的。

具体设置如下:

使用vscode快捷键的设置

iar 的相关的设置

iar的astyle的格式化的设置

打开IAR软件,选择Tools菜单下Configure Tools.

格式化代码的时候将光标定位在准备格式化的文件内部

Astyle Current File

C:\Program Files\AStyle\bin\AStyle.exe

--style=allman --indent=spaces=4 --indent-switches --indent-namespaces --pad-oper --indent-labels --min-conditional-indent=0 --max-continuation-indent=40 --convert-tabs --indent-preproc-block --delete-empty-lines --squeeze-lines=2 --suffix=none $FILE_PATH$
//--style=ansi -s4 -S -N -p -L -N -m0 -M40 --convert-tabs --suffix=.pre $CUR_DIR$
--delete-empty-lines 这个参数可以保持代码的一致性,但是可以根据需要,是否添加。

Iar的快捷键进行设置

按照下面的步骤进行设置。

在iar的ide中打开vscode进行编程

设置快捷键进行打开同样的设置。

通过这样进行设置,可以保证编程的统一性。

keil参考教程 。

iar参考教程。

sourceinsight 自动格式化整个目录的脚本

"C:\Program Files\AStyle\bin\AStyle.exe" --style=ansi -s4 -S -N -R ./*.c ./*.h -p -L -N -m0 -M40 --convert-tabs --suffix=.pre %f

如果需要对于整个代码的文件夹进行格式化,可以使用如下的批处理进行设置。

@echo off

set curdir=%~dp0
cd /d %curdir%

set astyle="C:\Program Files\AStyle\bin\AStyle.exe" 

for /r %~dp1%~nx1 %%a in (.) do %astyle% "%%a\*.c"   --style=allman --indent=spaces=4 --indent-switches --indent-namespaces --pad-oper --indent-labels --min-conditional-indent=0 --max-continuation-indent=40 --convert-tabs --indent-preproc-block --delete-empty-lines --pad-method-prefix --squeeze-lines=2 --suffix=none %f

for /r %~dp1%~nx1 %%a in (.) do %astyle% "%%a\*.h"   --style=allman --indent=spaces=4 --indent-switches --indent-namespaces --pad-oper --indent-labels --min-conditional-indent=0 --max-continuation-indent=40 --convert-tabs --indent-preproc-block --delete-empty-lines --pad-method-prefix --squeeze-lines=2 --suffix=none %f


REM ::删除astyle生成的所有中间文件
for /r %~dp1%~nx1 %%a in (*.orig) do del %%a
for /r %~dp1%~nx1 %%a in (*.pre) do del %%a
for /r %~dp1%~nx1 %%a in (*.bak) do del %%a


timeout /t 5 /nobreak >nul

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eyyhappy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值