Alexander Kornienko | 70ce788 | 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 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-token-breaker" |
| 17 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace format { |
| 27 | |
Craig Topper | cf1e216 | 2013-06-30 22:44:02 +0000 | [diff] [blame] | 28 | static const char *const Blanks = " \t\v\f"; |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 29 | static bool IsBlank(char C) { |
| 30 | switch (C) { |
| 31 | case ' ': |
| 32 | case '\t': |
| 33 | case '\v': |
| 34 | case '\f': |
| 35 | return true; |
| 36 | default: |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | |
Craig Topper | b8f7164 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 41 | static BreakableToken::Split getCommentSplit(StringRef Text, |
| 42 | unsigned ContentStartColumn, |
| 43 | unsigned ColumnLimit, |
| 44 | encoding::Encoding Encoding) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 45 | if (ColumnLimit <= ContentStartColumn + 1) |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 46 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 47 | |
| 48 | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 49 | unsigned MaxSplitBytes = 0; |
| 50 | |
| 51 | for (unsigned NumChars = 0; |
| 52 | NumChars < MaxSplit && MaxSplitBytes < Text.size(); ++NumChars) |
| 53 | MaxSplitBytes += |
| 54 | encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding); |
| 55 | |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 56 | StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 57 | if (SpaceOffset == StringRef::npos || |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 58 | // Don't break at leading whitespace. |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 59 | Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) { |
Manuel Klimek | be9ed77 | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 60 | // Make sure that we don't break at leading whitespace that |
| 61 | // reaches past MaxSplit. |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 62 | StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks); |
Manuel Klimek | be9ed77 | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 63 | if (FirstNonWhitespace == StringRef::npos) |
| 64 | // If the comment is only whitespace, we cannot split. |
| 65 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 66 | SpaceOffset = Text.find_first_of( |
| 67 | Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace)); |
Manuel Klimek | be9ed77 | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 68 | } |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 69 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 70 | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks); |
| 71 | StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 72 | return BreakableToken::Split(BeforeCut.size(), |
| 73 | AfterCut.begin() - BeforeCut.end()); |
| 74 | } |
| 75 | return BreakableToken::Split(StringRef::npos, 0); |
| 76 | } |
| 77 | |
Craig Topper | b8f7164 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 78 | static BreakableToken::Split getStringSplit(StringRef Text, |
| 79 | unsigned ContentStartColumn, |
| 80 | unsigned ColumnLimit, |
| 81 | encoding::Encoding Encoding) { |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 82 | // FIXME: Reduce unit test case. |
| 83 | if (Text.empty()) |
| 84 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 85 | if (ColumnLimit <= ContentStartColumn) |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 86 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 87 | unsigned MaxSplit = |
| 88 | std::min<unsigned>(ColumnLimit - ContentStartColumn, |
| 89 | encoding::getCodePointCount(Text, Encoding) - 1); |
| 90 | StringRef::size_type SpaceOffset = 0; |
| 91 | StringRef::size_type SlashOffset = 0; |
Alexander Kornienko | f2b2c7d | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 92 | StringRef::size_type WordStartOffset = 0; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 93 | StringRef::size_type SplitPoint = 0; |
| 94 | for (unsigned Chars = 0;;) { |
| 95 | unsigned Advance; |
| 96 | if (Text[0] == '\\') { |
| 97 | Advance = encoding::getEscapeSequenceLength(Text); |
| 98 | Chars += Advance; |
| 99 | } else { |
| 100 | Advance = encoding::getCodePointNumBytes(Text[0], Encoding); |
| 101 | Chars += 1; |
| 102 | } |
| 103 | |
| 104 | if (Chars > MaxSplit) |
| 105 | break; |
| 106 | |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 107 | if (IsBlank(Text[0])) |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 108 | SpaceOffset = SplitPoint; |
| 109 | if (Text[0] == '/') |
| 110 | SlashOffset = SplitPoint; |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 111 | if (Advance == 1 && !isAlphanumeric(Text[0])) |
Alexander Kornienko | f2b2c7d | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 112 | WordStartOffset = SplitPoint; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 113 | |
| 114 | SplitPoint += Advance; |
| 115 | Text = Text.substr(Advance); |
| 116 | } |
| 117 | |
| 118 | if (SpaceOffset != 0) |
| 119 | return BreakableToken::Split(SpaceOffset + 1, 0); |
| 120 | if (SlashOffset != 0) |
| 121 | return BreakableToken::Split(SlashOffset + 1, 0); |
Alexander Kornienko | f2b2c7d | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 122 | if (WordStartOffset != 0) |
| 123 | return BreakableToken::Split(WordStartOffset + 1, 0); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 124 | if (SplitPoint != 0) |
| 125 | return BreakableToken::Split(SplitPoint, 0); |
| 126 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 129 | unsigned BreakableSingleLineToken::getLineCount() const { return 1; } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 130 | |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 131 | unsigned BreakableSingleLineToken::getLineLengthAfterSplit( |
| 132 | unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 133 | return StartColumn + Prefix.size() + Postfix.size() + |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 134 | encoding::getCodePointCount(Line.substr(Offset, Length), Encoding); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 137 | BreakableSingleLineToken::BreakableSingleLineToken( |
| 138 | const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, |
| 139 | StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding) |
| 140 | : BreakableToken(Tok, InPPDirective, Encoding), StartColumn(StartColumn), |
| 141 | Prefix(Prefix), Postfix(Postfix) { |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 142 | assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); |
| 143 | Line = Tok.TokenText.substr( |
| 144 | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 147 | BreakableStringLiteral::BreakableStringLiteral(const FormatToken &Tok, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 148 | unsigned StartColumn, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 149 | bool InPPDirective, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 150 | encoding::Encoding Encoding) |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 151 | : BreakableSingleLineToken(Tok, StartColumn, "\"", "\"", InPPDirective, |
| 152 | Encoding) {} |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 153 | |
| 154 | BreakableToken::Split |
| 155 | BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 156 | unsigned ColumnLimit) const { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 157 | return getStringSplit(Line.substr(TailOffset), StartColumn + 2, ColumnLimit, |
| 158 | Encoding); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 161 | void BreakableStringLiteral::insertBreak(unsigned LineIndex, |
| 162 | unsigned TailOffset, Split Split, |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 163 | WhitespaceManager &Whitespaces) { |
| 164 | Whitespaces.replaceWhitespaceInToken( |
| 165 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
| 166 | Prefix, InPPDirective, 1, StartColumn); |
| 167 | } |
| 168 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 169 | static StringRef getLineCommentPrefix(StringRef Comment) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 170 | const char *KnownPrefixes[] = { "/// ", "///", "// ", "//" }; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 171 | for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i) |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 172 | if (Comment.startswith(KnownPrefixes[i])) |
| 173 | return KnownPrefixes[i]; |
| 174 | return ""; |
| 175 | } |
| 176 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 177 | BreakableLineComment::BreakableLineComment(const FormatToken &Token, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 178 | unsigned StartColumn, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 179 | bool InPPDirective, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 180 | encoding::Encoding Encoding) |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 181 | : BreakableSingleLineToken(Token, StartColumn, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 182 | getLineCommentPrefix(Token.TokenText), "", |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 183 | InPPDirective, Encoding) { |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 184 | OriginalPrefix = Prefix; |
| 185 | if (Token.TokenText.size() > Prefix.size() && |
| 186 | isAlphanumeric(Token.TokenText[Prefix.size()])) { |
| 187 | if (Prefix == "//") |
| 188 | Prefix = "// "; |
| 189 | else if (Prefix == "///") |
| 190 | Prefix = "/// "; |
| 191 | } |
| 192 | } |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 193 | |
| 194 | BreakableToken::Split |
| 195 | BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 196 | unsigned ColumnLimit) const { |
| 197 | return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 198 | ColumnLimit, Encoding); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 201 | void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 202 | Split Split, |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 203 | WhitespaceManager &Whitespaces) { |
| 204 | Whitespaces.replaceWhitespaceInToken( |
| 205 | Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, |
| 206 | Postfix, Prefix, InPPDirective, 1, StartColumn); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex, |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 211 | WhitespaceManager &Whitespaces) { |
| 212 | if (OriginalPrefix != Prefix) { |
| 213 | Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "", |
| 214 | false, 0, 1); |
| 215 | } |
| 216 | } |
| 217 | |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 218 | BreakableBlockComment::BreakableBlockComment( |
| 219 | const FormatStyle &Style, const FormatToken &Token, unsigned StartColumn, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 220 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
| 221 | encoding::Encoding Encoding) |
| 222 | : BreakableToken(Token, InPPDirective, Encoding) { |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 223 | StringRef TokenText(Token.TokenText); |
| 224 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 225 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 226 | |
| 227 | int IndentDelta = StartColumn - OriginalStartColumn; |
| 228 | bool NeedsStar = true; |
| 229 | LeadingWhitespace.resize(Lines.size()); |
| 230 | StartOfLineColumn.resize(Lines.size()); |
| 231 | if (Lines.size() == 1 && !FirstInLine) { |
| 232 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 233 | // and available horizontal space can be too small to align consecutive |
| 234 | // lines with the first one. |
| 235 | // FIXME: We could, probably, align them to current indentation level, but |
| 236 | // now we just wrap them without stars. |
| 237 | NeedsStar = false; |
| 238 | } |
| 239 | StartOfLineColumn[0] = StartColumn + 2; |
| 240 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 241 | adjustWhitespace(Style, i, IndentDelta); |
| 242 | if (Lines[i].empty()) |
| 243 | // If the last line is empty, the closing "*/" will have a star. |
| 244 | NeedsStar = NeedsStar && i + 1 == Lines.size(); |
| 245 | else |
| 246 | NeedsStar = NeedsStar && Lines[i][0] == '*'; |
| 247 | } |
| 248 | Decoration = NeedsStar ? "* " : ""; |
| 249 | IndentAtLineBreak = StartOfLineColumn[0] + 1; |
| 250 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 251 | if (Lines[i].empty()) { |
| 252 | if (!NeedsStar && i + 1 != Lines.size()) |
| 253 | // For all but the last line (which always ends in */), set the |
| 254 | // start column to 0 if they're empty, so we do not insert |
| 255 | // trailing whitespace anywhere. |
| 256 | StartOfLineColumn[i] = 0; |
| 257 | continue; |
| 258 | } |
| 259 | if (NeedsStar) { |
| 260 | // The first line already excludes the star. |
| 261 | // For all other lines, adjust the line to exclude the star and |
| 262 | // (optionally) the first whitespace. |
| 263 | int Offset = Lines[i].startswith("* ") ? 2 : 1; |
| 264 | StartOfLineColumn[i] += Offset; |
| 265 | Lines[i] = Lines[i].substr(Offset); |
| 266 | LeadingWhitespace[i] += Offset; |
| 267 | } |
| 268 | IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]); |
| 269 | } |
Daniel Jasper | cb4b40b | 2013-05-30 17:27:48 +0000 | [diff] [blame] | 270 | IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 271 | DEBUG({ |
| 272 | for (size_t i = 0; i < Lines.size(); ++i) { |
| 273 | llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i] |
| 274 | << "\n"; |
| 275 | } |
| 276 | }); |
| 277 | } |
| 278 | |
| 279 | void BreakableBlockComment::adjustWhitespace(const FormatStyle &Style, |
| 280 | unsigned LineIndex, |
| 281 | int IndentDelta) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 282 | // When in a preprocessor directive, the trailing backslash in a block comment |
| 283 | // is not needed, but can serve a purpose of uniformity with necessary escaped |
| 284 | // newlines outside the comment. In this case we remove it here before |
| 285 | // trimming the trailing whitespace. The backslash will be re-added later when |
| 286 | // inserting a line break. |
| 287 | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
| 288 | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")) |
| 289 | --EndOfPreviousLine; |
| 290 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 291 | // Calculate the end of the non-whitespace text in the previous line. |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 292 | EndOfPreviousLine = |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 293 | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 294 | if (EndOfPreviousLine == StringRef::npos) |
| 295 | EndOfPreviousLine = 0; |
| 296 | else |
| 297 | ++EndOfPreviousLine; |
| 298 | // Calculate the start of the non-whitespace text in the current line. |
Alexander Kornienko | 8afa39b | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 299 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 300 | if (StartOfLine == StringRef::npos) |
| 301 | StartOfLine = Lines[LineIndex].size(); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 302 | |
| 303 | // Adjust Lines to only contain relevant text. |
| 304 | Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine); |
| 305 | Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
| 306 | // Adjust LeadingWhitespace to account all whitespace between the lines |
| 307 | // to the current line. |
| 308 | LeadingWhitespace[LineIndex] = |
| 309 | Lines[LineIndex].begin() - Lines[LineIndex - 1].end(); |
Manuel Klimek | d63312b | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 310 | |
| 311 | // FIXME: We currently count tabs as 1 character. To solve this, we need to |
| 312 | // get the correct indentation width of the start of the comment, which |
| 313 | // requires correct counting of the tab expansions before the comment, and |
| 314 | // a configurable tab width. Since the current implementation only breaks |
| 315 | // if leading tabs are intermixed with spaces, that is not a high priority. |
| 316 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 317 | // Adjust the start column uniformly accross all lines. |
Manuel Klimek | d63312b | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 318 | StartOfLineColumn[LineIndex] = std::max<int>(0, StartOfLine + IndentDelta); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); } |
| 322 | |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 323 | unsigned BreakableBlockComment::getLineLengthAfterSplit( |
| 324 | unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const { |
| 325 | return getContentStartColumn(LineIndex, Offset) + |
| 326 | encoding::getCodePointCount(Lines[LineIndex].substr(Offset, Length), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 327 | Encoding) + |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 328 | // The last line gets a "*/" postfix. |
| 329 | (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 330 | } |
| 331 | |
| 332 | BreakableToken::Split |
| 333 | BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 334 | unsigned ColumnLimit) const { |
| 335 | return getCommentSplit(Lines[LineIndex].substr(TailOffset), |
| 336 | getContentStartColumn(LineIndex, TailOffset), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 337 | ColumnLimit, Encoding); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 341 | Split Split, |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 342 | WhitespaceManager &Whitespaces) { |
| 343 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 344 | StringRef Prefix = Decoration; |
| 345 | if (LineIndex + 1 == Lines.size() && |
| 346 | Text.size() == Split.first + Split.second) { |
| 347 | // For the last line we need to break before "*/", but not to add "* ". |
| 348 | Prefix = ""; |
| 349 | } |
| 350 | |
| 351 | unsigned BreakOffsetInToken = |
| 352 | Text.data() - Tok.TokenText.data() + Split.first; |
| 353 | unsigned CharsToRemove = Split.second; |
Manuel Klimek | b6dba33 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 354 | assert(IndentAtLineBreak >= Decoration.size()); |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 355 | Whitespaces.replaceWhitespaceInToken(Tok, BreakOffsetInToken, CharsToRemove, |
| 356 | "", Prefix, InPPDirective, 1, |
| 357 | IndentAtLineBreak - Decoration.size()); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void |
| 361 | BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 362 | WhitespaceManager &Whitespaces) { |
| 363 | if (LineIndex == 0) |
| 364 | return; |
| 365 | StringRef Prefix = Decoration; |
Manuel Klimek | c5cc4bf | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 366 | if (Lines[LineIndex].empty()) { |
| 367 | if (LineIndex + 1 == Lines.size()) { |
| 368 | // If the last line is empty, we don't need a prefix, as the */ will line |
| 369 | // up with the decoration (if it exists). |
| 370 | Prefix = ""; |
| 371 | } else if (!Decoration.empty()) { |
| 372 | // For other empty lines, if we do have a decoration, adapt it to not |
| 373 | // contain a trailing whitespace. |
| 374 | Prefix = Prefix.substr(0, 1); |
| 375 | } |
Daniel Jasper | e2c482f | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 376 | } else { |
| 377 | if (StartOfLineColumn[LineIndex] == 1) { |
| 378 | // This lines starts immediately after the decorating *. |
| 379 | Prefix = Prefix.substr(0, 1); |
| 380 | } |
Manuel Klimek | c5cc4bf | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 381 | } |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 382 | |
| 383 | unsigned WhitespaceOffsetInToken = |
| 384 | Lines[LineIndex].data() - Tok.TokenText.data() - |
| 385 | LeadingWhitespace[LineIndex]; |
Manuel Klimek | b6dba33 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 386 | assert(StartOfLineColumn[LineIndex] >= Prefix.size()); |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 387 | Whitespaces.replaceWhitespaceInToken( |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 388 | Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix, |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 389 | InPPDirective, 1, StartOfLineColumn[LineIndex] - Prefix.size()); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | unsigned |
| 393 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 394 | unsigned TailOffset) const { |
| 395 | // If we break, we always break at the predefined indent. |
| 396 | if (TailOffset != 0) |
| 397 | return IndentAtLineBreak; |
| 398 | return StartOfLineColumn[LineIndex]; |
| 399 | } |
| 400 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 401 | } // namespace format |
| 402 | } // namespace clang |