clang-format: Align asterisk (*) of pointer declaration with variable name

女生的网名这么多〃 提交于 2019-12-03 12:18:40

问题


I am using the following options in my .clang-format file:

AlignConsecutiveDeclarations: true
PointerAlignment: Right

The current formatting result is the following:

char *         var1;
SomeOtherType *var2;
int            var3;

The result I was expecting would be:

char          *var1; //note the changed position of * 
SomeOtherType *var2;
int            var3;

How can I configure clang-format to align the asterix (*) with the variable name rather then with the type when I am using the AlignConsecutiveDeclarations option?


回答1:


PointerAlignment: Right is unfortunately not implemented yet.

See https://github.com/llvm-mirror/clang/blob/release_39/lib/Format/WhitespaceManager.cpp#L327-L340

void WhitespaceManager::alignConsecutiveDeclarations() {
  if (!Style.AlignConsecutiveDeclarations)
    return;

  // FIXME: Currently we don't handle properly the PointerAlignment: Right
  // The * and & are not aligned and are left dangling. Something has to be done
  // about it, but it raises the question of alignment of code like:
  //   const char* const* v1;
  //   float const* v2;
  //   SomeVeryLongType const& v3;

  AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },
              Changes);
}


来源:https://stackoverflow.com/questions/38392889/clang-format-align-asterisk-of-pointer-declaration-with-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!