Using astyle to format code

As you know, we had our coding style which everyone should follow, and also we had the cpplint tool to check if we had followed the rule, but we do not have a tool to check and transfer our code to the desired style code. Then there comes astyle:

Astyle is a tool which can automatically format our code and make most of the code fit our coding style. As from the website: "Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages."

Astyle can be installed both in windows and linux.It can also be integrated as a third party tool to format the code.

Below I would like to introduce its usage in Qt creator and Linux command line.

 

Qt creator can be used to generated part of the code, but it have its own style, which it's different from the google style which we use, it's tedious to change it by hand. Luckily, Qt creator had integrated astyle as part of the  plugin Beautifer. Here is the step to use astyle in Qt creator( The Qt creator I demoed here isQt Creator 3.3.0 (opensource) version).

  1. Click "Help"->"About plugin", check in  Beautifier.and restart the Qt creator.



2. Click "Tools"->"Options", you will see "Beautifier" is shown:


3. Download Astyle windows version which is a green software, unzip the package and browse to the Astyle.exe as above,check use the default .astylerc file and use customized style and edit it:



After the above three steps, you can use astyle to format the code in your Qt creator by click as follows or create your own customized shortcut:



Here is an example before and after using astyle:


In Linux, you can use astyle the same way, download the linux version and install to you $HOME, create a .astylerc file under your $HOME folder, customize it as:

#short option for command line uses: -A14s4SYk1fpHcn
# Predefined style option -A14
--style=google
 
# Indent using 4 spaces per indent. Don't use tab -s4
--indent=spaces=4
 
 
# Indent 'switch' blocks so that the 'case X:' statement are
# indented in the switch block. -S
--indent-switches
 
#Indent C++ comments beginning in column one. -Y
--indent-col1-comments
 
#Attach a pointer or reference operator (*, &, or ^) to either the variable type left -k1
--align-pointer=type
 
# Pad empty lines around header blocks (eg. 'if', 'while' ...) -f
--break-blocks
 
# Insert space padding around operators. -p
--pad-oper
 
#Insert space padding between a header (e.g. 'if', 'for', 'while'...) and the following paren -H
--pad-header
 
# Converts tabs into spaces in the non-indentation part of the line -c
--convert-tabs
 
# Do not retain a backup of the origional file -n
--suffix=none

Then you can use it like this:

for what more astyle can do, you can referhere.

### 推荐的文本格式化软件和工具 以下是几款适用于不同类型文本格式化的强大工具: #### 1. **Sublime Text 3** Sublime Text 是一款功能强大的轻量级文本编辑器,支持多种文件类型的格式化操作。对于 JSON 数据格式化,可以通过安装插件 `JSON Reindent` 或者直接使用内置的功能来实现快速格式化[^1]。 此外,Sublime Text 还提供了 SQL 格式化插件(如 `SQLFormat`),能够帮助用户轻松完成复杂 SQL 查询语句的格式化。 #### 2. **AStyle (Artistic Style)** AStyle 是一种专门针对 C/C++/C#/Java 等编程语言的源代码格式化工具。它可以自动化调整代码风格,例如缩进、空格管理以及括号位置优化等[^2]。虽然其主要用途在于程序代码格式化,但对于某些结构化文本(如配置文件)也有一定的适用场景。 #### 3. **Jalopy** 如果专注于 Java 开发领域,则可以选择 Jalopy 来执行更精细粒度的代码格式化工作。该工具有助于遵循严格的编码标准并保持团队间的一致性[^4]。通过自定义规则集,还可以满足特殊需求下的格式要求。 #### 4. **Prettier** Prettier 是现代 JavaScript 生态中最流行的代码格式化工具体现之一。除了 JS 外,还扩展支持 HTML, CSS, TypeScript等多种前端技术栈的语言格式化任务。它的特点是零配置即可获得良好的效果,并且易于与其他构建工具链集成在一起。 #### 5. **Beautifier Tools Built Into Browsers or IDEs** 许多主流浏览器调试控制台本身就具备基本级别的 JSON 查看与解析能力;而像 Visual Studio Code这样的综合型IDE则内置了大量的第三方扩展选项供选择,从而极大地方便了日常开发过程中遇到的各种形式的数据整理需求。 ```javascript // Example of using Prettier to format a piece of code programmatically. const prettier = require('prettier'); let formattedCode = prettier.format("function test(){return 'hello world';}", {parser:"babel"}); console.log(formattedCode); ``` 以上列举了几种常见的文本格式化解决方案及其特点介绍,请依据实际应用场景选取最适合的产品组合起来运用吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值