You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置clang-format使函数的)和}单独换行?

配置clang-format实现函数参数换行及右括号单独成行

要实现你需要的函数声明/调用格式——每个参数单独换行,且右括号)单独占一行,只需在现有.clang-format配置中添加几个关键选项:

BinPackParameters: false
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
AllowAllArgumentsOnNextLine: false
ClosingParenthesisIndentation: Align

完整的配置文件如下:

BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 2
TabWidth: 2
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: true
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -2
FixNamespaceComments: false
BinPackParameters: false
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
AllowAllArgumentsOnNextLine: false
ClosingParenthesisIndentation: Align

新增选项说明

  • BinPackParameters: false:禁止将函数声明的多个参数压缩到同一行,强制每个参数单独换行
  • AllowAllParametersOfDeclarationOnNextLine: false:不允许把所有函数声明参数放在函数名的下一行,确保每个参数各自占一行
  • BinPackArguments: false:对函数调用做同样处理,强制每个调用参数单独换行
  • AllowAllArgumentsOnNextLine: false:禁止将所有函数调用参数放在同一行
  • ClosingParenthesisIndentation: Align:让右括号)与函数名/调用语句的起始位置对齐,单独占一行

配置后,函数声明会被格式化为:

void draw(
    const geometry::rect &source,
    const geometry::rect &destination,
    const double angle = 0.0,
    flip flip = flip::none,
    const uint8_t alpha = 255
) const noexcept;

函数调用会被格式化为:

lua.new_usertype<audio::soundmanager>(
      "SoundManager",
      "play", &audio::soundmanager::play,
      "stop", &audio::soundmanager::stop
  );

不会出现右括号与最后一个参数同行的错误格式:

lua.new_usertype<audio::soundmanager>(
      "SoundManager",
      "play", &audio::soundmanager::play,
      "stop", &audio::soundmanager::stop);

内容的提问来源于stack exchange,提问作者Rodrigo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 17:30:04