05月25, 2018

Code::Blocks 17.12中取消代码格式化功能对单行语句的换行

之前用了16.01之后就没改过,最近装了个17.12,发现source formatter功能在细节上发生了一些变化。

我们知道这个功能是借助于astyle实现的,估计是codeblocks对于astyle的默认的命令行参数发生了变化。

经过一番查找测试之后,我发现勾选Source Formatter -> Formatting -> Break one-line headers即可解决问题。

alt

但很明显这个描述是不太对的。我们需要的是Don't break one-line headers啊!

于是我怀疑codeblocks的代码有问题,下载其源码找到codeblocks-17.12\src\plugins\astyle\formattersettings.cpp,126至136行如下:

  formatter.setBreakClosingHeaderBracketsMode(cfg->ReadBool(_T("/break_closing")));
  formatter.setBreakElseIfsMode(cfg->ReadBool(_T("/break_elseifs")));
  formatter.setAddBracketsMode(cfg->ReadBool(_T("/add_brackets")));
  formatter.setAddOneLineBracketsMode(cfg->ReadBool(_T("/add_one_line_brackets")));
  formatter.setRemoveBracketsMode(cfg->ReadBool(_T("/remove_brackets")));
  formatter.setBreakOneLineBlocksMode(!cfg->ReadBool(_T("/keep_blocks")));
  formatter.setBreakOneLineHeadersMode(!cfg->ReadBool(_T("/keep_headers")));
  formatter.setBreakOneLineStatementsMode(!cfg->ReadBool(_T("/keep_statements")));
  formatter.setTabSpaceConversionMode(cfg->ReadBool(_T("/convert_tabs")));
  formatter.setCloseTemplatesMode(cfg->ReadBool(_T("/close_templates")));
  formatter.setStripCommentPrefix(cfg->ReadBool(_T("/remove_comment_prefix")));

我们发现keep_blockskeep_headerskeep_statements前面取了非,但设置界面中的描述(codeblocks-17.12\src\plugins\astyle\resources\configuration.xrc)却是Don't break one-line blocksBreak one-line headersBreak one-line statements

我们结合astyle可执行程序的选项(文档

  • --keep-one-line-blocks / -O: Don't break one-line blocks.
  • --break-one-line-headers / -xb: Break one line headers (e.g. 'if', 'while', 'else', ...) from a statement residing on the same line. If the statement is enclosed in braces, the braces will be formatted according to the requested brace style. A multi-statement line will NOT be broken if keep-one-line-statements is requested. One line blocks will NOT be broken if keep-one-line-blocks is requested and the header is enclosed in the block.
  • --keep-one-line-statements / -o: Don't break complex statements and multiple statements residing on a single line.

以及astyle的代码codeblocks-17.12\src\plugins\astyle\astyle\ASFormatter.cpp的2186-2212行:

/**
 * set option to break/not break one-line blocks
 *
 * @param state        true = break, false = don't break.
 */
void ASFormatter::setBreakOneLineBlocksMode(bool state)
{
    shouldBreakOneLineBlocks = state;
}

/**
* set one line headers breaking mode
*/
void ASFormatter::setBreakOneLineHeadersMode(bool state)
{
    shouldBreakOneLineHeaders = state;
}

/**
* set option to break/not break lines consisting of multiple statements.
*
* @param state        true = break, false = don't break.
*/
void ASFormatter::setBreakOneLineStatementsMode(bool state)
{
    shouldBreakOneLineStatements = state;
}

即true都是break。

因此在默认情况下我们应该取!keep_blockskeep_headers!keep_statements,对应的描述应该是Don't break one-line blocksBreak one-line headersDon't break one-line statements

这样的话在默认情况下所有都不勾选也正是astyle可执行程序的默认情况。

这些功能都是Astyle 3版本新增加的功能,2里面是没有的,由于Codeblocks更新后Astyle也使用了新版本,但没搞清楚用法,所以出现了这个问题。

PS: 我已经向codeblocks项目反馈了这个问题。

本文链接:https://debug.fanzheng.org/post/disable-breaking-one-line-in-codeblocks.html

-- EOF --

Comments

评论加载中...

注:如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理。