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