VS 2022 Community 代碼格式化時(shí)自動wrap的問題
在 VS 2022 Community 里確實(shí)有點(diǎn)坑:微軟把 C++ 的 Wrapping 選項(xiàng)默認(rèn)隱藏了,取而代之的是用 clang-format 或 .editorconfig 來控制換行風(fēng)格。
也就是說:
如果項(xiàng)目里沒有 .clang-format 或 .editorconfig,VS 會使用內(nèi)置的 clang-format 默認(rèn)規(guī)則(它會在 80 或 120 列后強(qiáng)制 wrap)。
所以你在 Tools → Options 里找不到 Wrap long lines的相關(guān)設(shè)置。
? 解決辦法
方法 1:禁用 clang-format 自動格式化
-
打開:
Tools → Options → Text Editor → C/C++ → Code Style → Formatting → General -
把 Enable ClangFormat support 取消勾選。
- 這樣 VS 就會回到傳統(tǒng)的格式化器(然后你才會看到
Wrapping選項(xiàng))。
- 這樣 VS 就會回到傳統(tǒng)的格式化器(然后你才會看到
方法 2:自己創(chuàng)建 .clang-format
如果你想繼續(xù)用 clang-format,但不要 wrap,可以在項(xiàng)目根目錄(或者解決方案目錄)放一個(gè)文件:
.clang-format
BasedOnStyle: Microsoft
ColumnLimit: 0 # 0 表示禁用自動換行
保存之后,重新格式化 (Ctrl+K, Ctrl+D),就不會再 wrap。
方法 3:用 .editorconfig
如果你更習(xí)慣 VS 原生支持的 .editorconfig,可以加一行:
[*]
c++_wrap_long_lines = false
不過注意:這個(gè)選項(xiàng)支持有限,clang-format 更推薦。

浙公網(wǎng)安備 33010602011771號