blob: e5cbe8f197e1cb312c488604fd5972af6f838492 [file] [log] [blame]
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001//===--- BreakableToken.h - 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
Krasimir Georgiev91834222017-01-25 13:58:58 +000011/// \brief Declares BreakableToken, BreakableStringLiteral, BreakableComment,
12/// BreakableBlockComment and BreakableLineCommentSection classes, that contain
13/// token type-specific logic to break long lines in tokens and reflow content
14/// between tokens.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000015///
16//===----------------------------------------------------------------------===//
17
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000018#ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
19#define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000020
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000021#include "Encoding.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000022#include "TokenAnnotator.h"
23#include "WhitespaceManager.h"
Krasimir Georgiev00c5c722017-02-02 15:32:19 +000024#include "llvm/Support/Regex.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000025#include <utility>
26
27namespace clang {
28namespace format {
29
Krasimir Georgiev91834222017-01-25 13:58:58 +000030/// \brief Checks if \p Token switches formatting, like /* clang-format off */.
31/// \p Token must be a comment.
32bool switchesFormatting(const FormatToken &Token);
33
Manuel Klimek9043c742013-05-27 15:23:34 +000034struct FormatStyle;
35
36/// \brief Base class for strategies on how to break tokens.
37///
Krasimir Georgiev91834222017-01-25 13:58:58 +000038/// This is organised around the concept of a \c Split, which is a whitespace
39/// range that signifies a position of the content of a token where a
40/// reformatting might be done. Operating with splits is divided into 3
41/// operations:
42/// - getSplit, for finding a split starting at a position,
43/// - getLineLengthAfterSplit, for calculating the size in columns of the rest
44/// of the content after a split has been used for breaking, and
45/// - insertBreak, for executing the split using a whitespace manager.
46///
47/// There is a pair of operations that are used to compress a long whitespace
48/// range with a single space if that will bring the line lenght under the
49/// column limit:
50/// - getLineLengthAfterCompression, for calculating the size in columns of the
51/// line after a whitespace range has been compressed, and
52/// - compressWhitespace, for executing the whitespace compression using a
53/// whitespace manager; note that the compressed whitespace may be in the
54/// middle of the original line and of the reformatted line.
55///
56/// For tokens where the whitespace before each line needs to be also
57/// reformatted, for example for tokens supporting reflow, there are analogous
58/// operations that might be executed before the main line breaking occurs:
59/// - getSplitBefore, for finding a split such that the content preceding it
60/// needs to be specially reflown,
61/// - getLineLengthAfterSplitBefore, for calculating the line length in columns
62/// of the remainder of the content after the beginning of the content has
63/// been reformatted, and
64/// - replaceWhitespaceBefore, for executing the reflow using a whitespace
65/// manager.
66///
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +000067/// For tokens that require the whitespace after the last line to be
68/// reformatted, for example in multiline jsdoc comments that require the
69/// trailing '*/' to be on a line of itself, there are analogous operations
70/// that might be executed after the last line has been reformatted:
71/// - getSplitAfterLastLine, for finding a split after the last line that needs
72/// to be reflown,
73/// - getLineLengthAfterSplitAfterLastLine, for calculating the line length in
74/// columns of the remainder of the token, and
75/// - replaceWhitespaceAfterLastLine, for executing the reflow using a
76/// whitespace manager.
77///
Manuel Klimek9043c742013-05-27 15:23:34 +000078/// FIXME: The interface seems set in stone, so we might want to just pull the
79/// strategy into the class, instead of controlling it from the outside.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000080class BreakableToken {
81public:
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000082 /// \brief Contains starting character index and length of split.
Manuel Klimek9043c742013-05-27 15:23:34 +000083 typedef std::pair<StringRef::size_type, unsigned> Split;
84
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000085 virtual ~BreakableToken() {}
Manuel Klimek9043c742013-05-27 15:23:34 +000086
87 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000088 virtual unsigned getLineCount() const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000089
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000090 /// \brief Returns the number of columns required to format the piece of line
Krasimir Georgiev91834222017-01-25 13:58:58 +000091 /// at \p LineIndex, from byte offset \p TailOffset with length \p Length.
Manuel Klimek9043c742013-05-27 15:23:34 +000092 ///
Krasimir Georgiev91834222017-01-25 13:58:58 +000093 /// Note that previous breaks are not taken into account. \p TailOffset is
94 /// always specified from the start of the (original) line.
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000095 /// \p Length can be set to StringRef::npos, which means "to the end of line".
96 virtual unsigned
Krasimir Georgiev91834222017-01-25 13:58:58 +000097 getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000098 StringRef::size_type Length) const = 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000099
Manuel Klimek9043c742013-05-27 15:23:34 +0000100 /// \brief Returns a range (offset, length) at which to break the line at
101 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
102 /// violate \p ColumnLimit.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000103 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000104 unsigned ColumnLimit,
105 llvm::Regex &CommentPragmasRegex) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000106
107 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000108 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000109 WhitespaceManager &Whitespaces) = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000110
Krasimir Georgiev91834222017-01-25 13:58:58 +0000111 /// \brief Returns the number of columns required to format the piece of line
112 /// at \p LineIndex, from byte offset \p TailOffset after the whitespace range
113 /// \p Split has been compressed into a single space.
114 unsigned getLineLengthAfterCompression(unsigned RemainingTokenColumns,
115 Split Split) const;
116
Alexander Kornienko875395f2013-11-12 17:50:13 +0000117 /// \brief Replaces the whitespace range described by \p Split with a single
118 /// space.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000119 virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
120 Split Split,
121 WhitespaceManager &Whitespaces) = 0;
122
123 /// \brief Returns a whitespace range (offset, length) of the content at
124 /// \p LineIndex such that the content preceding this range needs to be
125 /// reformatted before any breaks are made to this line.
126 ///
127 /// \p PreviousEndColumn is the end column of the previous line after
128 /// formatting.
129 ///
130 /// A result having offset == StringRef::npos means that no piece of the line
131 /// needs to be reformatted before any breaks are made.
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000132 virtual Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000133 unsigned ColumnLimit,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000134 llvm::Regex &CommentPragmasRegex) const {
Krasimir Georgiev91834222017-01-25 13:58:58 +0000135 return Split(StringRef::npos, 0);
136 }
137
138 /// \brief Returns the number of columns required to format the piece of line
139 /// at \p LineIndex after the content preceding the whitespace range specified
140 /// \p SplitBefore has been reformatted, but before any breaks are made to
141 /// this line.
142 virtual unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000143 unsigned TailOffset,
144 unsigned PreviousEndColumn,
145 unsigned ColumnLimit,
146 Split SplitBefore) const {
Krasimir Georgiev91834222017-01-25 13:58:58 +0000147 return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
148 }
Alexander Kornienko875395f2013-11-12 17:50:13 +0000149
Manuel Klimek9043c742013-05-27 15:23:34 +0000150 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000151 /// Performs a reformatting of the content at \p LineIndex preceding the
152 /// whitespace range \p SplitBefore.
Manuel Klimek9043c742013-05-27 15:23:34 +0000153 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000154 unsigned PreviousEndColumn,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000155 unsigned ColumnLimit, Split SplitBefore,
Manuel Klimek9043c742013-05-27 15:23:34 +0000156 WhitespaceManager &Whitespaces) {}
157
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000158 /// \brief Returns a whitespace range (offset, length) of the content at
159 /// the last line that needs to be reformatted after the last line has been
160 /// reformatted.
161 ///
162 /// A result having offset == StringRef::npos means that no reformat is
163 /// necessary.
164 virtual Split getSplitAfterLastLine(unsigned TailOffset, unsigned ColumnLimit,
165 llvm::Regex &CommentPragmasRegex) const {
166 return Split(StringRef::npos, 0);
167 }
168
169 /// \brief Returns the number of columns required to format the piece token
170 /// after the last line after a reformat of the whitespace range \p
171 /// \p SplitAfterLastLine on the last line has been performed.
172 virtual unsigned
173 getLineLengthAfterSplitAfterLastLine(unsigned TailOffset,
174 Split SplitAfterLastLine) const {
175 return getLineLengthAfterSplit(getLineCount() - 1,
176 TailOffset + SplitAfterLastLine.first +
177 SplitAfterLastLine.second,
178 StringRef::npos);
179 }
180
181 /// \brief Replaces the whitespace from \p SplitAfterLastLine on the last line
182 /// after the last line has been formatted by performing a reformatting.
183 virtual void replaceWhitespaceAfterLastLine(unsigned TailOffset,
184 Split SplitAfterLastLine,
185 WhitespaceManager &Whitespaces) {
186 insertBreak(getLineCount() - 1, TailOffset, SplitAfterLastLine,
187 Whitespaces);
188 }
189
Krasimir Georgiev91834222017-01-25 13:58:58 +0000190 /// \brief Updates the next token of \p State to the next token after this
191 /// one. This can be used when this token manages a set of underlying tokens
192 /// as a unit and is responsible for the formatting of the them.
193 virtual void updateNextToken(LineState &State) const {}
194
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000195protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000196 BreakableToken(const FormatToken &Tok, bool InPPDirective,
197 encoding::Encoding Encoding, const FormatStyle &Style)
198 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
199 Style(Style) {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000200
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000201 const FormatToken &Tok;
Alexander Kornienkobe633902013-06-14 11:46:10 +0000202 const bool InPPDirective;
203 const encoding::Encoding Encoding;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000204 const FormatStyle &Style;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000205};
206
Manuel Klimek9043c742013-05-27 15:23:34 +0000207/// \brief Base class for single line tokens that can be broken.
208///
209/// \c getSplit() needs to be implemented by child classes.
210class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000211public:
Craig Topperfb6b25b2014-03-15 04:29:04 +0000212 unsigned getLineCount() const override;
213 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
214 StringRef::size_type Length) const override;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000215
216protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000217 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
218 StringRef Prefix, StringRef Postfix,
219 bool InPPDirective, encoding::Encoding Encoding,
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000220 const FormatStyle &Style);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000221
Manuel Klimek9043c742013-05-27 15:23:34 +0000222 // The column in which the token starts.
223 unsigned StartColumn;
224 // The prefix a line needs after a break in the token.
225 StringRef Prefix;
226 // The postfix a line needs before introducing a break.
227 StringRef Postfix;
228 // The token text excluding the prefix and postfix.
229 StringRef Line;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000230};
231
Manuel Klimek9043c742013-05-27 15:23:34 +0000232class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000233public:
Manuel Klimek9043c742013-05-27 15:23:34 +0000234 /// \brief Creates a breakable token for a single line string literal.
235 ///
236 /// \p StartColumn specifies the column in which the token will start
237 /// after formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000238 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
239 StringRef Prefix, StringRef Postfix,
240 bool InPPDirective, encoding::Encoding Encoding,
241 const FormatStyle &Style);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000242
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000243 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
244 llvm::Regex &CommentPragmasRegex) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000245 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
246 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000247 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
248 WhitespaceManager &Whitespaces) override {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000249};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000250
Krasimir Georgiev91834222017-01-25 13:58:58 +0000251class BreakableComment : public BreakableToken {
252protected:
253 /// \brief Creates a breakable token for a comment.
Manuel Klimek9043c742013-05-27 15:23:34 +0000254 ///
Krasimir Georgiev4b159222017-02-21 10:54:50 +0000255 /// \p StartColumn specifies the column in which the comment will start after
256 /// formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000257 BreakableComment(const FormatToken &Token, unsigned StartColumn,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000258 bool InPPDirective, encoding::Encoding Encoding,
259 const FormatStyle &Style);
Krasimir Georgiev91834222017-01-25 13:58:58 +0000260
261public:
262 unsigned getLineCount() const override;
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000263 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
264 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000265 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
266 WhitespaceManager &Whitespaces) override;
267
268protected:
269 virtual unsigned getContentStartColumn(unsigned LineIndex,
270 unsigned TailOffset) const = 0;
271
272 // Returns a split that divides Text into a left and right parts, such that
273 // the left part is suitable for reflowing after PreviousEndColumn.
274 Split getReflowSplit(StringRef Text, StringRef ReflowPrefix,
275 unsigned PreviousEndColumn, unsigned ColumnLimit) const;
276
277 // Returns the token containing the line at LineIndex.
278 const FormatToken &tokenAt(unsigned LineIndex) const;
279
280 // Checks if the content of line LineIndex may be reflown with the previous
281 // line.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000282 virtual bool mayReflow(unsigned LineIndex,
283 llvm::Regex &CommentPragmasRegex) const = 0;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000284
285 // Contains the original text of the lines of the block comment.
286 //
287 // In case of a block comments, excludes the leading /* in the first line and
288 // trailing */ in the last line. In case of line comments, excludes the
289 // leading // and spaces.
290 SmallVector<StringRef, 16> Lines;
291
292 // Contains the text of the lines excluding all leading and trailing
293 // whitespace between the lines. Note that the decoration (if present) is also
294 // not considered part of the text.
295 SmallVector<StringRef, 16> Content;
296
297 // Tokens[i] contains a reference to the token containing Lines[i] if the
298 // whitespace range before that token is managed by this block.
299 // Otherwise, Tokens[i] is a null pointer.
300 SmallVector<FormatToken *, 16> Tokens;
301
302 // ContentColumn[i] is the target column at which Content[i] should be.
303 // Note that this excludes a leading "* " or "*" in case of block comments
304 // where all lines have a "*" prefix, or the leading "// " or "//" in case of
305 // line comments.
306 //
307 // In block comments, the first line's target column is always positive. The
308 // remaining lines' target columns are relative to the first line to allow
309 // correct indentation of comments in \c WhitespaceManager. Thus they can be
310 // negative as well (in case the first line needs to be unindented more than
311 // there's actual whitespace in another line).
312 SmallVector<int, 16> ContentColumn;
313
314 // The intended start column of the first line of text from this section.
315 unsigned StartColumn;
316
Krasimir Georgiev91834222017-01-25 13:58:58 +0000317 // The prefix to use in front a line that has been reflown up.
318 // For example, when reflowing the second line after the first here:
319 // // comment 1
320 // // comment 2
321 // we expect:
322 // // comment 1 comment 2
323 // and not:
324 // // comment 1comment 2
325 StringRef ReflowPrefix = " ";
326};
327
328class BreakableBlockComment : public BreakableComment {
329public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000330 BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
331 unsigned OriginalStartColumn, bool FirstInLine,
332 bool InPPDirective, encoding::Encoding Encoding,
333 const FormatStyle &Style);
Manuel Klimek9043c742013-05-27 15:23:34 +0000334
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000335 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000336 StringRef::size_type Length) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000337 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
338 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000339 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000340 unsigned ColumnLimit,
341 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000342 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
343 unsigned TailOffset,
344 unsigned PreviousEndColumn,
345 unsigned ColumnLimit,
346 Split SplitBefore) const override;
347 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000348 unsigned ColumnLimit, Split SplitBefore,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000349 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000350 Split getSplitAfterLastLine(unsigned TailOffset, unsigned ColumnLimit,
351 llvm::Regex &CommentPragmasRegex) const override;
352
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000353 bool mayReflow(unsigned LineIndex,
354 llvm::Regex &CommentPragmasRegex) const override;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000355
356private:
Krasimir Georgiev91834222017-01-25 13:58:58 +0000357 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
Manuel Klimek9043c742013-05-27 15:23:34 +0000358 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000359 // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
360 // leading and trailing whitespace.
361 //
362 // Sets ContentColumn to the intended column in which the text at
Manuel Klimek9043c742013-05-27 15:23:34 +0000363 // Lines[LineIndex] starts (note that the decoration, if present, is not
364 // considered part of the text).
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000365 void adjustWhitespace(unsigned LineIndex, int IndentDelta);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000366
Krasimir Georgiev91834222017-01-25 13:58:58 +0000367 // Computes the end column if the full Content from LineIndex gets reflown
368 // after PreviousEndColumn.
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000369 unsigned getReflownColumn(StringRef Content, unsigned LineIndex,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000370 unsigned PreviousEndColumn) const;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000371
Krasimir Georgiev91834222017-01-25 13:58:58 +0000372 unsigned getContentStartColumn(unsigned LineIndex,
373 unsigned TailOffset) const override;
Manuel Klimek9043c742013-05-27 15:23:34 +0000374
375 // The column at which the text of a broken line should start.
376 // Note that an optional decoration would go before that column.
377 // IndentAtLineBreak is a uniform position for all lines in a block comment,
378 // regardless of their relative position.
379 // FIXME: Revisit the decision to do this; the main reason was to support
380 // patterns like
381 // /**************//**
382 // * Comment
383 // We could also support such patterns by special casing the first line
384 // instead.
385 unsigned IndentAtLineBreak;
386
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000387 // This is to distinguish between the case when the last line was empty and
388 // the case when it started with a decoration ("*" or "* ").
389 bool LastLineNeedsDecoration;
390
Manuel Klimek9043c742013-05-27 15:23:34 +0000391 // Either "* " if all lines begin with a "*", or empty.
392 StringRef Decoration;
Krasimir Georgievbb99a362017-02-16 12:39:31 +0000393
394 // If this block comment has decorations, this is the column of the start of
395 // the decorations.
396 unsigned DecorationColumn;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000397
398 // If true, make sure that the opening '/**' and the closing '*/' ends on a
399 // line of itself. Styles like jsdoc require this for multiline comments.
400 bool DelimitersOnNewline;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000401};
402
Krasimir Georgiev91834222017-01-25 13:58:58 +0000403class BreakableLineCommentSection : public BreakableComment {
404public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000405 BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000406 unsigned OriginalStartColumn, bool FirstInLine,
407 bool InPPDirective, encoding::Encoding Encoding,
408 const FormatStyle &Style);
409
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000410 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000411 StringRef::size_type Length) const override;
412 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
413 WhitespaceManager &Whitespaces) override;
414 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000415 unsigned ColumnLimit,
416 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000417 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
418 unsigned TailOffset,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000419 unsigned PreviousEndColumn,
420 unsigned ColumnLimit,
421 Split SplitBefore) const override;
422 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
423 unsigned ColumnLimit, Split SplitBefore,
424 WhitespaceManager &Whitespaces) override;
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000425 void updateNextToken(LineState &State) const override;
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000426 bool mayReflow(unsigned LineIndex,
427 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000428
429private:
430 unsigned getContentStartColumn(unsigned LineIndex,
431 unsigned TailOffset) const override;
432
Krasimir Georgiev2091a3a2017-02-08 14:45:19 +0000433 // OriginalPrefix[i] contains the original prefix of line i, including
434 // trailing whitespace before the start of the content. The indentation
435 // preceding the prefix is not included.
436 // For example, if the line is:
437 // // content
438 // then the original prefix is "// ".
439 SmallVector<StringRef, 16> OriginalPrefix;
440
Krasimir Georgiev91834222017-01-25 13:58:58 +0000441 // Prefix[i] contains the intended leading "//" with trailing spaces to
442 // account for the indentation of content within the comment at line i after
443 // formatting. It can be different than the original prefix when the original
444 // line starts like this:
445 // //content
446 // Then the original prefix is "//", but the prefix is "// ".
447 SmallVector<StringRef, 16> Prefix;
448
449 SmallVector<unsigned, 16> OriginalContentColumn;
450
451 /// \brief The token to which the last line of this breakable token belongs
452 /// to; nullptr if that token is the initial token.
453 ///
454 /// The distinction is because if the token of the last line of this breakable
455 /// token is distinct from the initial token, this breakable token owns the
456 /// whitespace before the token of the last line, and the whitespace manager
457 /// must be able to modify it.
458 FormatToken *LastLineTok = nullptr;
459};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000460} // namespace format
461} // namespace clang
462
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000463#endif