Index: lib/Format/BreakableToken.cpp =================================================================== --- lib/Format/BreakableToken.cpp +++ lib/Format/BreakableToken.cpp @@ -77,6 +77,14 @@ } StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); + + // Do not split before a number followed by a dot: this would be interpreted + // as a numbered list, which would prevent re-flowing in subsequent passes. + static llvm::Regex kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\."); + if (SpaceOffset != StringRef::npos && + kNumberedListRegexp.match(Text.substr(SpaceOffset).ltrim(Blanks))) + SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); + if (SpaceOffset == StringRef::npos || // Don't break at leading whitespace. Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) { @@ -298,8 +306,9 @@ static bool mayReflowContent(StringRef Content) { Content = Content.trim(Blanks); // Lines starting with '@' commonly have special meaning. - static const SmallVector kSpecialMeaningPrefixes = { - "@", "TODO", "FIXME", "XXX"}; + // Lines starting with '-', '-#', '+' or '*' are bulleted/numbered lists. + static const SmallVector kSpecialMeaningPrefixes = { + "@", "TODO", "FIXME", "XXX", "-# ", "- ", "+ ", "* " }; bool hasSpecialMeaningPrefix = false; for (StringRef Prefix : kSpecialMeaningPrefixes) { if (Content.startswith(Prefix)) { @@ -307,6 +316,14 @@ break; } } + + // Numbered lists may also start with a number followed by '.' + // To avoid issues if a line starts with a number which is actually the end + // of a previous line, we only consider numbers with up to 2 digits. + static llvm::Regex kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\. "); + hasSpecialMeaningPrefix = hasSpecialMeaningPrefix || + kNumberedListRegexp.match(Content); + // Simple heuristic for what to reflow: content should contain at least two // characters and either the first or second character must be // non-punctuation. Index: unittests/Format/FormatTestComments.cpp =================================================================== --- unittests/Format/FormatTestComments.cpp +++ unittests/Format/FormatTestComments.cpp @@ -1534,7 +1534,7 @@ " *\n" " * long */", getLLVMStyleWithColumns(20))); - + // Don't reflow lines having content that is a single character. EXPECT_EQ("// long long long\n" "// long\n" @@ -1559,7 +1559,7 @@ format("// long long long long\n" "// @param arg", getLLVMStyleWithColumns(20))); - + // Don't reflow lines starting with 'TODO'. EXPECT_EQ("// long long long\n" "// long\n" @@ -1628,6 +1628,69 @@ "// long", getLLVMStyleWithColumns(20))); + // Don't reflow separate bullets in list + EXPECT_EQ("// - long long long\n" + "// long\n" + "// - long", + format("// - long long long long\n" + "// - long", + getLLVMStyleWithColumns(20))); + EXPECT_EQ("// * long long long\n" + "// long\n" + "// * long", + format("// * long long long long\n" + "// * long", + getLLVMStyleWithColumns(20))); + EXPECT_EQ("// + long long long\n" + "// long\n" + "// + long", + format("// + long long long long\n" + "// + long", + getLLVMStyleWithColumns(20))); + EXPECT_EQ("// 1. long long long\n" + "// long\n" + "// 2. long", + format("// 1. long long long long\n" + "// 2. long", + getLLVMStyleWithColumns(20))); + EXPECT_EQ("// -# long long long\n" + "// long\n" + "// -# long", + format("// -# long long long long\n" + "// -# long", + getLLVMStyleWithColumns(20))); + + EXPECT_EQ("// - long long long\n" + "// long long long\n" + "// - long", + format("// - long long long long\n" + "// long long\n" + "// - long", + getLLVMStyleWithColumns(20))); + EXPECT_EQ("// - long long long\n" + "// long long long\n" + "// long\n" + "// - long", + format("// - long long long long\n" + "// long long long\n" + "// - long", + getLLVMStyleWithColumns(20))); + + // Large number (>2 digits) are not list items + EXPECT_EQ("// long long long\n" + "// long 1024. long.", + format("// long long long long\n" + "// 1024. long.", + getLLVMStyleWithColumns(20))); + + // Do not break before number, to avoid introducing a non-reflowable doxygen + // list item. + EXPECT_EQ("// long long\n" + "// long 10. long.", + format("// long long long 10.\n" + "// long.", + getLLVMStyleWithColumns(20))); + // Don't break or reflow after implicit string literals. verifyFormat("#include // l l l\n" " // l",