blob: 8c2dc741d1e0bb7516d3c6cee20b02d06b39c765 [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,
Krasimir Georgiev35599fd2017-10-16 09:08:53 +000061/// - introducesBreakBefore, for checking if reformatting the beginning
62/// of the content introduces a line break before it,
Krasimir Georgiev91834222017-01-25 13:58:58 +000063/// - getLineLengthAfterSplitBefore, for calculating the line length in columns
64/// of the remainder of the content after the beginning of the content has
65/// been reformatted, and
66/// - replaceWhitespaceBefore, for executing the reflow using a whitespace
67/// manager.
68///
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +000069/// For tokens that require the whitespace after the last line to be
70/// reformatted, for example in multiline jsdoc comments that require the
71/// trailing '*/' to be on a line of itself, there are analogous operations
72/// that might be executed after the last line has been reformatted:
73/// - getSplitAfterLastLine, for finding a split after the last line that needs
74/// to be reflown,
75/// - getLineLengthAfterSplitAfterLastLine, for calculating the line length in
76/// columns of the remainder of the token, and
77/// - replaceWhitespaceAfterLastLine, for executing the reflow using a
78/// whitespace manager.
79///
Manuel Klimek9043c742013-05-27 15:23:34 +000080/// FIXME: The interface seems set in stone, so we might want to just pull the
81/// strategy into the class, instead of controlling it from the outside.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000082class BreakableToken {
83public:
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000084 /// \brief Contains starting character index and length of split.
Manuel Klimek9043c742013-05-27 15:23:34 +000085 typedef std::pair<StringRef::size_type, unsigned> Split;
86
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000087 virtual ~BreakableToken() {}
Manuel Klimek9043c742013-05-27 15:23:34 +000088
89 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000090 virtual unsigned getLineCount() const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000091
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000092 /// \brief Returns the number of columns required to format the piece of line
Krasimir Georgiev91834222017-01-25 13:58:58 +000093 /// at \p LineIndex, from byte offset \p TailOffset with length \p Length.
Manuel Klimek9043c742013-05-27 15:23:34 +000094 ///
Krasimir Georgiev91834222017-01-25 13:58:58 +000095 /// Note that previous breaks are not taken into account. \p TailOffset is
96 /// always specified from the start of the (original) line.
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000097 /// \p Length can be set to StringRef::npos, which means "to the end of line".
98 virtual unsigned
Krasimir Georgiev91834222017-01-25 13:58:58 +000099 getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000100 StringRef::size_type Length) const = 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000101
Manuel Klimek9043c742013-05-27 15:23:34 +0000102 /// \brief Returns a range (offset, length) at which to break the line at
103 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
104 /// violate \p ColumnLimit.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000105 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000106 unsigned ColumnLimit,
107 llvm::Regex &CommentPragmasRegex) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000108
109 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000110 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000111 WhitespaceManager &Whitespaces) = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000112
Krasimir Georgiev91834222017-01-25 13:58:58 +0000113 /// \brief Returns the number of columns required to format the piece of line
114 /// at \p LineIndex, from byte offset \p TailOffset after the whitespace range
115 /// \p Split has been compressed into a single space.
116 unsigned getLineLengthAfterCompression(unsigned RemainingTokenColumns,
117 Split Split) const;
118
Alexander Kornienko875395f2013-11-12 17:50:13 +0000119 /// \brief Replaces the whitespace range described by \p Split with a single
120 /// space.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000121 virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
122 Split Split,
123 WhitespaceManager &Whitespaces) = 0;
124
125 /// \brief Returns a whitespace range (offset, length) of the content at
126 /// \p LineIndex such that the content preceding this range needs to be
127 /// reformatted before any breaks are made to this line.
128 ///
129 /// \p PreviousEndColumn is the end column of the previous line after
130 /// formatting.
131 ///
132 /// A result having offset == StringRef::npos means that no piece of the line
133 /// needs to be reformatted before any breaks are made.
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000134 virtual Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000135 unsigned ColumnLimit,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000136 llvm::Regex &CommentPragmasRegex) const {
Krasimir Georgiev91834222017-01-25 13:58:58 +0000137 return Split(StringRef::npos, 0);
138 }
139
Krasimir Georgiev35599fd2017-10-16 09:08:53 +0000140 /// \brief Returns if a break before the content at \p LineIndex will be
141 /// inserted after the whitespace preceding the content has been reformatted.
142 virtual bool introducesBreakBefore(unsigned LineIndex) const {
143 return false;
144 }
145
Krasimir Georgiev91834222017-01-25 13:58:58 +0000146 /// \brief Returns the number of columns required to format the piece of line
147 /// at \p LineIndex after the content preceding the whitespace range specified
148 /// \p SplitBefore has been reformatted, but before any breaks are made to
149 /// this line.
150 virtual unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000151 unsigned TailOffset,
152 unsigned PreviousEndColumn,
153 unsigned ColumnLimit,
154 Split SplitBefore) const {
Krasimir Georgiev91834222017-01-25 13:58:58 +0000155 return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
156 }
Alexander Kornienko875395f2013-11-12 17:50:13 +0000157
Manuel Klimek9043c742013-05-27 15:23:34 +0000158 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000159 /// Performs a reformatting of the content at \p LineIndex preceding the
160 /// whitespace range \p SplitBefore.
Manuel Klimek9043c742013-05-27 15:23:34 +0000161 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000162 unsigned PreviousEndColumn,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000163 unsigned ColumnLimit, Split SplitBefore,
Manuel Klimek9043c742013-05-27 15:23:34 +0000164 WhitespaceManager &Whitespaces) {}
165
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000166 /// \brief Returns a whitespace range (offset, length) of the content at
167 /// the last line that needs to be reformatted after the last line has been
168 /// reformatted.
169 ///
170 /// A result having offset == StringRef::npos means that no reformat is
171 /// necessary.
Krasimir Georgiev3b865342017-08-09 09:42:32 +0000172 virtual Split getSplitAfterLastLine(unsigned TailOffset,
173 unsigned ColumnLimit) const {
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000174 return Split(StringRef::npos, 0);
175 }
176
177 /// \brief Returns the number of columns required to format the piece token
178 /// after the last line after a reformat of the whitespace range \p
179 /// \p SplitAfterLastLine on the last line has been performed.
180 virtual unsigned
181 getLineLengthAfterSplitAfterLastLine(unsigned TailOffset,
182 Split SplitAfterLastLine) const {
183 return getLineLengthAfterSplit(getLineCount() - 1,
184 TailOffset + SplitAfterLastLine.first +
185 SplitAfterLastLine.second,
186 StringRef::npos);
187 }
188
189 /// \brief Replaces the whitespace from \p SplitAfterLastLine on the last line
190 /// after the last line has been formatted by performing a reformatting.
191 virtual void replaceWhitespaceAfterLastLine(unsigned TailOffset,
192 Split SplitAfterLastLine,
193 WhitespaceManager &Whitespaces) {
194 insertBreak(getLineCount() - 1, TailOffset, SplitAfterLastLine,
195 Whitespaces);
196 }
197
Krasimir Georgiev91834222017-01-25 13:58:58 +0000198 /// \brief Updates the next token of \p State to the next token after this
199 /// one. This can be used when this token manages a set of underlying tokens
200 /// as a unit and is responsible for the formatting of the them.
201 virtual void updateNextToken(LineState &State) const {}
202
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000203protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000204 BreakableToken(const FormatToken &Tok, bool InPPDirective,
205 encoding::Encoding Encoding, const FormatStyle &Style)
206 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
207 Style(Style) {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000208
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000209 const FormatToken &Tok;
Alexander Kornienkobe633902013-06-14 11:46:10 +0000210 const bool InPPDirective;
211 const encoding::Encoding Encoding;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000212 const FormatStyle &Style;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000213};
214
Manuel Klimek9043c742013-05-27 15:23:34 +0000215/// \brief Base class for single line tokens that can be broken.
216///
217/// \c getSplit() needs to be implemented by child classes.
218class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000219public:
Craig Topperfb6b25b2014-03-15 04:29:04 +0000220 unsigned getLineCount() const override;
221 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
222 StringRef::size_type Length) const override;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000223
224protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000225 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
226 StringRef Prefix, StringRef Postfix,
227 bool InPPDirective, encoding::Encoding Encoding,
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000228 const FormatStyle &Style);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000229
Manuel Klimek9043c742013-05-27 15:23:34 +0000230 // The column in which the token starts.
231 unsigned StartColumn;
232 // The prefix a line needs after a break in the token.
233 StringRef Prefix;
234 // The postfix a line needs before introducing a break.
235 StringRef Postfix;
236 // The token text excluding the prefix and postfix.
237 StringRef Line;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000238};
239
Manuel Klimek9043c742013-05-27 15:23:34 +0000240class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000241public:
Manuel Klimek9043c742013-05-27 15:23:34 +0000242 /// \brief Creates a breakable token for a single line string literal.
243 ///
244 /// \p StartColumn specifies the column in which the token will start
245 /// after formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000246 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
247 StringRef Prefix, StringRef Postfix,
248 bool InPPDirective, encoding::Encoding Encoding,
249 const FormatStyle &Style);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000250
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000251 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
252 llvm::Regex &CommentPragmasRegex) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000253 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
254 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000255 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
256 WhitespaceManager &Whitespaces) override {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000257};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000258
Krasimir Georgiev91834222017-01-25 13:58:58 +0000259class BreakableComment : public BreakableToken {
260protected:
261 /// \brief Creates a breakable token for a comment.
Manuel Klimek9043c742013-05-27 15:23:34 +0000262 ///
Krasimir Georgiev4b159222017-02-21 10:54:50 +0000263 /// \p StartColumn specifies the column in which the comment will start after
264 /// formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000265 BreakableComment(const FormatToken &Token, unsigned StartColumn,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000266 bool InPPDirective, encoding::Encoding Encoding,
267 const FormatStyle &Style);
Krasimir Georgiev91834222017-01-25 13:58:58 +0000268
269public:
270 unsigned getLineCount() const override;
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000271 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
272 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000273 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
274 WhitespaceManager &Whitespaces) override;
275
276protected:
277 virtual unsigned getContentStartColumn(unsigned LineIndex,
278 unsigned TailOffset) const = 0;
279
280 // Returns a split that divides Text into a left and right parts, such that
281 // the left part is suitable for reflowing after PreviousEndColumn.
282 Split getReflowSplit(StringRef Text, StringRef ReflowPrefix,
283 unsigned PreviousEndColumn, unsigned ColumnLimit) const;
284
285 // Returns the token containing the line at LineIndex.
286 const FormatToken &tokenAt(unsigned LineIndex) const;
287
288 // Checks if the content of line LineIndex may be reflown with the previous
289 // line.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000290 virtual bool mayReflow(unsigned LineIndex,
291 llvm::Regex &CommentPragmasRegex) const = 0;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000292
293 // Contains the original text of the lines of the block comment.
294 //
295 // In case of a block comments, excludes the leading /* in the first line and
296 // trailing */ in the last line. In case of line comments, excludes the
297 // leading // and spaces.
298 SmallVector<StringRef, 16> Lines;
299
300 // Contains the text of the lines excluding all leading and trailing
301 // whitespace between the lines. Note that the decoration (if present) is also
302 // not considered part of the text.
303 SmallVector<StringRef, 16> Content;
304
305 // Tokens[i] contains a reference to the token containing Lines[i] if the
306 // whitespace range before that token is managed by this block.
307 // Otherwise, Tokens[i] is a null pointer.
308 SmallVector<FormatToken *, 16> Tokens;
309
310 // ContentColumn[i] is the target column at which Content[i] should be.
311 // Note that this excludes a leading "* " or "*" in case of block comments
312 // where all lines have a "*" prefix, or the leading "// " or "//" in case of
313 // line comments.
314 //
315 // In block comments, the first line's target column is always positive. The
316 // remaining lines' target columns are relative to the first line to allow
317 // correct indentation of comments in \c WhitespaceManager. Thus they can be
318 // negative as well (in case the first line needs to be unindented more than
319 // there's actual whitespace in another line).
320 SmallVector<int, 16> ContentColumn;
321
322 // The intended start column of the first line of text from this section.
323 unsigned StartColumn;
324
Krasimir Georgiev91834222017-01-25 13:58:58 +0000325 // The prefix to use in front a line that has been reflown up.
326 // For example, when reflowing the second line after the first here:
327 // // comment 1
328 // // comment 2
329 // we expect:
330 // // comment 1 comment 2
331 // and not:
332 // // comment 1comment 2
333 StringRef ReflowPrefix = " ";
334};
335
336class BreakableBlockComment : public BreakableComment {
337public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000338 BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
339 unsigned OriginalStartColumn, bool FirstInLine,
340 bool InPPDirective, encoding::Encoding Encoding,
341 const FormatStyle &Style);
Manuel Klimek9043c742013-05-27 15:23:34 +0000342
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000343 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000344 StringRef::size_type Length) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000345 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
346 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000347 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000348 unsigned ColumnLimit,
349 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev35599fd2017-10-16 09:08:53 +0000350 bool introducesBreakBefore(unsigned LineIndex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000351 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
352 unsigned TailOffset,
353 unsigned PreviousEndColumn,
354 unsigned ColumnLimit,
355 Split SplitBefore) const override;
356 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000357 unsigned ColumnLimit, Split SplitBefore,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000358 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev3b865342017-08-09 09:42:32 +0000359 Split getSplitAfterLastLine(unsigned TailOffset,
360 unsigned ColumnLimit) const override;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000361
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000362 bool mayReflow(unsigned LineIndex,
363 llvm::Regex &CommentPragmasRegex) const override;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000364
365private:
Krasimir Georgiev91834222017-01-25 13:58:58 +0000366 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
Manuel Klimek9043c742013-05-27 15:23:34 +0000367 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000368 // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
369 // leading and trailing whitespace.
370 //
371 // Sets ContentColumn to the intended column in which the text at
Manuel Klimek9043c742013-05-27 15:23:34 +0000372 // Lines[LineIndex] starts (note that the decoration, if present, is not
373 // considered part of the text).
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000374 void adjustWhitespace(unsigned LineIndex, int IndentDelta);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000375
Krasimir Georgiev91834222017-01-25 13:58:58 +0000376 // Computes the end column if the full Content from LineIndex gets reflown
377 // after PreviousEndColumn.
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000378 unsigned getReflownColumn(StringRef Content, unsigned LineIndex,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000379 unsigned PreviousEndColumn) const;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000380
Krasimir Georgiev91834222017-01-25 13:58:58 +0000381 unsigned getContentStartColumn(unsigned LineIndex,
382 unsigned TailOffset) const override;
Manuel Klimek9043c742013-05-27 15:23:34 +0000383
384 // The column at which the text of a broken line should start.
385 // Note that an optional decoration would go before that column.
386 // IndentAtLineBreak is a uniform position for all lines in a block comment,
387 // regardless of their relative position.
388 // FIXME: Revisit the decision to do this; the main reason was to support
389 // patterns like
390 // /**************//**
391 // * Comment
392 // We could also support such patterns by special casing the first line
393 // instead.
394 unsigned IndentAtLineBreak;
395
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000396 // This is to distinguish between the case when the last line was empty and
397 // the case when it started with a decoration ("*" or "* ").
398 bool LastLineNeedsDecoration;
399
Manuel Klimek9043c742013-05-27 15:23:34 +0000400 // Either "* " if all lines begin with a "*", or empty.
401 StringRef Decoration;
Krasimir Georgievbb99a362017-02-16 12:39:31 +0000402
403 // If this block comment has decorations, this is the column of the start of
404 // the decorations.
405 unsigned DecorationColumn;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000406
407 // If true, make sure that the opening '/**' and the closing '*/' ends on a
408 // line of itself. Styles like jsdoc require this for multiline comments.
409 bool DelimitersOnNewline;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000410};
411
Krasimir Georgiev91834222017-01-25 13:58:58 +0000412class BreakableLineCommentSection : public BreakableComment {
413public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000414 BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000415 unsigned OriginalStartColumn, bool FirstInLine,
416 bool InPPDirective, encoding::Encoding Encoding,
417 const FormatStyle &Style);
418
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000419 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000420 StringRef::size_type Length) const override;
421 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
422 WhitespaceManager &Whitespaces) override;
423 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000424 unsigned ColumnLimit,
425 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000426 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
427 unsigned TailOffset,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000428 unsigned PreviousEndColumn,
429 unsigned ColumnLimit,
430 Split SplitBefore) const override;
431 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
432 unsigned ColumnLimit, Split SplitBefore,
433 WhitespaceManager &Whitespaces) override;
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000434 void updateNextToken(LineState &State) const override;
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000435 bool mayReflow(unsigned LineIndex,
436 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000437
438private:
439 unsigned getContentStartColumn(unsigned LineIndex,
440 unsigned TailOffset) const override;
441
Krasimir Georgiev2091a3a2017-02-08 14:45:19 +0000442 // OriginalPrefix[i] contains the original prefix of line i, including
443 // trailing whitespace before the start of the content. The indentation
444 // preceding the prefix is not included.
445 // For example, if the line is:
446 // // content
447 // then the original prefix is "// ".
448 SmallVector<StringRef, 16> OriginalPrefix;
449
Krasimir Georgiev91834222017-01-25 13:58:58 +0000450 // Prefix[i] contains the intended leading "//" with trailing spaces to
451 // account for the indentation of content within the comment at line i after
452 // formatting. It can be different than the original prefix when the original
453 // line starts like this:
454 // //content
455 // Then the original prefix is "//", but the prefix is "// ".
456 SmallVector<StringRef, 16> Prefix;
457
458 SmallVector<unsigned, 16> OriginalContentColumn;
459
460 /// \brief The token to which the last line of this breakable token belongs
461 /// to; nullptr if that token is the initial token.
462 ///
463 /// The distinction is because if the token of the last line of this breakable
464 /// token is distinct from the initial token, this breakable token owns the
465 /// whitespace before the token of the last line, and the whitespace manager
466 /// must be able to modify it.
467 FormatToken *LastLineTok = nullptr;
468};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000469} // namespace format
470} // namespace clang
471
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000472#endif