我在 .clang-format
文件中使用以下选项:
AlignConsecutiveDeclarations: true
PointerAlignment: Right
当前格式化结果如下:
char * var1;
SomeOtherType *var2;
int var3;
我期望的结果是:
char *var1; //note the changed position of *
SomeOtherType *var2;
int var3;
如何配置clang-format
以将星号(*)与变量名称对齐,而不是与类型对齐
使用 AlignConsecutiveDeclarations
选项?
最佳答案
PointerAlignment: Right
不幸的是尚未实现。
参见https://github.com/llvm/llvm-project/blob/master/clang/lib/Format/WhitespaceManager.cpp#L643
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);
}
关于clang-format:将指针声明的星号 (*) 与变量名对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392889/