Формат Clang разбивает тело оператора на несколько строк

У меня есть следующий файл.cpp:

////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) { call_function_name(test); }
  if (test) { sf(test); }
  return 0;
}

(косая черта ограничивает 80 символов). Используя следующий конфигурационный файл, clang-format предлагает:

////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) { 
    call_function_name(test); 
  }
  if (test) { 
    sf(test); 
  }
  return 0;
}

даже в файле я разрешаю короткие операторы if помещаться в одну строку.

  • Я неправильно установил какие-либо параметры?

  • Какие варианты я могу использовать, чтобы минимизировать потерянное вертикальное пространство?

Файл в формате Clang.clang

BasedOnStyle: Google
AccessModifierOffset: -1
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: true
BreakBeforeBinaryOperators: true
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 0
Cpp11BracedListStyle: true
DerivePointerBinding: true
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: true
IndentWidth: 2
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerBindsToType: true
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInAngles:  false
Standard: Cpp11
TabWidth: 2
UseTab: Never

2 ответа

Решение

Более новые версии clang-формата имеют дополнительную опцию "AllowShortBlocksOnASingleLine", которая управляет этим поведением.

Похоже, что clang-format применяется только AllowShortIfStatementsOnASingleLine вариант, если вы опустите скобки. Я проверил следующее:

void call_long_function_name(bool) {}
void call_long_super_duper_long_really_really_long_way_long_function_name(bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) 
    call_function_name(test);
  if (test)
    sf(test);
  if (test)
    call_long_super_duper_long_really_really_long_way_long_function_name(test);
  if (test) {
    return 0;
  }
  return 0;
}

И получил:

void call_long_function_name(bool) {}
void call_long_super_duper_long_really_really_long_way_long_function_name(
bool) {}
void sf(bool) {}
int main() {
  bool test = true;
  if (test) call_function_name(test);
  if (test) sf(test);
  if (test)
    call_long_super_duper_long_really_really_long_way_long_function_name(test);
  if (test) {
    return 0;
  }
  return 0;
}
Другие вопросы по тегам