前言

本文推荐一种格式化工具, AStyle

使用方法

下载 AStyle

Astyle官网下载地址:

https://sourceforge.net/projects/astyle

在 vscode 中安装 AStyle

在 vscode 扩展商城中安装 AStyle 插件

配置 AStyle 路径

  • 在 vscode 中按快捷键: CTRL+SHIFT+P 搜索 setting , 打开 settings.json
  • 输入以下信息,配置路径
1
"astyle.executable": "E:\\1.software\\astyle-3.6-x64\\astyle.exe",

配置 AStyle 参数

  • 在 vscode 中按快捷键: CTRL+SHIFT+P 搜索 setting , 打开 settings.json
  • 输入以下信息,配置路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"astyle.astylerc": "",
// "astyle.cmd_options": [
/* AStyle */
"astyle.additional_languages": [

"*.c",
"*.cpp",
"*.h",
"*.hpp",
],
"astyle.cmd_options": [
// 预定义风格 -----------------------------------------------------------
// "--style=ansi", // 使用 ANSI 风格格式和缩进
"--style=linux", // 使用 Linux 风格格式和缩进
//"--style=gnu", // 使用 GNU 风格格式和缩进
"--indent=spaces=4", // 缩进使用 4 个空格
"--indent-preproc-block", // 处理器指令块与其余代码对齐
"--pad-oper", // 在操作符号两边增加空格字符
"--pad-header", // 在关键字 (如 if, for, while) 后增加空格
"--unpad-paren", // 移除括号两端的多余空格
"--suffix=none", // 不生成备份文件
"--align-pointer=type", // 将 * 和 & 符号靠近类型
"--align-pointer=name", // 将 * 和 & 符号靠近变量名称
"--lineend=linux", // 使用 Linux 风格的换行符
"--convert-tabs", // 将制表符转换为空格
"--verbose", // 输出详细信息
"--delete-empty-lines", // 删除多余的空行
//"--pad-paren-in", // 在括号内部添加空格
"--unpad-paren", // 移除括号两端的多余空格
"-S", // `switch` 和 `case` 不同列, `case` 缩进
"-F", // 使用空行分隔无关块
],