Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- BreakableToken.cpp - Format C++ code -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Contains implementation of BreakableToken class and classes derived |
| 12 | /// from it. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "BreakableToken.h" |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 17 | #include "ContinuationIndenter.h" |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 19 | #include "clang/Format/Format.h" |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "format-token-breaker" |
| 25 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 29 | static const char *const Blanks = " \t\v\f\r"; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 30 | static bool IsBlank(char C) { |
| 31 | switch (C) { |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 32 | case ' ': |
| 33 | case '\t': |
| 34 | case '\v': |
| 35 | case '\f': |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 36 | case '\r': |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 37 | return true; |
| 38 | default: |
| 39 | return false; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 43 | static StringRef getLineCommentIndentPrefix(StringRef Comment) { |
| 44 | static const char *const KnownPrefixes[] = {"///", "//", "//!"}; |
| 45 | StringRef LongestPrefix; |
| 46 | for (StringRef KnownPrefix : KnownPrefixes) { |
| 47 | if (Comment.startswith(KnownPrefix)) { |
| 48 | size_t PrefixLength = KnownPrefix.size(); |
| 49 | while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ') |
| 50 | ++PrefixLength; |
| 51 | if (PrefixLength > LongestPrefix.size()) |
| 52 | LongestPrefix = Comment.substr(0, PrefixLength); |
| 53 | } |
| 54 | } |
| 55 | return LongestPrefix; |
| 56 | } |
| 57 | |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 58 | static BreakableToken::Split getCommentSplit(StringRef Text, |
| 59 | unsigned ContentStartColumn, |
| 60 | unsigned ColumnLimit, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 61 | unsigned TabWidth, |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 62 | encoding::Encoding Encoding) { |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 63 | if (ColumnLimit <= ContentStartColumn + 1) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 64 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 67 | unsigned MaxSplitBytes = 0; |
| 68 | |
| 69 | for (unsigned NumChars = 0; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 70 | NumChars < MaxSplit && MaxSplitBytes < Text.size();) { |
| 71 | unsigned BytesInChar = |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 72 | encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding); |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 73 | NumChars += |
| 74 | encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar), |
| 75 | ContentStartColumn, TabWidth, Encoding); |
| 76 | MaxSplitBytes += BytesInChar; |
| 77 | } |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 78 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 79 | StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 80 | if (SpaceOffset == StringRef::npos || |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 81 | // Don't break at leading whitespace. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 82 | Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) { |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 83 | // Make sure that we don't break at leading whitespace that |
| 84 | // reaches past MaxSplit. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 85 | StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 86 | if (FirstNonWhitespace == StringRef::npos) |
| 87 | // If the comment is only whitespace, we cannot split. |
| 88 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 89 | SpaceOffset = Text.find_first_of( |
| 90 | Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace)); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 91 | } |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 92 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 93 | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks); |
| 94 | StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 95 | return BreakableToken::Split(BeforeCut.size(), |
| 96 | AfterCut.begin() - BeforeCut.end()); |
| 97 | } |
| 98 | return BreakableToken::Split(StringRef::npos, 0); |
| 99 | } |
| 100 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 101 | static BreakableToken::Split |
| 102 | getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit, |
| 103 | unsigned TabWidth, encoding::Encoding Encoding) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 104 | // FIXME: Reduce unit test case. |
| 105 | if (Text.empty()) |
| 106 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 107 | if (ColumnLimit <= UsedColumns) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 108 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 71d95d6 | 2013-11-26 10:38:53 +0000 | [diff] [blame] | 109 | unsigned MaxSplit = ColumnLimit - UsedColumns; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 110 | StringRef::size_type SpaceOffset = 0; |
| 111 | StringRef::size_type SlashOffset = 0; |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 112 | StringRef::size_type WordStartOffset = 0; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 113 | StringRef::size_type SplitPoint = 0; |
| 114 | for (unsigned Chars = 0;;) { |
| 115 | unsigned Advance; |
| 116 | if (Text[0] == '\\') { |
| 117 | Advance = encoding::getEscapeSequenceLength(Text); |
| 118 | Chars += Advance; |
| 119 | } else { |
| 120 | Advance = encoding::getCodePointNumBytes(Text[0], Encoding); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 121 | Chars += encoding::columnWidthWithTabs( |
| 122 | Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Daniel Jasper | e4b48c6 | 2015-01-21 19:50:35 +0000 | [diff] [blame] | 125 | if (Chars > MaxSplit || Text.size() <= Advance) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 128 | if (IsBlank(Text[0])) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 129 | SpaceOffset = SplitPoint; |
| 130 | if (Text[0] == '/') |
| 131 | SlashOffset = SplitPoint; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 132 | if (Advance == 1 && !isAlphanumeric(Text[0])) |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 133 | WordStartOffset = SplitPoint; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 134 | |
| 135 | SplitPoint += Advance; |
| 136 | Text = Text.substr(Advance); |
| 137 | } |
| 138 | |
| 139 | if (SpaceOffset != 0) |
| 140 | return BreakableToken::Split(SpaceOffset + 1, 0); |
| 141 | if (SlashOffset != 0) |
| 142 | return BreakableToken::Split(SlashOffset + 1, 0); |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 143 | if (WordStartOffset != 0) |
| 144 | return BreakableToken::Split(WordStartOffset + 1, 0); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 145 | if (SplitPoint != 0) |
| 146 | return BreakableToken::Split(SplitPoint, 0); |
| 147 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 150 | bool switchesFormatting(const FormatToken &Token) { |
| 151 | assert((Token.is(TT_BlockComment) || Token.is(TT_LineComment)) && |
| 152 | "formatting regions are switched by comment tokens"); |
| 153 | StringRef Content = Token.TokenText.substr(2).ltrim(); |
| 154 | return Content.startswith("clang-format on") || |
| 155 | Content.startswith("clang-format off"); |
| 156 | } |
| 157 | |
| 158 | unsigned |
| 159 | BreakableToken::getLineLengthAfterCompression(unsigned RemainingTokenColumns, |
| 160 | Split Split) const { |
| 161 | // Example: consider the content |
| 162 | // lala lala |
| 163 | // - RemainingTokenColumns is the original number of columns, 10; |
| 164 | // - Split is (4, 2), denoting the two spaces between the two words; |
| 165 | // |
| 166 | // We compute the number of columns when the split is compressed into a single |
| 167 | // space, like: |
| 168 | // lala lala |
| 169 | return RemainingTokenColumns + 1 - Split.second; |
| 170 | } |
| 171 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 172 | unsigned BreakableSingleLineToken::getLineCount() const { return 1; } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 173 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 174 | unsigned BreakableSingleLineToken::getLineLengthAfterSplit( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 175 | unsigned LineIndex, unsigned TailOffset, |
| 176 | StringRef::size_type Length) const { |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 177 | return StartColumn + Prefix.size() + Postfix.size() + |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 178 | encoding::columnWidthWithTabs(Line.substr(TailOffset, Length), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 179 | StartColumn + Prefix.size(), |
| 180 | Style.TabWidth, Encoding); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 183 | BreakableSingleLineToken::BreakableSingleLineToken( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 184 | const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn, |
| 185 | StringRef Prefix, StringRef Postfix, bool InPPDirective, |
| 186 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 187 | : BreakableToken(Tok, IndentLevel, InPPDirective, Encoding, Style), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 188 | StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) { |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 189 | assert(Tok.TokenText.endswith(Postfix)); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 190 | Line = Tok.TokenText.substr( |
| 191 | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 194 | BreakableStringLiteral::BreakableStringLiteral( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 195 | const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn, |
| 196 | StringRef Prefix, StringRef Postfix, bool InPPDirective, |
| 197 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 198 | : BreakableSingleLineToken(Tok, IndentLevel, StartColumn, Prefix, Postfix, |
| 199 | InPPDirective, Encoding, Style) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 200 | |
| 201 | BreakableToken::Split |
| 202 | BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 203 | unsigned ColumnLimit) const { |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 204 | return getStringSplit(Line.substr(TailOffset), |
| 205 | StartColumn + Prefix.size() + Postfix.size(), |
| 206 | ColumnLimit, Style.TabWidth, Encoding); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 209 | void BreakableStringLiteral::insertBreak(unsigned LineIndex, |
| 210 | unsigned TailOffset, Split Split, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 211 | WhitespaceManager &Whitespaces) { |
Daniel Jasper | d07c2ee | 2014-01-14 09:53:07 +0000 | [diff] [blame] | 212 | unsigned LeadingSpaces = StartColumn; |
| 213 | // The '@' of an ObjC string literal (@"Test") does not become part of the |
| 214 | // string token. |
| 215 | // FIXME: It might be a cleaner solution to merge the tokens as a |
| 216 | // precomputation step. |
| 217 | if (Prefix.startswith("@")) |
| 218 | --LeadingSpaces; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 219 | Whitespaces.replaceWhitespaceInToken( |
| 220 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
Daniel Jasper | d07c2ee | 2014-01-14 09:53:07 +0000 | [diff] [blame] | 221 | Prefix, InPPDirective, 1, IndentLevel, LeadingSpaces); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 224 | BreakableComment::BreakableComment(const FormatToken &Token, |
| 225 | unsigned IndentLevel, unsigned StartColumn, |
| 226 | unsigned OriginalStartColumn, |
| 227 | bool FirstInLine, bool InPPDirective, |
| 228 | encoding::Encoding Encoding, |
| 229 | const FormatStyle &Style) |
| 230 | : BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style), |
| 231 | StartColumn(StartColumn), OriginalStartColumn(OriginalStartColumn), |
| 232 | FirstInLine(FirstInLine) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 233 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 234 | unsigned BreakableComment::getLineCount() const { return Lines.size(); } |
| 235 | |
| 236 | BreakableToken::Split BreakableComment::getSplit(unsigned LineIndex, |
| 237 | unsigned TailOffset, |
| 238 | unsigned ColumnLimit) const { |
| 239 | return getCommentSplit(Content[LineIndex].substr(TailOffset), |
| 240 | getContentStartColumn(LineIndex, TailOffset), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 241 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 244 | void BreakableComment::compressWhitespace(unsigned LineIndex, |
| 245 | unsigned TailOffset, Split Split, |
| 246 | WhitespaceManager &Whitespaces) { |
| 247 | StringRef Text = Content[LineIndex].substr(TailOffset); |
| 248 | // Text is relative to the content line, but Whitespaces operates relative to |
| 249 | // the start of the corresponding token, so compute the start of the Split |
| 250 | // that needs to be compressed into a single space relative to the start of |
| 251 | // its token. |
| 252 | unsigned BreakOffsetInToken = |
| 253 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 254 | unsigned CharsToRemove = Split.second; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 255 | Whitespaces.replaceWhitespaceInToken( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 256 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", "", |
| 257 | /*InPPDirective=*/false, |
| 258 | /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1); |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 261 | BreakableToken::Split |
| 262 | BreakableComment::getReflowSplit(StringRef Text, StringRef ReflowPrefix, |
| 263 | unsigned PreviousEndColumn, |
| 264 | unsigned ColumnLimit) const { |
| 265 | unsigned ReflowStartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 266 | StringRef TrimmedText = Text.rtrim(Blanks); |
| 267 | // This is the width of the resulting line in case the full line of Text gets |
| 268 | // reflown up starting at ReflowStartColumn. |
| 269 | unsigned FullWidth = ReflowStartColumn + encoding::columnWidthWithTabs( |
| 270 | TrimmedText, ReflowStartColumn, |
| 271 | Style.TabWidth, Encoding); |
| 272 | // If the full line fits up, we return a reflow split after it, |
| 273 | // otherwise we compute the largest piece of text that fits after |
| 274 | // ReflowStartColumn. |
| 275 | Split ReflowSplit = |
| 276 | FullWidth <= ColumnLimit |
| 277 | ? Split(TrimmedText.size(), Text.size() - TrimmedText.size()) |
| 278 | : getCommentSplit(Text, ReflowStartColumn, ColumnLimit, |
| 279 | Style.TabWidth, Encoding); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 280 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 281 | // We need to be extra careful here, because while it's OK to keep a long line |
| 282 | // if it can't be broken into smaller pieces (like when the first word of a |
| 283 | // long line is longer than the column limit), it's not OK to reflow that long |
| 284 | // word up. So we recompute the size of the previous line after reflowing and |
| 285 | // only return the reflow split if that's under the line limit. |
| 286 | if (ReflowSplit.first != StringRef::npos && |
| 287 | // Check if the width of the newly reflown line is under the limit. |
| 288 | PreviousEndColumn + ReflowPrefix.size() + |
| 289 | encoding::columnWidthWithTabs(Text.substr(0, ReflowSplit.first), |
| 290 | PreviousEndColumn + |
| 291 | ReflowPrefix.size(), |
| 292 | Style.TabWidth, Encoding) <= |
| 293 | ColumnLimit) { |
| 294 | return ReflowSplit; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 295 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 296 | return Split(StringRef::npos, 0); |
| 297 | } |
| 298 | |
| 299 | const FormatToken &BreakableComment::tokenAt(unsigned LineIndex) const { |
| 300 | return Tokens[LineIndex] ? *Tokens[LineIndex] : Tok; |
| 301 | } |
| 302 | |
| 303 | static bool mayReflowContent(StringRef Content) { |
| 304 | Content = Content.trim(Blanks); |
| 305 | // Simple heuristic for what to reflow: content should contain at least two |
| 306 | // characters and either the first or second character must be |
| 307 | // non-punctuation. |
| 308 | return Content.size() >= 2 && !Content.endswith("\\") && |
| 309 | // Note that this is UTF-8 safe, since if isPunctuation(Content[0]) is |
| 310 | // true, then the first code point must be 1 byte long. |
| 311 | (!isPunctuation(Content[0]) || !isPunctuation(Content[1])); |
| 312 | } |
| 313 | |
| 314 | bool BreakableComment::mayReflow(unsigned LineIndex) const { |
| 315 | return LineIndex > 0 && mayReflowContent(Content[LineIndex]) && |
| 316 | !Tok.Finalized && !switchesFormatting(tokenAt(LineIndex)) && |
| 317 | (!Tok.is(TT_LineComment) || |
| 318 | OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1]); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 321 | BreakableBlockComment::BreakableBlockComment( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 322 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 323 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 324 | encoding::Encoding Encoding, const FormatStyle &Style) |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 325 | : BreakableComment(Token, IndentLevel, StartColumn, OriginalStartColumn, |
| 326 | FirstInLine, InPPDirective, Encoding, Style) { |
| 327 | assert(Tok.is(TT_BlockComment) && |
| 328 | "block comment section must start with a block comment"); |
| 329 | |
| 330 | StringRef TokenText(Tok.TokenText); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 331 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 332 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 333 | |
| 334 | int IndentDelta = StartColumn - OriginalStartColumn; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 335 | Content.resize(Lines.size()); |
| 336 | Content[0] = Lines[0]; |
| 337 | ContentColumn.resize(Lines.size()); |
| 338 | // Account for the initial '/*'. |
| 339 | ContentColumn[0] = StartColumn + 2; |
| 340 | Tokens.resize(Lines.size()); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 341 | for (size_t i = 1; i < Lines.size(); ++i) |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 342 | adjustWhitespace(i, IndentDelta); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 343 | |
| 344 | Decoration = "* "; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 345 | if (Lines.size() == 1 && !FirstInLine) { |
| 346 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 347 | // and available horizontal space can be too small to align consecutive |
| 348 | // lines with the first one. |
| 349 | // FIXME: We could, probably, align them to current indentation level, but |
| 350 | // now we just wrap them without stars. |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 351 | Decoration = ""; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 352 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 353 | for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) { |
| 354 | // If the last line is empty, the closing "*/" will have a star. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 355 | if (i + 1 == e && Content[i].empty()) |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 356 | break; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 357 | if (!Content[i].empty() && i + 1 != e && |
| 358 | Decoration.startswith(Content[i])) |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 359 | continue; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 360 | while (!Content[i].startswith(Decoration)) |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 361 | Decoration = Decoration.substr(0, Decoration.size() - 1); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 362 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 363 | |
| 364 | LastLineNeedsDecoration = true; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 365 | IndentAtLineBreak = ContentColumn[0] + 1; |
| 366 | for (size_t i = 1, e = Lines.size(); i < e; ++i) { |
| 367 | if (Content[i].empty()) { |
| 368 | if (i + 1 == e) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 369 | // Empty last line means that we already have a star as a part of the |
| 370 | // trailing */. We also need to preserve whitespace, so that */ is |
| 371 | // correctly indented. |
| 372 | LastLineNeedsDecoration = false; |
| 373 | } else if (Decoration.empty()) { |
| 374 | // For all other lines, set the start column to 0 if they're empty, so |
| 375 | // we do not insert trailing whitespace anywhere. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 376 | ContentColumn[i] = 0; |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 377 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 380 | |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 381 | // The first line already excludes the star. |
| 382 | // For all other lines, adjust the line to exclude the star and |
| 383 | // (optionally) the first whitespace. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 384 | unsigned DecorationSize = Decoration.startswith(Content[i]) |
| 385 | ? Content[i].size() |
| 386 | : Decoration.size(); |
| 387 | ContentColumn[i] += DecorationSize; |
| 388 | Content[i] = Content[i].substr(DecorationSize); |
| 389 | if (!Decoration.startswith(Content[i])) |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 390 | IndentAtLineBreak = |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 391 | std::min<int>(IndentAtLineBreak, std::max(0, ContentColumn[i])); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 392 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 393 | IndentAtLineBreak = |
| 394 | std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
| 395 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 396 | DEBUG({ |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 397 | llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 398 | for (size_t i = 0; i < Lines.size(); ++i) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 399 | llvm::dbgs() << i << " |" << Content[i] << "| " |
| 400 | << (Content[i].data() - Lines[i].data()) << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 401 | } |
| 402 | }); |
| 403 | } |
| 404 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 405 | void BreakableBlockComment::adjustWhitespace(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 406 | int IndentDelta) { |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 407 | // When in a preprocessor directive, the trailing backslash in a block comment |
| 408 | // is not needed, but can serve a purpose of uniformity with necessary escaped |
| 409 | // newlines outside the comment. In this case we remove it here before |
| 410 | // trimming the trailing whitespace. The backslash will be re-added later when |
| 411 | // inserting a line break. |
| 412 | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
| 413 | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")) |
| 414 | --EndOfPreviousLine; |
| 415 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 416 | // Calculate the end of the non-whitespace text in the previous line. |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 417 | EndOfPreviousLine = |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 418 | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 419 | if (EndOfPreviousLine == StringRef::npos) |
| 420 | EndOfPreviousLine = 0; |
| 421 | else |
| 422 | ++EndOfPreviousLine; |
| 423 | // Calculate the start of the non-whitespace text in the current line. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 424 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 425 | if (StartOfLine == StringRef::npos) |
Daniel Jasper | d6e6188 | 2015-06-17 12:23:15 +0000 | [diff] [blame] | 426 | StartOfLine = Lines[LineIndex].rtrim("\r\n").size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 427 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 428 | StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 429 | // Adjust Lines to only contain relevant text. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 430 | size_t PreviousContentOffset = |
| 431 | Content[LineIndex - 1].data() - Lines[LineIndex - 1].data(); |
| 432 | Content[LineIndex - 1] = Lines[LineIndex - 1].substr( |
| 433 | PreviousContentOffset, EndOfPreviousLine - PreviousContentOffset); |
| 434 | Content[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
Manuel Klimek | 34d1515 | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 435 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 436 | // Adjust the start column uniformly across all lines. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 437 | ContentColumn[LineIndex] = |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 438 | encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) + |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 439 | IndentDelta; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 442 | unsigned BreakableBlockComment::getLineLengthAfterSplit( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 443 | unsigned LineIndex, unsigned TailOffset, |
| 444 | StringRef::size_type Length) const { |
| 445 | unsigned ContentStartColumn = getContentStartColumn(LineIndex, TailOffset); |
| 446 | unsigned LineLength = |
| 447 | ContentStartColumn + encoding::columnWidthWithTabs( |
| 448 | Content[LineIndex].substr(TailOffset, Length), |
| 449 | ContentStartColumn, Style.TabWidth, Encoding); |
| 450 | // The last line gets a "*/" postfix. |
| 451 | if (LineIndex + 1 == Lines.size()) { |
| 452 | LineLength += 2; |
| 453 | // We never need a decoration when breaking just the trailing "*/" postfix. |
| 454 | // Note that checking that Length == 0 is not enough, since Length could |
| 455 | // also be StringRef::npos. |
| 456 | if (Content[LineIndex].substr(TailOffset, Length).empty()) { |
| 457 | LineLength -= Decoration.size(); |
| 458 | } |
| 459 | } |
| 460 | return LineLength; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 464 | Split Split, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 465 | WhitespaceManager &Whitespaces) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 466 | StringRef Text = Content[LineIndex].substr(TailOffset); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 467 | StringRef Prefix = Decoration; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 468 | // We need this to account for the case when we have a decoration "* " for all |
| 469 | // the lines except for the last one, where the star in "*/" acts as a |
| 470 | // decoration. |
| 471 | unsigned LocalIndentAtLineBreak = IndentAtLineBreak; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 472 | if (LineIndex + 1 == Lines.size() && |
| 473 | Text.size() == Split.first + Split.second) { |
| 474 | // For the last line we need to break before "*/", but not to add "* ". |
| 475 | Prefix = ""; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 476 | if (LocalIndentAtLineBreak >= 2) |
| 477 | LocalIndentAtLineBreak -= 2; |
| 478 | } |
| 479 | // The split offset is from the beginning of the line. Convert it to an offset |
| 480 | // from the beginning of the token text. |
| 481 | unsigned BreakOffsetInToken = |
| 482 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 483 | unsigned CharsToRemove = Split.second; |
| 484 | assert(LocalIndentAtLineBreak >= Prefix.size()); |
| 485 | Whitespaces.replaceWhitespaceInToken( |
| 486 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", Prefix, |
| 487 | InPPDirective, /*Newlines=*/1, IndentLevel, |
| 488 | /*Spaces=*/LocalIndentAtLineBreak - Prefix.size()); |
| 489 | } |
| 490 | |
| 491 | BreakableToken::Split BreakableBlockComment::getSplitBefore( |
| 492 | unsigned LineIndex, |
| 493 | unsigned PreviousEndColumn, |
| 494 | unsigned ColumnLimit) const { |
| 495 | if (!mayReflow(LineIndex)) |
| 496 | return Split(StringRef::npos, 0); |
| 497 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 498 | return getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn, |
| 499 | ColumnLimit); |
| 500 | } |
| 501 | |
| 502 | unsigned BreakableBlockComment::getReflownColumn( |
| 503 | StringRef Content, |
| 504 | unsigned LineIndex, |
| 505 | unsigned PreviousEndColumn) const { |
| 506 | unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 507 | // If this is the last line, it will carry around its '*/' postfix. |
| 508 | unsigned PostfixLength = (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 509 | // The line is composed of previous text, reflow prefix, reflown text and |
| 510 | // postfix. |
| 511 | unsigned ReflownColumn = |
| 512 | StartColumn + encoding::columnWidthWithTabs(Content, StartColumn, |
| 513 | Style.TabWidth, Encoding) + |
| 514 | PostfixLength; |
| 515 | return ReflownColumn; |
| 516 | } |
| 517 | |
| 518 | unsigned BreakableBlockComment::getLineLengthAfterSplitBefore( |
| 519 | unsigned LineIndex, unsigned TailOffset, |
| 520 | unsigned PreviousEndColumn, |
| 521 | unsigned ColumnLimit, |
| 522 | Split SplitBefore) const { |
| 523 | if (SplitBefore.first == StringRef::npos || |
| 524 | SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) { |
| 525 | // A piece of line, not the whole, gets reflown. |
| 526 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 527 | } else { |
| 528 | // The whole line gets reflown, need to check if we need to insert a break |
| 529 | // for the postfix or not. |
| 530 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 531 | unsigned ReflownColumn = |
| 532 | getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn); |
| 533 | if (ReflownColumn <= ColumnLimit) { |
| 534 | return ReflownColumn; |
| 535 | } |
| 536 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 537 | } |
| 538 | } |
| 539 | void BreakableBlockComment::replaceWhitespaceBefore( |
| 540 | unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, |
| 541 | Split SplitBefore, WhitespaceManager &Whitespaces) { |
| 542 | if (LineIndex == 0) return; |
| 543 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 544 | if (SplitBefore.first != StringRef::npos) { |
| 545 | // Here we need to reflow. |
| 546 | assert(Tokens[LineIndex - 1] == Tokens[LineIndex] && |
| 547 | "Reflowing whitespace within a token"); |
| 548 | // This is the offset of the end of the last line relative to the start of |
| 549 | // the token text in the token. |
| 550 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
| 551 | Content[LineIndex - 1].size() - |
| 552 | tokenAt(LineIndex).TokenText.data(); |
| 553 | unsigned WhitespaceLength = TrimmedContent.data() - |
| 554 | tokenAt(LineIndex).TokenText.data() - |
| 555 | WhitespaceOffsetInToken; |
| 556 | Whitespaces.replaceWhitespaceInToken( |
| 557 | tokenAt(LineIndex), WhitespaceOffsetInToken, |
| 558 | /*ReplaceChars=*/WhitespaceLength, |
| 559 | /*PreviousPostfix=*/"", |
| 560 | /*CurrentPrefix=*/ReflowPrefix, InPPDirective, |
| 561 | /*Newlines=*/0, IndentLevel, /*Spaces=*/0); |
| 562 | // Check if we need to also insert a break at the whitespace range. |
| 563 | // For this we first adapt the reflow split relative to the beginning of the |
| 564 | // content. |
| 565 | // Note that we don't need a penalty for this break, since it doesn't change |
| 566 | // the total number of lines. |
| 567 | Split BreakSplit = SplitBefore; |
| 568 | BreakSplit.first += TrimmedContent.data() - Content[LineIndex].data(); |
| 569 | unsigned ReflownColumn = |
| 570 | getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn); |
| 571 | if (ReflownColumn > ColumnLimit) { |
| 572 | insertBreak(LineIndex, 0, BreakSplit, Whitespaces); |
| 573 | } |
| 574 | return; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 577 | // Here no reflow with the previous line will happen. |
| 578 | // Fix the decoration of the line at LineIndex. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 579 | StringRef Prefix = Decoration; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 580 | if (Content[LineIndex].empty()) { |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 581 | if (LineIndex + 1 == Lines.size()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 582 | if (!LastLineNeedsDecoration) { |
| 583 | // If the last line was empty, we don't need a prefix, as the */ will |
| 584 | // line up with the decoration (if it exists). |
| 585 | Prefix = ""; |
| 586 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 587 | } else if (!Decoration.empty()) { |
| 588 | // For other empty lines, if we do have a decoration, adapt it to not |
| 589 | // contain a trailing whitespace. |
| 590 | Prefix = Prefix.substr(0, 1); |
| 591 | } |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 592 | } else { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 593 | if (ContentColumn[LineIndex] == 1) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 594 | // This line starts immediately after the decorating *. |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 595 | Prefix = Prefix.substr(0, 1); |
| 596 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 597 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 598 | // This is the offset of the end of the last line relative to the start of the |
| 599 | // token text in the token. |
| 600 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
| 601 | Content[LineIndex - 1].size() - |
| 602 | tokenAt(LineIndex).TokenText.data(); |
| 603 | unsigned WhitespaceLength = Content[LineIndex].data() - |
| 604 | tokenAt(LineIndex).TokenText.data() - |
| 605 | WhitespaceOffsetInToken; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 606 | Whitespaces.replaceWhitespaceInToken( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 607 | tokenAt(LineIndex), WhitespaceOffsetInToken, WhitespaceLength, "", |
| 608 | Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, |
| 609 | ContentColumn[LineIndex] - Prefix.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | unsigned |
| 613 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 614 | unsigned TailOffset) const { |
| 615 | // If we break, we always break at the predefined indent. |
| 616 | if (TailOffset != 0) |
| 617 | return IndentAtLineBreak; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 618 | return std::max(0, ContentColumn[LineIndex]); |
| 619 | } |
| 620 | |
| 621 | BreakableLineCommentSection::BreakableLineCommentSection( |
| 622 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
| 623 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
| 624 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 625 | : BreakableComment(Token, IndentLevel, StartColumn, OriginalStartColumn, |
| 626 | FirstInLine, InPPDirective, Encoding, Style) { |
| 627 | assert(Tok.is(TT_LineComment) && |
| 628 | "line comment section must start with a line comment"); |
| 629 | FormatToken *LineTok = nullptr; |
| 630 | for (const FormatToken *CurrentTok = &Tok; |
| 631 | CurrentTok && CurrentTok->is(TT_LineComment); |
| 632 | CurrentTok = CurrentTok->Next) { |
| 633 | LastLineTok = LineTok; |
| 634 | StringRef TokenText(CurrentTok->TokenText); |
| 635 | assert(TokenText.startswith("//")); |
| 636 | size_t FirstLineIndex = Lines.size(); |
| 637 | TokenText.split(Lines, "\n"); |
| 638 | Content.resize(Lines.size()); |
| 639 | ContentColumn.resize(Lines.size()); |
| 640 | OriginalContentColumn.resize(Lines.size()); |
| 641 | Tokens.resize(Lines.size()); |
| 642 | Prefix.resize(Lines.size()); |
| 643 | OriginalPrefix.resize(Lines.size()); |
| 644 | for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) { |
Krasimir Georgiev | e518e0b | 2017-01-30 21:00:01 +0000 | [diff] [blame^] | 645 | // We need to trim the blanks in case this is not the first line in a |
| 646 | // multiline comment. Then the indent is included in Lines[i]. |
| 647 | StringRef IndentPrefix = |
| 648 | getLineCommentIndentPrefix(Lines[i].ltrim(Blanks)); |
| 649 | assert(IndentPrefix.startswith("//")); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 650 | OriginalPrefix[i] = Prefix[i] = IndentPrefix; |
| 651 | if (Lines[i].size() > Prefix[i].size() && |
| 652 | isAlphanumeric(Lines[i][Prefix[i].size()])) { |
| 653 | if (Prefix[i] == "//") |
| 654 | Prefix[i] = "// "; |
| 655 | else if (Prefix[i] == "///") |
| 656 | Prefix[i] = "/// "; |
| 657 | else if (Prefix[i] == "//!") |
| 658 | Prefix[i] = "//! "; |
| 659 | } |
| 660 | |
| 661 | Tokens[i] = LineTok; |
| 662 | Content[i] = Lines[i].substr(IndentPrefix.size()); |
| 663 | OriginalContentColumn[i] = |
| 664 | StartColumn + |
| 665 | encoding::columnWidthWithTabs(OriginalPrefix[i], |
| 666 | StartColumn, |
| 667 | Style.TabWidth, |
| 668 | Encoding); |
| 669 | ContentColumn[i] = |
| 670 | StartColumn + |
| 671 | encoding::columnWidthWithTabs(Prefix[i], |
| 672 | StartColumn, |
| 673 | Style.TabWidth, |
| 674 | Encoding); |
| 675 | |
| 676 | // Calculate the end of the non-whitespace text in this line. |
| 677 | size_t EndOfLine = Content[i].find_last_not_of(Blanks); |
| 678 | if (EndOfLine == StringRef::npos) |
| 679 | EndOfLine = Content[i].size(); |
| 680 | else |
| 681 | ++EndOfLine; |
| 682 | Content[i] = Content[i].substr(0, EndOfLine); |
| 683 | } |
| 684 | LineTok = CurrentTok->Next; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | unsigned BreakableLineCommentSection::getLineLengthAfterSplit( |
| 689 | unsigned LineIndex, unsigned TailOffset, |
| 690 | StringRef::size_type Length) const { |
| 691 | unsigned ContentStartColumn = |
| 692 | (TailOffset == 0 ? ContentColumn[LineIndex] |
| 693 | : OriginalContentColumn[LineIndex]); |
| 694 | return ContentStartColumn + encoding::columnWidthWithTabs( |
| 695 | Content[LineIndex].substr(TailOffset, Length), |
| 696 | ContentStartColumn, Style.TabWidth, Encoding); |
| 697 | } |
| 698 | |
| 699 | void BreakableLineCommentSection::insertBreak(unsigned LineIndex, |
| 700 | unsigned TailOffset, Split Split, |
| 701 | WhitespaceManager &Whitespaces) { |
| 702 | StringRef Text = Content[LineIndex].substr(TailOffset); |
| 703 | // Compute the offset of the split relative to the beginning of the token |
| 704 | // text. |
| 705 | unsigned BreakOffsetInToken = |
| 706 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 707 | unsigned CharsToRemove = Split.second; |
| 708 | // Compute the size of the new indent, including the size of the new prefix of |
| 709 | // the newly broken line. |
| 710 | unsigned IndentAtLineBreak = OriginalContentColumn[LineIndex] + |
| 711 | Prefix[LineIndex].size() - |
| 712 | OriginalPrefix[LineIndex].size(); |
| 713 | assert(IndentAtLineBreak >= Prefix[LineIndex].size()); |
| 714 | Whitespaces.replaceWhitespaceInToken( |
| 715 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", |
| 716 | Prefix[LineIndex], InPPDirective, /*Newlines=*/1, IndentLevel, |
| 717 | /*Spaces=*/IndentAtLineBreak - Prefix[LineIndex].size()); |
| 718 | } |
| 719 | |
| 720 | BreakableComment::Split BreakableLineCommentSection::getSplitBefore( |
| 721 | unsigned LineIndex, |
| 722 | unsigned PreviousEndColumn, |
| 723 | unsigned ColumnLimit) const { |
| 724 | if (!mayReflow(LineIndex)) return Split(StringRef::npos, 0); |
| 725 | return getReflowSplit(Content[LineIndex], ReflowPrefix, PreviousEndColumn, |
| 726 | ColumnLimit); |
| 727 | } |
| 728 | |
| 729 | unsigned BreakableLineCommentSection::getLineLengthAfterSplitBefore( |
| 730 | unsigned LineIndex, unsigned TailOffset, |
| 731 | unsigned PreviousEndColumn, |
| 732 | unsigned ColumnLimit, |
| 733 | Split SplitBefore) const { |
| 734 | if (SplitBefore.first == StringRef::npos || |
| 735 | SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) { |
| 736 | // A piece of line, not the whole line, gets reflown. |
| 737 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 738 | } else { |
| 739 | // The whole line gets reflown. |
| 740 | unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 741 | return StartColumn + encoding::columnWidthWithTabs(Content[LineIndex], |
| 742 | StartColumn, |
| 743 | Style.TabWidth, |
| 744 | Encoding); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | void BreakableLineCommentSection::replaceWhitespaceBefore( |
| 749 | unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, |
| 750 | Split SplitBefore, WhitespaceManager &Whitespaces) { |
| 751 | // If this is the first line of a token, we need to inform Whitespace Manager |
| 752 | // about it: either adapt the whitespace range preceding it, or mark it as an |
| 753 | // untouchable token. |
| 754 | // This happens for instance here: |
| 755 | // // line 1 \ |
| 756 | // // line 2 |
| 757 | if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]) { |
| 758 | if (SplitBefore.first != StringRef::npos) { |
| 759 | // Reflow happens between tokens. Replace the whitespace between the |
| 760 | // tokens by the empty string. |
| 761 | Whitespaces.replaceWhitespace(*Tokens[LineIndex], |
| 762 | /*Newlines=*/0, |
| 763 | /*IndentLevel=*/IndentLevel, |
| 764 | /*Spaces=*/0, |
| 765 | /*StartOfTokenColumn=*/StartColumn, |
| 766 | /*InPPDirective=*/false); |
| 767 | // Replace the indent and prefix of the token with the reflow prefix. |
| 768 | unsigned WhitespaceLength = |
| 769 | Content[LineIndex].data() - tokenAt(LineIndex).TokenText.data(); |
| 770 | Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], |
| 771 | /*Offset=*/0, |
| 772 | /*ReplaceChars=*/WhitespaceLength, |
| 773 | /*PreviousPostfix=*/"", |
| 774 | /*CurrentPrefix=*/ReflowPrefix, |
| 775 | /*InPPDirective=*/false, |
| 776 | /*Newlines=*/0, |
| 777 | /*IndentLevel=*/IndentLevel, |
| 778 | /*Spaces=*/0); |
| 779 | } else { |
| 780 | // This is the first line for the current token, but no reflow with the |
| 781 | // previous token is necessary. However, we still may need to adjust the |
| 782 | // start column. |
| 783 | unsigned LineColumn = |
| 784 | ContentColumn[LineIndex] - |
| 785 | (Content[LineIndex].data() - Lines[LineIndex].data()); |
| 786 | if (tokenAt(LineIndex).OriginalColumn != LineColumn) { |
| 787 | Whitespaces.replaceWhitespace(*Tokens[LineIndex], |
| 788 | /*Newlines=*/1, |
| 789 | /*IndentLevel=*/IndentLevel, |
| 790 | /*Spaces=*/LineColumn, |
| 791 | /*StartOfTokenColumn=*/LineColumn, |
| 792 | /*InPPDirective=*/false); |
| 793 | } else { |
| 794 | // The whitespace preceding the first line of this token does not need |
| 795 | // to be touched. |
| 796 | Whitespaces.addUntouchableToken(tokenAt(LineIndex), |
| 797 | /*InPPDirective=*/false); |
| 798 | } |
| 799 | } |
| 800 | } else if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) { |
| 801 | // This is not the first line of the token. Adjust the prefix if necessary. |
| 802 | |
| 803 | // Take care of the space possibly introduced after a decoration. |
| 804 | assert(Prefix[LineIndex] == (OriginalPrefix[LineIndex] + " ").str() && |
| 805 | "Expecting a block comment decoration to differ from original by " |
| 806 | "at most a space"); |
| 807 | Whitespaces.replaceWhitespaceInToken( |
| 808 | tokenAt(LineIndex), OriginalPrefix[LineIndex].size(), 0, "", "", |
| 809 | /*InPPDirective=*/false, |
| 810 | /*Newlines=*/0, /*IndentLevel=*/0, |
| 811 | /*Spaces=*/1); |
| 812 | } |
| 813 | // Add a break after a reflow split has been introduced, if necessary. |
| 814 | // Note that this break doesn't need to be penalized, since it doesn't change |
| 815 | // the number of lines. |
| 816 | if (SplitBefore.first != StringRef::npos && |
| 817 | SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) { |
| 818 | insertBreak(LineIndex, 0, SplitBefore, Whitespaces); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | void BreakableLineCommentSection::updateNextToken(LineState& State) const { |
| 823 | if (LastLineTok) { |
| 824 | State.NextToken = LastLineTok->Next; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | unsigned |
| 829 | BreakableLineCommentSection::getContentStartColumn(unsigned LineIndex, |
| 830 | unsigned TailOffset) const { |
| 831 | if (TailOffset != 0) { |
| 832 | return OriginalContentColumn[LineIndex]; |
| 833 | } |
| 834 | return ContentColumn[LineIndex]; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 837 | } // namespace format |
| 838 | } // namespace clang |