如何标记一个区域以便 clang-format 不会触及它?

问题描述 投票:0回答:3

例如,在 MySQL++ 库中,有一些宏可用于基于 sql 表定义来定义简单的结构体,如下所示:

sql_create_6(stock, 1, 6,
    mysqlpp::sql_char, item,
    mysqlpp::sql_bigint, num,
    mysqlpp::sql_double, weight,
    mysqlpp::sql_decimal, price,
    mysqlpp::sql_date, sdate,
    mysqlpp::Null<mysqlpp::sql_mediumtext>, description)

问题是 clang-format 将以一种更难以阅读的方式重新格式化它(每个参数都在新行上)。 大多数代码格式化程序可以识别特殊的 format-off / format-on 注释,但我在 clang-format 手册中没有找到类似的内容。

c++ clang-format
3个回答
168
投票

在较新的版本中,您可以用以下内容包围一段代码:

// clang-format off
...
// clang-format on

9
投票

尝试在每行后面添加一个

//
注释标记,这可能会成功。我在 Eclipse 中遇到了同样的问题并学会了这个技巧。

sql_create_6(stock, 1, 6, //
    mysqlpp::sql_char, item, //
    mysqlpp::sql_bigint, num, //
    mysqlpp::sql_double, weight, //
    mysqlpp::sql_decimal, price, //
    mysqlpp::sql_date, sdate, //
    mysqlpp::Null<mysqlpp::sql_mediumtext>, description)

4
投票

我注意到了

//clang-format off
没用,但是
// clang-format off
做到了!

//
之后的空格使一切变得不同。

© www.soinside.com 2019 - 2024. All rights reserved.