安裝 Astyle
- 下載 Astyle:https://sourceforge.net/projects/astyle/files/
- 下載完成解壓縮后虹脯,進(jìn)入其中的 doc 目錄,打開(kāi) install.html 文檔埃儿,里面有各種安裝 Astyle 的方式匣掸。此處我選擇的是 Linux 環(huán)境下的 Cmake 安裝方式疗杉。
- 在 Astyle 根目錄輸入以下命令:
mkdir as-gcc-exe
cd as-gcc-exe
cmake ../
make
- 安裝完成后還需將 Astyle 的可執(zhí)行文件所在的目錄加入 PATH关噪,可執(zhí)行文件就在上述的 as-gcc-exe 的文件夾下面拿穴,編輯 ~/.bashrc 將該路徑加入 PATH削彬,并且 source ~/.bashrc 即完整安裝 Astyle 全庸。
配置 VSCode
- 在 VSCode 里安裝 astyle 插件秀仲。
- 編輯 settings.json 文件,此處我是 "Ctrl + ," 打開(kāi)設(shè)置壶笼,然后輸入 "astyle" 來(lái)打開(kāi)的啄育。
在 settings.json 中加入
"astyle.additional_languages": [
"c",
"cpp",
],
"astyle.cmd_options": [
"--style=allman",
"--indent=spaces=4",
"--indent-preproc-block",
"--pad-oper",
"--pad-header",
"--unpad-paren",
"--suffix=none",
"--align-pointer=name",
"--lineend=linux",
"--convert-tabs",
"--verbose",
],
注意 json 文件的格式,此處的選項(xiàng)使用的是 RT-Thread 的代碼格式化選項(xiàng)拌消。
- 對(duì)當(dāng)前 C/C++ 文件按 "Ctrl + Shift + I" 即可完成代碼格式化挑豌,網(wǎng)上有說(shuō)按 "Alt + Shift + F" 來(lái)執(zhí)行代碼格式化,但我這里不行墩崩。