blob: 6363f895f95b94fa1c3f48632b68422f8aff9293 [file] [log] [blame]
Alexander Kornienkocb45bc12013-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
16#include "BreakableToken.h"
Eric Liu99eeab72016-10-19 08:19:46 +000017#include "Comments.h"
Alexander Kornienko555efc32013-06-11 16:01:49 +000018#include "clang/Basic/CharInfo.h"
Manuel Klimek9043c742013-05-27 15:23:34 +000019#include "clang/Format/Format.h"
Alexander Kornienko9e90b622013-04-17 17:34:05 +000020#include "llvm/ADT/STLExtras.h"
Manuel Klimek9043c742013-05-27 15:23:34 +000021#include "llvm/Support/Debug.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000022#include <algorithm>
23
Chandler Carruth10346662014-04-22 03:17:02 +000024#define DEBUG_TYPE "format-token-breaker"
25
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000026namespace clang {
27namespace format {
28
Daniel Jasper580da272013-10-30 07:36:40 +000029static const char *const Blanks = " \t\v\f\r";
Alexander Kornienkob93062e2013-06-20 13:58:37 +000030static bool IsBlank(char C) {
31 switch (C) {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000032 case ' ':
33 case '\t':
34 case '\v':
35 case '\f':
Daniel Jasper580da272013-10-30 07:36:40 +000036 case '\r':
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000037 return true;
38 default:
39 return false;
Alexander Kornienkob93062e2013-06-20 13:58:37 +000040 }
41}
42
Craig Topperbfb5c402013-07-01 03:38:29 +000043static BreakableToken::Split getCommentSplit(StringRef Text,
44 unsigned ContentStartColumn,
45 unsigned ColumnLimit,
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +000046 unsigned TabWidth,
Craig Topperbfb5c402013-07-01 03:38:29 +000047 encoding::Encoding Encoding) {
Alexander Kornienko9e90b622013-04-17 17:34:05 +000048 if (ColumnLimit <= ContentStartColumn + 1)
Manuel Klimek9043c742013-05-27 15:23:34 +000049 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko9e90b622013-04-17 17:34:05 +000050
51 unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000052 unsigned MaxSplitBytes = 0;
53
54 for (unsigned NumChars = 0;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +000055 NumChars < MaxSplit && MaxSplitBytes < Text.size();) {
56 unsigned BytesInChar =
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000057 encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +000058 NumChars +=
59 encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar),
60 ContentStartColumn, TabWidth, Encoding);
61 MaxSplitBytes += BytesInChar;
62 }
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000063
Alexander Kornienkob93062e2013-06-20 13:58:37 +000064 StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
Alexander Kornienko9e90b622013-04-17 17:34:05 +000065 if (SpaceOffset == StringRef::npos ||
Manuel Klimek9043c742013-05-27 15:23:34 +000066 // Don't break at leading whitespace.
Alexander Kornienkob93062e2013-06-20 13:58:37 +000067 Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
Manuel Klimekae1fbfb2013-05-29 22:06:18 +000068 // Make sure that we don't break at leading whitespace that
69 // reaches past MaxSplit.
Alexander Kornienkob93062e2013-06-20 13:58:37 +000070 StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
Manuel Klimekae1fbfb2013-05-29 22:06:18 +000071 if (FirstNonWhitespace == StringRef::npos)
72 // If the comment is only whitespace, we cannot split.
73 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienkob93062e2013-06-20 13:58:37 +000074 SpaceOffset = Text.find_first_of(
75 Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
Manuel Klimekae1fbfb2013-05-29 22:06:18 +000076 }
Alexander Kornienko9e90b622013-04-17 17:34:05 +000077 if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
Alexander Kornienkob93062e2013-06-20 13:58:37 +000078 StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
79 StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
Alexander Kornienko9e90b622013-04-17 17:34:05 +000080 return BreakableToken::Split(BeforeCut.size(),
81 AfterCut.begin() - BeforeCut.end());
82 }
83 return BreakableToken::Split(StringRef::npos, 0);
84}
85
Daniel Jasperb05a81d2014-05-09 13:11:16 +000086static BreakableToken::Split
87getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit,
88 unsigned TabWidth, encoding::Encoding Encoding) {
Manuel Klimek9043c742013-05-27 15:23:34 +000089 // FIXME: Reduce unit test case.
90 if (Text.empty())
91 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko81e32942013-09-16 20:20:49 +000092 if (ColumnLimit <= UsedColumns)
Manuel Klimek9043c742013-05-27 15:23:34 +000093 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko71d95d62013-11-26 10:38:53 +000094 unsigned MaxSplit = ColumnLimit - UsedColumns;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000095 StringRef::size_type SpaceOffset = 0;
96 StringRef::size_type SlashOffset = 0;
Alexander Kornienko72852072013-06-19 14:22:47 +000097 StringRef::size_type WordStartOffset = 0;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000098 StringRef::size_type SplitPoint = 0;
99 for (unsigned Chars = 0;;) {
100 unsigned Advance;
101 if (Text[0] == '\\') {
102 Advance = encoding::getEscapeSequenceLength(Text);
103 Chars += Advance;
104 } else {
105 Advance = encoding::getCodePointNumBytes(Text[0], Encoding);
Alexander Kornienko81e32942013-09-16 20:20:49 +0000106 Chars += encoding::columnWidthWithTabs(
107 Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000108 }
109
Daniel Jaspere4b48c62015-01-21 19:50:35 +0000110 if (Chars > MaxSplit || Text.size() <= Advance)
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000111 break;
112
Alexander Kornienkob93062e2013-06-20 13:58:37 +0000113 if (IsBlank(Text[0]))
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000114 SpaceOffset = SplitPoint;
115 if (Text[0] == '/')
116 SlashOffset = SplitPoint;
Alexander Kornienkob93062e2013-06-20 13:58:37 +0000117 if (Advance == 1 && !isAlphanumeric(Text[0]))
Alexander Kornienko72852072013-06-19 14:22:47 +0000118 WordStartOffset = SplitPoint;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000119
120 SplitPoint += Advance;
121 Text = Text.substr(Advance);
122 }
123
124 if (SpaceOffset != 0)
125 return BreakableToken::Split(SpaceOffset + 1, 0);
126 if (SlashOffset != 0)
127 return BreakableToken::Split(SlashOffset + 1, 0);
Alexander Kornienko72852072013-06-19 14:22:47 +0000128 if (WordStartOffset != 0)
129 return BreakableToken::Split(WordStartOffset + 1, 0);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000130 if (SplitPoint != 0)
131 return BreakableToken::Split(SplitPoint, 0);
132 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000133}
134
Manuel Klimek9043c742013-05-27 15:23:34 +0000135unsigned BreakableSingleLineToken::getLineCount() const { return 1; }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000136
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000137unsigned BreakableSingleLineToken::getLineLengthAfterSplit(
138 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000139 return StartColumn + Prefix.size() + Postfix.size() +
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000140 encoding::columnWidthWithTabs(Line.substr(Offset, Length),
141 StartColumn + Prefix.size(),
142 Style.TabWidth, Encoding);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000143}
144
Alexander Kornienkobe633902013-06-14 11:46:10 +0000145BreakableSingleLineToken::BreakableSingleLineToken(
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000146 const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
147 StringRef Prefix, StringRef Postfix, bool InPPDirective,
148 encoding::Encoding Encoding, const FormatStyle &Style)
149 : BreakableToken(Tok, IndentLevel, InPPDirective, Encoding, Style),
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000150 StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) {
Daniel Jasper174b0122014-01-09 14:18:12 +0000151 assert(Tok.TokenText.endswith(Postfix));
Manuel Klimek9043c742013-05-27 15:23:34 +0000152 Line = Tok.TokenText.substr(
153 Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size());
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000154}
155
Alexander Kornienko81e32942013-09-16 20:20:49 +0000156BreakableStringLiteral::BreakableStringLiteral(
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000157 const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
158 StringRef Prefix, StringRef Postfix, bool InPPDirective,
159 encoding::Encoding Encoding, const FormatStyle &Style)
160 : BreakableSingleLineToken(Tok, IndentLevel, StartColumn, Prefix, Postfix,
161 InPPDirective, Encoding, Style) {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000162
163BreakableToken::Split
164BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset,
165 unsigned ColumnLimit) const {
Alexander Kornienko81e32942013-09-16 20:20:49 +0000166 return getStringSplit(Line.substr(TailOffset),
167 StartColumn + Prefix.size() + Postfix.size(),
168 ColumnLimit, Style.TabWidth, Encoding);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000169}
170
Alexander Kornienko555efc32013-06-11 16:01:49 +0000171void BreakableStringLiteral::insertBreak(unsigned LineIndex,
172 unsigned TailOffset, Split Split,
Alexander Kornienko555efc32013-06-11 16:01:49 +0000173 WhitespaceManager &Whitespaces) {
Daniel Jasperd07c2ee2014-01-14 09:53:07 +0000174 unsigned LeadingSpaces = StartColumn;
175 // The '@' of an ObjC string literal (@"Test") does not become part of the
176 // string token.
177 // FIXME: It might be a cleaner solution to merge the tokens as a
178 // precomputation step.
179 if (Prefix.startswith("@"))
180 --LeadingSpaces;
Alexander Kornienko555efc32013-06-11 16:01:49 +0000181 Whitespaces.replaceWhitespaceInToken(
182 Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
Daniel Jasperd07c2ee2014-01-14 09:53:07 +0000183 Prefix, InPPDirective, 1, IndentLevel, LeadingSpaces);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000184}
185
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000186BreakableLineComment::BreakableLineComment(
187 const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn,
188 bool InPPDirective, encoding::Encoding Encoding, const FormatStyle &Style)
189 : BreakableSingleLineToken(Token, IndentLevel, StartColumn,
Alexander Kornienko4504f932014-03-10 13:14:56 +0000190 getLineCommentIndentPrefix(Token.TokenText), "",
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000191 InPPDirective, Encoding, Style) {
Alexander Kornienko555efc32013-06-11 16:01:49 +0000192 OriginalPrefix = Prefix;
193 if (Token.TokenText.size() > Prefix.size() &&
194 isAlphanumeric(Token.TokenText[Prefix.size()])) {
195 if (Prefix == "//")
196 Prefix = "// ";
197 else if (Prefix == "///")
198 Prefix = "/// ";
Daniel Jasperdee894f2015-06-09 13:16:54 +0000199 else if (Prefix == "//!")
200 Prefix = "//! ";
Alexander Kornienko555efc32013-06-11 16:01:49 +0000201 }
202}
Manuel Klimek9043c742013-05-27 15:23:34 +0000203
204BreakableToken::Split
205BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
206 unsigned ColumnLimit) const {
207 return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000208 ColumnLimit, Style.TabWidth, Encoding);
Manuel Klimek9043c742013-05-27 15:23:34 +0000209}
210
Alexander Kornienko555efc32013-06-11 16:01:49 +0000211void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienkobe633902013-06-14 11:46:10 +0000212 Split Split,
Alexander Kornienko555efc32013-06-11 16:01:49 +0000213 WhitespaceManager &Whitespaces) {
214 Whitespaces.replaceWhitespaceInToken(
215 Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
Alexander Kornienko875395f2013-11-12 17:50:13 +0000216 Postfix, Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, StartColumn);
217}
218
219void BreakableLineComment::replaceWhitespace(unsigned LineIndex,
220 unsigned TailOffset, Split Split,
221 WhitespaceManager &Whitespaces) {
222 Whitespaces.replaceWhitespaceInToken(
223 Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, "",
224 "", /*InPPDirective=*/false, /*Newlines=*/0, /*IndentLevel=*/0,
225 /*Spaces=*/1);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000226}
227
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000228void BreakableLineComment::replaceWhitespaceBefore(
229 unsigned LineIndex, WhitespaceManager &Whitespaces) {
Alexander Kornienko555efc32013-06-11 16:01:49 +0000230 if (OriginalPrefix != Prefix) {
231 Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
Alexander Kornienko875395f2013-11-12 17:50:13 +0000232 /*InPPDirective=*/false,
233 /*Newlines=*/0, /*IndentLevel=*/0,
234 /*Spaces=*/1);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000235 }
236}
237
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000238BreakableBlockComment::BreakableBlockComment(
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000239 const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn,
Alexander Kornienkobe633902013-06-14 11:46:10 +0000240 unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000241 encoding::Encoding Encoding, const FormatStyle &Style)
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000242 : BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style) {
Manuel Klimek9043c742013-05-27 15:23:34 +0000243 StringRef TokenText(Token.TokenText);
244 assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
245 TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
246
247 int IndentDelta = StartColumn - OriginalStartColumn;
Manuel Klimek9043c742013-05-27 15:23:34 +0000248 LeadingWhitespace.resize(Lines.size());
249 StartOfLineColumn.resize(Lines.size());
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000250 StartOfLineColumn[0] = StartColumn + 2;
251 for (size_t i = 1; i < Lines.size(); ++i)
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000252 adjustWhitespace(i, IndentDelta);
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000253
254 Decoration = "* ";
Manuel Klimek9043c742013-05-27 15:23:34 +0000255 if (Lines.size() == 1 && !FirstInLine) {
256 // Comments for which FirstInLine is false can start on arbitrary column,
257 // and available horizontal space can be too small to align consecutive
258 // lines with the first one.
259 // FIXME: We could, probably, align them to current indentation level, but
260 // now we just wrap them without stars.
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000261 Decoration = "";
Manuel Klimek9043c742013-05-27 15:23:34 +0000262 }
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000263 for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) {
264 // If the last line is empty, the closing "*/" will have a star.
265 if (i + 1 == e && Lines[i].empty())
266 break;
Daniel Jasper6d9b88d2015-05-06 07:17:22 +0000267 if (!Lines[i].empty() && i + 1 != e && Decoration.startswith(Lines[i]))
268 continue;
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000269 while (!Lines[i].startswith(Decoration))
270 Decoration = Decoration.substr(0, Decoration.size() - 1);
Manuel Klimek9043c742013-05-27 15:23:34 +0000271 }
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000272
273 LastLineNeedsDecoration = true;
Manuel Klimek9043c742013-05-27 15:23:34 +0000274 IndentAtLineBreak = StartOfLineColumn[0] + 1;
275 for (size_t i = 1; i < Lines.size(); ++i) {
276 if (Lines[i].empty()) {
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000277 if (i + 1 == Lines.size()) {
278 // Empty last line means that we already have a star as a part of the
279 // trailing */. We also need to preserve whitespace, so that */ is
280 // correctly indented.
281 LastLineNeedsDecoration = false;
282 } else if (Decoration.empty()) {
283 // For all other lines, set the start column to 0 if they're empty, so
284 // we do not insert trailing whitespace anywhere.
Manuel Klimek9043c742013-05-27 15:23:34 +0000285 StartOfLineColumn[i] = 0;
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000286 }
Manuel Klimek9043c742013-05-27 15:23:34 +0000287 continue;
288 }
Daniel Jasper6d9b88d2015-05-06 07:17:22 +0000289
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000290 // The first line already excludes the star.
291 // For all other lines, adjust the line to exclude the star and
292 // (optionally) the first whitespace.
Daniel Jasper6d9b88d2015-05-06 07:17:22 +0000293 unsigned DecorationSize =
294 Decoration.startswith(Lines[i]) ? Lines[i].size() : Decoration.size();
295 StartOfLineColumn[i] += DecorationSize;
296 Lines[i] = Lines[i].substr(DecorationSize);
297 LeadingWhitespace[i] += DecorationSize;
298 if (!Decoration.startswith(Lines[i]))
299 IndentAtLineBreak =
300 std::min<int>(IndentAtLineBreak, std::max(0, StartOfLineColumn[i]));
Manuel Klimek9043c742013-05-27 15:23:34 +0000301 }
Daniel Jasperce257f22013-05-30 17:27:48 +0000302 IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
Manuel Klimek9043c742013-05-27 15:23:34 +0000303 DEBUG({
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000304 llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n";
Manuel Klimek9043c742013-05-27 15:23:34 +0000305 for (size_t i = 0; i < Lines.size(); ++i) {
306 llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i]
307 << "\n";
308 }
309 });
310}
311
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000312void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
Manuel Klimek9043c742013-05-27 15:23:34 +0000313 int IndentDelta) {
Alexander Kornienkobe633902013-06-14 11:46:10 +0000314 // When in a preprocessor directive, the trailing backslash in a block comment
315 // is not needed, but can serve a purpose of uniformity with necessary escaped
316 // newlines outside the comment. In this case we remove it here before
317 // trimming the trailing whitespace. The backslash will be re-added later when
318 // inserting a line break.
319 size_t EndOfPreviousLine = Lines[LineIndex - 1].size();
320 if (InPPDirective && Lines[LineIndex - 1].endswith("\\"))
321 --EndOfPreviousLine;
322
Manuel Klimek9043c742013-05-27 15:23:34 +0000323 // Calculate the end of the non-whitespace text in the previous line.
Alexander Kornienkobe633902013-06-14 11:46:10 +0000324 EndOfPreviousLine =
Alexander Kornienkob93062e2013-06-20 13:58:37 +0000325 Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
Manuel Klimek9043c742013-05-27 15:23:34 +0000326 if (EndOfPreviousLine == StringRef::npos)
327 EndOfPreviousLine = 0;
328 else
329 ++EndOfPreviousLine;
330 // Calculate the start of the non-whitespace text in the current line.
Alexander Kornienkob93062e2013-06-20 13:58:37 +0000331 size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
Manuel Klimek9043c742013-05-27 15:23:34 +0000332 if (StartOfLine == StringRef::npos)
Daniel Jasperd6e61882015-06-17 12:23:15 +0000333 StartOfLine = Lines[LineIndex].rtrim("\r\n").size();
Manuel Klimek9043c742013-05-27 15:23:34 +0000334
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000335 StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine);
Manuel Klimek9043c742013-05-27 15:23:34 +0000336 // Adjust Lines to only contain relevant text.
337 Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine);
338 Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine);
339 // Adjust LeadingWhitespace to account all whitespace between the lines
340 // to the current line.
341 LeadingWhitespace[LineIndex] =
342 Lines[LineIndex].begin() - Lines[LineIndex - 1].end();
Manuel Klimek34d15152013-05-28 10:01:59 +0000343
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000344 // Adjust the start column uniformly across all lines.
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000345 StartOfLineColumn[LineIndex] =
Alexander Kornienko39856b72013-09-10 09:38:25 +0000346 encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) +
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000347 IndentDelta;
Manuel Klimek9043c742013-05-27 15:23:34 +0000348}
349
350unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); }
351
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000352unsigned BreakableBlockComment::getLineLengthAfterSplit(
353 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000354 unsigned ContentStartColumn = getContentStartColumn(LineIndex, Offset);
355 return ContentStartColumn +
356 encoding::columnWidthWithTabs(Lines[LineIndex].substr(Offset, Length),
357 ContentStartColumn, Style.TabWidth,
358 Encoding) +
Manuel Klimek9043c742013-05-27 15:23:34 +0000359 // The last line gets a "*/" postfix.
360 (LineIndex + 1 == Lines.size() ? 2 : 0);
361}
362
363BreakableToken::Split
364BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset,
365 unsigned ColumnLimit) const {
366 return getCommentSplit(Lines[LineIndex].substr(TailOffset),
367 getContentStartColumn(LineIndex, TailOffset),
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000368 ColumnLimit, Style.TabWidth, Encoding);
Manuel Klimek9043c742013-05-27 15:23:34 +0000369}
370
371void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienkobe633902013-06-14 11:46:10 +0000372 Split Split,
Manuel Klimek9043c742013-05-27 15:23:34 +0000373 WhitespaceManager &Whitespaces) {
374 StringRef Text = Lines[LineIndex].substr(TailOffset);
375 StringRef Prefix = Decoration;
376 if (LineIndex + 1 == Lines.size() &&
377 Text.size() == Split.first + Split.second) {
378 // For the last line we need to break before "*/", but not to add "* ".
379 Prefix = "";
380 }
381
382 unsigned BreakOffsetInToken =
383 Text.data() - Tok.TokenText.data() + Split.first;
384 unsigned CharsToRemove = Split.second;
Manuel Klimek8910d192013-05-30 07:45:53 +0000385 assert(IndentAtLineBreak >= Decoration.size());
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000386 Whitespaces.replaceWhitespaceInToken(
387 Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, InPPDirective, 1,
388 IndentLevel, IndentAtLineBreak - Decoration.size());
Manuel Klimek9043c742013-05-27 15:23:34 +0000389}
390
Alexander Kornienko875395f2013-11-12 17:50:13 +0000391void BreakableBlockComment::replaceWhitespace(unsigned LineIndex,
392 unsigned TailOffset, Split Split,
393 WhitespaceManager &Whitespaces) {
394 StringRef Text = Lines[LineIndex].substr(TailOffset);
395 unsigned BreakOffsetInToken =
396 Text.data() - Tok.TokenText.data() + Split.first;
397 unsigned CharsToRemove = Split.second;
398 Whitespaces.replaceWhitespaceInToken(
399 Tok, BreakOffsetInToken, CharsToRemove, "", "", /*InPPDirective=*/false,
400 /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1);
401}
402
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000403void BreakableBlockComment::replaceWhitespaceBefore(
404 unsigned LineIndex, WhitespaceManager &Whitespaces) {
Manuel Klimek9043c742013-05-27 15:23:34 +0000405 if (LineIndex == 0)
406 return;
407 StringRef Prefix = Decoration;
Manuel Klimek281dcbe2013-05-28 08:55:01 +0000408 if (Lines[LineIndex].empty()) {
409 if (LineIndex + 1 == Lines.size()) {
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000410 if (!LastLineNeedsDecoration) {
411 // If the last line was empty, we don't need a prefix, as the */ will
412 // line up with the decoration (if it exists).
413 Prefix = "";
414 }
Manuel Klimek281dcbe2013-05-28 08:55:01 +0000415 } else if (!Decoration.empty()) {
416 // For other empty lines, if we do have a decoration, adapt it to not
417 // contain a trailing whitespace.
418 Prefix = Prefix.substr(0, 1);
419 }
Daniel Jasper51fb2b22013-05-30 06:40:07 +0000420 } else {
421 if (StartOfLineColumn[LineIndex] == 1) {
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000422 // This line starts immediately after the decorating *.
Daniel Jasper51fb2b22013-05-30 06:40:07 +0000423 Prefix = Prefix.substr(0, 1);
424 }
Manuel Klimek281dcbe2013-05-28 08:55:01 +0000425 }
Manuel Klimek9043c742013-05-27 15:23:34 +0000426
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000427 unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() -
428 Tok.TokenText.data() -
429 LeadingWhitespace[LineIndex];
Alexander Kornienko555efc32013-06-11 16:01:49 +0000430 Whitespaces.replaceWhitespaceInToken(
Manuel Klimek9043c742013-05-27 15:23:34 +0000431 Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000432 InPPDirective, 1, IndentLevel,
433 StartOfLineColumn[LineIndex] - Prefix.size());
Manuel Klimek9043c742013-05-27 15:23:34 +0000434}
435
436unsigned
437BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
438 unsigned TailOffset) const {
439 // If we break, we always break at the predefined indent.
440 if (TailOffset != 0)
441 return IndentAtLineBreak;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000442 return std::max(0, StartOfLineColumn[LineIndex]);
Manuel Klimek9043c742013-05-27 15:23:34 +0000443}
444
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000445} // namespace format
446} // namespace clang