天天看點

sublimeAstyle針對C++20三路運算符的格式化調整

tags: Sublime Tips C++

寫在前面

之前文章介紹過關于在sublime編輯器中格式化C/C++ 代碼的最佳方式, 但是昨天嘗試用C++20新增的三路運算符時候突然被格式化掉了, 具體例子如下:

auto ans1 = a <=> b;
// 被格式化為
auto ans1 = a <= > b;      

這就成了一個顯然的文法錯誤了, 一開始我想通過更新插件或者看issue的方式解決, 但是這個項目已經不活躍了, 沒辦法,隻能靠自己了.

配置方法

首先進入官方文檔​​1​​​, 但是選項太多了, 找起來不友善… 後來參考的gist​​2​​​, 還是不錯的, 通過在家目錄下配置​

​.astylerc​

​檔案完成配置, 這裡除了複制全部的内容并建立檔案, 還需要修改一個選項, 如下:

cd ~
vim      

注釋掉​

​pad-oper​

​選項, 使得不進行操作符之間空格的填充:

# Insert space padding around operators. Any end of line comments will remain
# in the original column, if possible. Note that there is no option to unpad.
# Once padded, they stay padded.
#
# if (foo==2)
#     a=bar((b-c)*a,d--);
#
# becomes:
#
# if (foo == 2)
#      a = bar((b - c) * a, d--);
#
#pad-oper      
// Language-specific for C++
    "options_c++": {
        "use_only_additional_options": true,
        "additional_options_file": "/Users/xxx/.astylerc",//your astylerc file dir
    },      
// Insert space padding around operators. Any end of line comments
        // will remain in the original column, if possible. Note that there
        // is no option to unpad. Once padded, they stay padded.
        "pad-oper": false,      

ref

  1. ​​Artistic Style (sourceforge.net)​​​; ​​↩︎​​
  2. ​​Astyle code automatic formatting settings (github.com)​​​; ​​↩︎​​

繼續閱讀