【问题标题】:How to prevent clang-format to add space after comment char?如何防止clang-format在评论字符后添加空格?
【发布时间】:2016-01-03 09:00:37
【问题描述】:

我的代码中有一些 cmets:

//asdf

当我对其使用 clang-format 时,它会在 // 字符之后添加一个空格:

// asdf

如何在 clang-format 配置中防止这种情况发生?

谢谢

【问题讨论】:

  • 有趣...documentationclang-format -dump-config | grep -i 'space\|comment' 似乎都没有提及此事。
  • SpacesBeforeTrailingComments 似乎意味着 // 在代码行上的 before 空格,但文档不是很清楚;至少值得一试。
  • 早些时候,我看过clang::format::FormatStyle class documentation“用于配置格式以遵循特定准则”)...仍然无法找到一个合适的选项来控制这种行为。正如 jpw 所说,您最好的选择是以定制的 clang 格式实现该功能。或者...也许您可以在the LLVM Bug Tracking System 提交报告?

标签: c++ clang-format


【解决方案1】:

结合这两个问题的答案应该可以解决问题:

所以.clang-format 文件中的以下行应该可以解决问题(我没有测试它):

CommentPragmas:  '^[^ ]'

这告诉 clang-format 不要与以空格以外的其他内容开头的 cmets 混淆。

For completeness, clang-format documentation here.

【讨论】:

  • 我对此进行了测试(使用 clang-format 3.7),它似乎确实有效。
  • 使用 clang-format 3.5 它也像一个魅力:) 谢谢
猜你喜欢
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 2016-05-20
  • 2011-11-04
相关资源
最近更新 更多