[clang-format] Add new option PenaltyIndentedWhitespace

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D90534
This commit is contained in:
Mark Nauwelaerts 2020-10-31 14:15:38 +01:00
parent a38ed62ea8
commit 1e4d6d1c1f
5 changed files with 49 additions and 0 deletions

View File

@ -2301,6 +2301,10 @@ the configuration (without a prefix: ``Auto``).
**PenaltyExcessCharacter** (``unsigned``)
The penalty for each character outside of the column limit.
**PenaltyIndentedWhitespace** (``unsigned``)
Penalty for each character of whitespace indentation
(counted relative to leading non-whitespace column).
**PenaltyReturnTypeOnItsOwnLine** (``unsigned``)
Penalty for putting the return type of a function onto its own
line.

View File

@ -1913,6 +1913,10 @@ struct FormatStyle {
/// line.
unsigned PenaltyReturnTypeOnItsOwnLine;
/// Penalty for each character of whitespace indentation
/// (counted relative to leading non-whitespace column).
unsigned PenaltyIndentedWhitespace;
/// The ``&`` and ``*`` alignment style.
enum PointerAlignmentStyle {
/// Align pointer to the left.

View File

@ -785,6 +785,22 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
State.Column = getNewLineColumn(State);
// Add Penalty proportional to amount of whitespace away from FirstColumn
// This tends to penalize several lines that are far-right indented,
// and prefers a line-break prior to such a block, e.g:
//
// Constructor() :
// member(value), looooooooooooooooong_member(
// looooooooooong_call(param_1, param_2, param_3))
// would then become
// Constructor() :
// member(value),
// looooooooooooooooong_member(
// looooooooooong_call(param_1, param_2, param_3))
if (State.Column > State.FirstIndent)
Penalty +=
Style.PenaltyIndentedWhitespace * (State.Column - State.FirstIndent);
// Indent nested blocks relative to this column, unless in a very specific
// JavaScript special case where:
//

View File

@ -590,6 +590,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine);
IO.mapOptional("PenaltyIndentedWhitespace",
Style.PenaltyIndentedWhitespace);
IO.mapOptional("PointerAlignment", Style.PointerAlignment);
IO.mapOptional("RawStringFormats", Style.RawStringFormats);
IO.mapOptional("ReflowComments", Style.ReflowComments);
@ -968,6 +970,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
LLVMStyle.PenaltyBreakTemplateDeclaration = prec::Relational;
LLVMStyle.PenaltyIndentedWhitespace = 0;
LLVMStyle.DisableFormat = false;
LLVMStyle.SortIncludes = true;

View File

@ -17214,6 +17214,28 @@ TEST_F(FormatTest, LikelyUnlikely) {
Style);
}
TEST_F(FormatTest, PenaltyIndentedWhitespace) {
verifyFormat("Constructor()\n"
" : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaa(aaaaaaaaaaaaaaaaaa, "
"aaaaaaaaaaaaaaaaaat))");
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaa), "
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
verifyFormat("Constructor()\n"
" : aaaaaa(aaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
StyleWithWhitespacePenalty);
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaa), "
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
StyleWithWhitespacePenalty);
}
TEST_F(FormatTest, LLVMDefaultStyle) {
FormatStyle Style = getLLVMStyle();
verifyFormat("extern \"C\" {\n"