ReputationZh

VSC 可以不可以设定某一段代码不参与格式化?右键格式化代码的时候会使整个文件格式化。

  •  
  •   ReputationZh · Sep 4, 2020 · 4914 views
    This topic created in 2260 days ago, the information mentioned may be changed or developed.
    举个栗子:
    写一个比较大框架的结构体,空格对齐后,此时格式化代码,会将所有的缩进删掉,使代码阅读观感降低。

    所以,有没有办法设定某一段代码不参与全文格式化呢?
    10 replies  •  2020-09-04 20:03:52 +08:00
    onfuns
    onfuns
    Sep 4, 2020
    1
    如果用的是 prettier 插件,可以在代码上面一行加注释 //prettier-ignore 可以使下面的代码块不被格式化
    ReputationZh
    ReputationZh
    OP
    Sep 4, 2020
    2
    @onfuns prettier 可以格式化 C 语言吗?
    onfuns
    onfuns
    Sep 4, 2020
    3
    @ReputationZh 可以啊,vscode 内置的好像就是 prettier 插件
    ReputationZh
    ReputationZh
    OP
    Sep 4, 2020
    4
    @onfuns 刚才去看了一下,prettier 只对前端语言支持的比较好,C 语言用的是 clang-format,好像没有这项功能唉。
    flowfire
    flowfire
    Sep 4, 2020
    5
    可以选中一段代码,然后右键格式化。
    如果你说的是按照规则固定格式化某段代码,那估计得改配置
    zhongrs232
    zhongrs232
    Sep 4, 2020
    6
    clang-format 也可以禁止某段代码格式化,格式是:
    // clang-format off
    // 不需要格式化的代码
    // clang-format on
    crclz
    crclz
    Sep 4, 2020
    8
    把不需要格式化的结构体分离到另一个文件
    GeruzoniAnsasu
    GeruzoniAnsasu
    Sep 4, 2020 via Android
    9
    我记得 clang format 有选项控制是否对齐结构体的等号
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2095 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 15:12 · PVG 23:12 · LAX 07:12 · JFK 10:12
    ♥ Do have faith in what you're doing.