简体   繁体   English

多行函数声明参数的 Clang 格式问题

[英]Clang Format Issue with multiline function declaration parameters

Clang Format is consistently doing this: Clang Format 一直在这样做:

bool importSomethingIn( const boost::property_tree::ptree& inBoostTree,
                        int inSomeIndex,
                        std::shared_ptr<Something>
                            inSomething,
                        int x,
                        int y );

When I want it to do this:当我希望它这样做时:

bool importSomethingIn( const boost::property_tree::ptree& inBoostTree,
                        int inSomeIndex,
                        std::shared_ptr<Something> inSomething,
                        int x,
                        int y );

Note that it is adding a newline and indent before the symbol inSomething .请注意,它在符号inSomething之前添加了换行符和缩进。 It seems to always be the templated types that cause this behaviour.似乎总是导致这种行为的模板类型。

How can I stop it from doing this?我怎样才能阻止它这样做?

Here is my current .clang-format:这是我当前的 .clang 格式:

BasedOnStyle: LLVM
Language: Cpp
AccessModifierOffset: -4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DerivePointerBinding: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: true
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
PointerBindsToType: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: true
Standard: Cpp11
TabWidth: 8
UseTab: Never

Edit: I am using the clang format Xcode plugin which I obtained here on github July 2016 (first installed Alcatraz then clang format plugin with Aclatraz).编辑:我使用的是我获得的铛格式的Xcode插件这里在github上2016年7月(第一次安装恶魔然后铛格式插件与Aclatraz)。 I'm not entirely sure where the clang-format binary is, thus haven't seen the version number for the one that came with the plugin.我不完全确定 clang-format 二进制文件在哪里,因此还没有看到插件附带的版本号。

However, based on the suggestions below I installed clang-format using Homebrew which gave me version 3.9.0.但是,根据下面的建议,我使用 Homebrew 安装了 clang-format,它给了我 3.9.0 版。 I then set the Xcode plugin to "use system clang-format" and I got the desired result, so I guess it must have been a bug.然后我将 Xcode 插件设置为“使用系统 clang-format”,我得到了想要的结果,所以我想它一定是一个错误。

Your case seems fine to me on ubuntu 16.04 with clang-format 3.8在 ubuntu 16.04 和 clang-format 3.8 上你的情况对我来说似乎很好

  • Install clang-format-3.8安装 clang-format-3.8

     sudo apt-get install clang-format-3.8
  • In ~/.vimrc~/.vimrc

     map <F3> :pyf /usr/share/vim/addons/syntax/clang-format-3.8.py<cr> imap <F3> <Co>:pyf /usr/share/vim/addons/syntax/clang-format-3.8.py<cr>
  • Use @OP's .clang-format and source code使用@OP 的 .clang 格式和源代码

     mkdir ~/test vim ~/test/.clang-format vim ~/test/test.cpp
  • Format test.cpp in vim (using F3), then the result:在vim中格式化test.cpp(使用F3),然后结果:

     bool importSomethingIn( const boost::property_tree::ptree& inBoostTree, int inSomeIndex, std::shared_ptr<Something> inSomething, int x, int y );

I think it is caused by ColumnLimit .我认为这是由ColumnLimit引起的。 I think it's a bug...I had this problem ones and I solved it by changing this variable.我认为这是一个错误......我有这个问题,我通过改变这个变量解决了它。 Try to set:尝试设置:

 ColumnLimit: 0

Reason原因

The length of bool importSomethingIn( const boost::property_tree::ptree& inBoostTree, int inSomeIndex, std::shared_ptr<Something>" has 117+-3 chars in dependence of computing invisible chars. is 117+-3 in dependence of computing invisible characters. ColumnLimit: 120 has been set and it is one of possible reasons, I think, it has been cut. bool importSomethingIn( const boost::property_tree::ptree& inBoostTree, int inSomeIndex, std::shared_ptr<Something>" has 117+-3 chars in dependence of computing invisible chars.的长度bool importSomethingIn( const boost::property_tree::ptree& inBoostTree, int inSomeIndex, std::shared_ptr<Something>" has 117+-3 chars in dependence of computing invisible chars.是 117+-3 ColumnLimit: 120已设置,这是可能的原因之一,我认为,它已被削减。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM