blob: c2365f1402f7d8d6a8f50fbe90c4ce8815b35a47 [file] [log] [blame]
Alexander Kornienko70ce7882013-04-15 14:28:00 +00001//===--- 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 Klimekde008c02013-05-27 15:23:34 +000016#define DEBUG_TYPE "format-token-breaker"
17
Alexander Kornienko70ce7882013-04-15 14:28:00 +000018#include "BreakableToken.h"
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000019#include "clang/Basic/CharInfo.h"
Manuel Klimekde008c02013-05-27 15:23:34 +000020#include "clang/Format/Format.h"
Alexander Kornienko919398b2013-04-17 17:34:05 +000021#include "llvm/ADT/STLExtras.h"
Manuel Klimekde008c02013-05-27 15:23:34 +000022#include "llvm/Support/Debug.h"
Alexander Kornienko712b7472013-08-07 23:29:01 +000023#include "llvm/Support/Locale.h"
Alexander Kornienko70ce7882013-04-15 14:28:00 +000024#include <algorithm>
25
26namespace clang {
27namespace format {
28
Craig Toppercf1e2162013-06-30 22:44:02 +000029static const char *const Blanks = " \t\v\f";
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000030static bool IsBlank(char C) {
31 switch (C) {
Daniel Jasper2a409b62013-07-08 14:34:09 +000032 case ' ':
33 case '\t':
34 case '\v':
35 case '\f':
36 return true;
37 default:
38 return false;
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000039 }
40}
41
Alexander Kornienko712b7472013-08-07 23:29:01 +000042static unsigned columnWidth(StringRef Text, encoding::Encoding Encoding) {
43 if (Encoding == encoding::Encoding_UTF8) {
44 int ContentWidth = llvm::sys::locale::columnWidth(Text);
45 if (ContentWidth >= 0)
46 return ContentWidth;
47 }
48 return encoding::getCodePointCount(Text, Encoding);
49}
50
Craig Topperb8f71642013-07-01 03:38:29 +000051static BreakableToken::Split getCommentSplit(StringRef Text,
52 unsigned ContentStartColumn,
53 unsigned ColumnLimit,
54 encoding::Encoding Encoding) {
Alexander Kornienko919398b2013-04-17 17:34:05 +000055 if (ColumnLimit <= ContentStartColumn + 1)
Manuel Klimekde008c02013-05-27 15:23:34 +000056 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko919398b2013-04-17 17:34:05 +000057
58 unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
Alexander Kornienko00895102013-06-05 14:09:10 +000059 unsigned MaxSplitBytes = 0;
60
61 for (unsigned NumChars = 0;
Alexander Kornienko712b7472013-08-07 23:29:01 +000062 NumChars < MaxSplit && MaxSplitBytes < Text.size();) {
63 unsigned NumBytes =
Alexander Kornienko00895102013-06-05 14:09:10 +000064 encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
Alexander Kornienko712b7472013-08-07 23:29:01 +000065 NumChars += columnWidth(Text.substr(MaxSplitBytes, NumBytes), Encoding);
66 MaxSplitBytes += NumBytes;
67 }
Alexander Kornienko00895102013-06-05 14:09:10 +000068
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000069 StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
Alexander Kornienko919398b2013-04-17 17:34:05 +000070 if (SpaceOffset == StringRef::npos ||
Manuel Klimekde008c02013-05-27 15:23:34 +000071 // Don't break at leading whitespace.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000072 Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
Manuel Klimekbe9ed772013-05-29 22:06:18 +000073 // Make sure that we don't break at leading whitespace that
74 // reaches past MaxSplit.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000075 StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
Manuel Klimekbe9ed772013-05-29 22:06:18 +000076 if (FirstNonWhitespace == StringRef::npos)
77 // If the comment is only whitespace, we cannot split.
78 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000079 SpaceOffset = Text.find_first_of(
80 Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
Manuel Klimekbe9ed772013-05-29 22:06:18 +000081 }
Alexander Kornienko919398b2013-04-17 17:34:05 +000082 if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000083 StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
84 StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
Alexander Kornienko919398b2013-04-17 17:34:05 +000085 return BreakableToken::Split(BeforeCut.size(),
86 AfterCut.begin() - BeforeCut.end());
87 }
88 return BreakableToken::Split(StringRef::npos, 0);
89}
90
Craig Topperb8f71642013-07-01 03:38:29 +000091static BreakableToken::Split getStringSplit(StringRef Text,
92 unsigned ContentStartColumn,
93 unsigned ColumnLimit,
94 encoding::Encoding Encoding) {
Manuel Klimekde008c02013-05-27 15:23:34 +000095 // FIXME: Reduce unit test case.
96 if (Text.empty())
97 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko00895102013-06-05 14:09:10 +000098 if (ColumnLimit <= ContentStartColumn)
Manuel Klimekde008c02013-05-27 15:23:34 +000099 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko712b7472013-08-07 23:29:01 +0000100 unsigned MaxSplit = std::min<unsigned>(ColumnLimit - ContentStartColumn,
101 columnWidth(Text, Encoding) - 1);
Alexander Kornienko00895102013-06-05 14:09:10 +0000102 StringRef::size_type SpaceOffset = 0;
103 StringRef::size_type SlashOffset = 0;
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +0000104 StringRef::size_type WordStartOffset = 0;
Alexander Kornienko00895102013-06-05 14:09:10 +0000105 StringRef::size_type SplitPoint = 0;
106 for (unsigned Chars = 0;;) {
107 unsigned Advance;
108 if (Text[0] == '\\') {
109 Advance = encoding::getEscapeSequenceLength(Text);
110 Chars += Advance;
111 } else {
112 Advance = encoding::getCodePointNumBytes(Text[0], Encoding);
Alexander Kornienko712b7472013-08-07 23:29:01 +0000113 Chars += columnWidth(Text.substr(0, Advance), Encoding);
Alexander Kornienko00895102013-06-05 14:09:10 +0000114 }
115
116 if (Chars > MaxSplit)
117 break;
118
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000119 if (IsBlank(Text[0]))
Alexander Kornienko00895102013-06-05 14:09:10 +0000120 SpaceOffset = SplitPoint;
121 if (Text[0] == '/')
122 SlashOffset = SplitPoint;
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000123 if (Advance == 1 && !isAlphanumeric(Text[0]))
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +0000124 WordStartOffset = SplitPoint;
Alexander Kornienko00895102013-06-05 14:09:10 +0000125
126 SplitPoint += Advance;
127 Text = Text.substr(Advance);
128 }
129
130 if (SpaceOffset != 0)
131 return BreakableToken::Split(SpaceOffset + 1, 0);
132 if (SlashOffset != 0)
133 return BreakableToken::Split(SlashOffset + 1, 0);
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +0000134 if (WordStartOffset != 0)
135 return BreakableToken::Split(WordStartOffset + 1, 0);
Alexander Kornienko00895102013-06-05 14:09:10 +0000136 if (SplitPoint != 0)
137 return BreakableToken::Split(SplitPoint, 0);
138 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000139}
140
Manuel Klimekde008c02013-05-27 15:23:34 +0000141unsigned BreakableSingleLineToken::getLineCount() const { return 1; }
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000142
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000143unsigned BreakableSingleLineToken::getLineLengthAfterSplit(
144 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
Alexander Kornienko00895102013-06-05 14:09:10 +0000145 return StartColumn + Prefix.size() + Postfix.size() +
Alexander Kornienko712b7472013-08-07 23:29:01 +0000146 columnWidth(Line.substr(Offset, Length), Encoding);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000147}
148
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000149BreakableSingleLineToken::BreakableSingleLineToken(
150 const FormatToken &Tok, unsigned StartColumn, StringRef Prefix,
151 StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding)
152 : BreakableToken(Tok, InPPDirective, Encoding), StartColumn(StartColumn),
153 Prefix(Prefix), Postfix(Postfix) {
Manuel Klimekde008c02013-05-27 15:23:34 +0000154 assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix));
155 Line = Tok.TokenText.substr(
156 Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size());
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000157}
158
Manuel Klimekde008c02013-05-27 15:23:34 +0000159BreakableStringLiteral::BreakableStringLiteral(const FormatToken &Tok,
Alexander Kornienko00895102013-06-05 14:09:10 +0000160 unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000161 bool InPPDirective,
Alexander Kornienko00895102013-06-05 14:09:10 +0000162 encoding::Encoding Encoding)
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000163 : BreakableSingleLineToken(Tok, StartColumn, "\"", "\"", InPPDirective,
164 Encoding) {}
Manuel Klimekde008c02013-05-27 15:23:34 +0000165
166BreakableToken::Split
167BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset,
168 unsigned ColumnLimit) const {
Alexander Kornienko00895102013-06-05 14:09:10 +0000169 return getStringSplit(Line.substr(TailOffset), StartColumn + 2, ColumnLimit,
170 Encoding);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000171}
172
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000173void BreakableStringLiteral::insertBreak(unsigned LineIndex,
174 unsigned TailOffset, Split Split,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000175 WhitespaceManager &Whitespaces) {
176 Whitespaces.replaceWhitespaceInToken(
177 Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
178 Prefix, InPPDirective, 1, StartColumn);
179}
180
Manuel Klimekde008c02013-05-27 15:23:34 +0000181static StringRef getLineCommentPrefix(StringRef Comment) {
Craig Topper3aa29df2013-07-15 08:24:27 +0000182 static const char *const KnownPrefixes[] = { "/// ", "///", "// ", "//" };
Manuel Klimekde008c02013-05-27 15:23:34 +0000183 for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i)
Alexander Kornienko919398b2013-04-17 17:34:05 +0000184 if (Comment.startswith(KnownPrefixes[i]))
185 return KnownPrefixes[i];
186 return "";
187}
188
Manuel Klimekde008c02013-05-27 15:23:34 +0000189BreakableLineComment::BreakableLineComment(const FormatToken &Token,
Alexander Kornienko00895102013-06-05 14:09:10 +0000190 unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000191 bool InPPDirective,
Alexander Kornienko00895102013-06-05 14:09:10 +0000192 encoding::Encoding Encoding)
Manuel Klimekde008c02013-05-27 15:23:34 +0000193 : BreakableSingleLineToken(Token, StartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +0000194 getLineCommentPrefix(Token.TokenText), "",
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000195 InPPDirective, Encoding) {
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000196 OriginalPrefix = Prefix;
197 if (Token.TokenText.size() > Prefix.size() &&
198 isAlphanumeric(Token.TokenText[Prefix.size()])) {
199 if (Prefix == "//")
200 Prefix = "// ";
201 else if (Prefix == "///")
202 Prefix = "/// ";
203 }
204}
Manuel Klimekde008c02013-05-27 15:23:34 +0000205
206BreakableToken::Split
207BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
208 unsigned ColumnLimit) const {
209 return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
Alexander Kornienko00895102013-06-05 14:09:10 +0000210 ColumnLimit, Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000211}
212
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000213void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000214 Split Split,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000215 WhitespaceManager &Whitespaces) {
216 Whitespaces.replaceWhitespaceInToken(
217 Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
218 Postfix, Prefix, InPPDirective, 1, StartColumn);
219}
220
221void
222BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000223 WhitespaceManager &Whitespaces) {
224 if (OriginalPrefix != Prefix) {
225 Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
226 false, 0, 1);
227 }
228}
229
Alexander Kornienko00895102013-06-05 14:09:10 +0000230BreakableBlockComment::BreakableBlockComment(
231 const FormatStyle &Style, const FormatToken &Token, unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000232 unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
233 encoding::Encoding Encoding)
234 : BreakableToken(Token, InPPDirective, Encoding) {
Manuel Klimekde008c02013-05-27 15:23:34 +0000235 StringRef TokenText(Token.TokenText);
236 assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
237 TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
238
239 int IndentDelta = StartColumn - OriginalStartColumn;
Manuel Klimekde008c02013-05-27 15:23:34 +0000240 LeadingWhitespace.resize(Lines.size());
241 StartOfLineColumn.resize(Lines.size());
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000242 StartOfLineColumn[0] = StartColumn + 2;
243 for (size_t i = 1; i < Lines.size(); ++i)
244 adjustWhitespace(Style, i, IndentDelta);
245
246 Decoration = "* ";
Manuel Klimekde008c02013-05-27 15:23:34 +0000247 if (Lines.size() == 1 && !FirstInLine) {
248 // Comments for which FirstInLine is false can start on arbitrary column,
249 // and available horizontal space can be too small to align consecutive
250 // lines with the first one.
251 // FIXME: We could, probably, align them to current indentation level, but
252 // now we just wrap them without stars.
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000253 Decoration = "";
Manuel Klimekde008c02013-05-27 15:23:34 +0000254 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000255 for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) {
256 // If the last line is empty, the closing "*/" will have a star.
257 if (i + 1 == e && Lines[i].empty())
258 break;
259 while (!Lines[i].startswith(Decoration))
260 Decoration = Decoration.substr(0, Decoration.size() - 1);
Manuel Klimekde008c02013-05-27 15:23:34 +0000261 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000262
263 LastLineNeedsDecoration = true;
Manuel Klimekde008c02013-05-27 15:23:34 +0000264 IndentAtLineBreak = StartOfLineColumn[0] + 1;
265 for (size_t i = 1; i < Lines.size(); ++i) {
266 if (Lines[i].empty()) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000267 if (i + 1 == Lines.size()) {
268 // Empty last line means that we already have a star as a part of the
269 // trailing */. We also need to preserve whitespace, so that */ is
270 // correctly indented.
271 LastLineNeedsDecoration = false;
272 } else if (Decoration.empty()) {
273 // For all other lines, set the start column to 0 if they're empty, so
274 // we do not insert trailing whitespace anywhere.
Manuel Klimekde008c02013-05-27 15:23:34 +0000275 StartOfLineColumn[i] = 0;
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000276 }
Manuel Klimekde008c02013-05-27 15:23:34 +0000277 continue;
278 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000279 // The first line already excludes the star.
280 // For all other lines, adjust the line to exclude the star and
281 // (optionally) the first whitespace.
282 StartOfLineColumn[i] += Decoration.size();
283 Lines[i] = Lines[i].substr(Decoration.size());
284 LeadingWhitespace[i] += Decoration.size();
Manuel Klimekde008c02013-05-27 15:23:34 +0000285 IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]);
286 }
Daniel Jaspercb4b40b2013-05-30 17:27:48 +0000287 IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000288 DEBUG({
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000289 llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n";
Manuel Klimekde008c02013-05-27 15:23:34 +0000290 for (size_t i = 0; i < Lines.size(); ++i) {
291 llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i]
292 << "\n";
293 }
294 });
295}
296
297void BreakableBlockComment::adjustWhitespace(const FormatStyle &Style,
298 unsigned LineIndex,
299 int IndentDelta) {
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000300 // When in a preprocessor directive, the trailing backslash in a block comment
301 // is not needed, but can serve a purpose of uniformity with necessary escaped
302 // newlines outside the comment. In this case we remove it here before
303 // trimming the trailing whitespace. The backslash will be re-added later when
304 // inserting a line break.
305 size_t EndOfPreviousLine = Lines[LineIndex - 1].size();
306 if (InPPDirective && Lines[LineIndex - 1].endswith("\\"))
307 --EndOfPreviousLine;
308
Manuel Klimekde008c02013-05-27 15:23:34 +0000309 // Calculate the end of the non-whitespace text in the previous line.
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000310 EndOfPreviousLine =
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000311 Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
Manuel Klimekde008c02013-05-27 15:23:34 +0000312 if (EndOfPreviousLine == StringRef::npos)
313 EndOfPreviousLine = 0;
314 else
315 ++EndOfPreviousLine;
316 // Calculate the start of the non-whitespace text in the current line.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000317 size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
Manuel Klimekde008c02013-05-27 15:23:34 +0000318 if (StartOfLine == StringRef::npos)
319 StartOfLine = Lines[LineIndex].size();
Manuel Klimekde008c02013-05-27 15:23:34 +0000320
321 // Adjust Lines to only contain relevant text.
322 Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine);
323 Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine);
324 // Adjust LeadingWhitespace to account all whitespace between the lines
325 // to the current line.
326 LeadingWhitespace[LineIndex] =
327 Lines[LineIndex].begin() - Lines[LineIndex - 1].end();
Manuel Klimekd63312b2013-05-28 10:01:59 +0000328
329 // FIXME: We currently count tabs as 1 character. To solve this, we need to
330 // get the correct indentation width of the start of the comment, which
331 // requires correct counting of the tab expansions before the comment, and
332 // a configurable tab width. Since the current implementation only breaks
333 // if leading tabs are intermixed with spaces, that is not a high priority.
334
Manuel Klimekde008c02013-05-27 15:23:34 +0000335 // Adjust the start column uniformly accross all lines.
Manuel Klimekd63312b2013-05-28 10:01:59 +0000336 StartOfLineColumn[LineIndex] = std::max<int>(0, StartOfLine + IndentDelta);
Manuel Klimekde008c02013-05-27 15:23:34 +0000337}
338
339unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); }
340
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000341unsigned BreakableBlockComment::getLineLengthAfterSplit(
342 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
343 return getContentStartColumn(LineIndex, Offset) +
Alexander Kornienko712b7472013-08-07 23:29:01 +0000344 columnWidth(Lines[LineIndex].substr(Offset, Length), Encoding) +
Manuel Klimekde008c02013-05-27 15:23:34 +0000345 // The last line gets a "*/" postfix.
346 (LineIndex + 1 == Lines.size() ? 2 : 0);
347}
348
349BreakableToken::Split
350BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset,
351 unsigned ColumnLimit) const {
352 return getCommentSplit(Lines[LineIndex].substr(TailOffset),
353 getContentStartColumn(LineIndex, TailOffset),
Alexander Kornienko00895102013-06-05 14:09:10 +0000354 ColumnLimit, Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000355}
356
357void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000358 Split Split,
Manuel Klimekde008c02013-05-27 15:23:34 +0000359 WhitespaceManager &Whitespaces) {
360 StringRef Text = Lines[LineIndex].substr(TailOffset);
361 StringRef Prefix = Decoration;
362 if (LineIndex + 1 == Lines.size() &&
363 Text.size() == Split.first + Split.second) {
364 // For the last line we need to break before "*/", but not to add "* ".
365 Prefix = "";
366 }
367
368 unsigned BreakOffsetInToken =
369 Text.data() - Tok.TokenText.data() + Split.first;
370 unsigned CharsToRemove = Split.second;
Manuel Klimekb6dba332013-05-30 07:45:53 +0000371 assert(IndentAtLineBreak >= Decoration.size());
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000372 Whitespaces.replaceWhitespaceInToken(Tok, BreakOffsetInToken, CharsToRemove,
373 "", Prefix, InPPDirective, 1,
374 IndentAtLineBreak - Decoration.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000375}
376
377void
378BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +0000379 WhitespaceManager &Whitespaces) {
380 if (LineIndex == 0)
381 return;
382 StringRef Prefix = Decoration;
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000383 if (Lines[LineIndex].empty()) {
384 if (LineIndex + 1 == Lines.size()) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000385 if (!LastLineNeedsDecoration) {
386 // If the last line was empty, we don't need a prefix, as the */ will
387 // line up with the decoration (if it exists).
388 Prefix = "";
389 }
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000390 } else if (!Decoration.empty()) {
391 // For other empty lines, if we do have a decoration, adapt it to not
392 // contain a trailing whitespace.
393 Prefix = Prefix.substr(0, 1);
394 }
Daniel Jaspere2c482f2013-05-30 06:40:07 +0000395 } else {
396 if (StartOfLineColumn[LineIndex] == 1) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000397 // This line starts immediately after the decorating *.
Daniel Jaspere2c482f2013-05-30 06:40:07 +0000398 Prefix = Prefix.substr(0, 1);
399 }
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000400 }
Manuel Klimekde008c02013-05-27 15:23:34 +0000401
Daniel Jasper2a409b62013-07-08 14:34:09 +0000402 unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() -
403 Tok.TokenText.data() -
404 LeadingWhitespace[LineIndex];
Manuel Klimekb6dba332013-05-30 07:45:53 +0000405 assert(StartOfLineColumn[LineIndex] >= Prefix.size());
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000406 Whitespaces.replaceWhitespaceInToken(
Manuel Klimekde008c02013-05-27 15:23:34 +0000407 Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000408 InPPDirective, 1, StartOfLineColumn[LineIndex] - Prefix.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000409}
410
411unsigned
412BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
413 unsigned TailOffset) const {
414 // If we break, we always break at the predefined indent.
415 if (TailOffset != 0)
416 return IndentAtLineBreak;
417 return StartOfLineColumn[LineIndex];
418}
419
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000420} // namespace format
421} // namespace clang