blob: a2a818f91148ef87451226082304c3156cc6503a [file] [log] [blame]
Erik Pilkington7adcf292018-07-24 00:07:49 +00001//===--- BreakableToken.h - Format C++ code ---------------------*- C++ -*-===//
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00002//
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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// Declares BreakableToken, BreakableStringLiteral, BreakableComment,
Krasimir Georgiev91834222017-01-25 13:58:58 +000012/// 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 Georgiev6a5c95b2018-07-30 08:45:45 +000024#include "llvm/ADT/StringSet.h"
Krasimir Georgiev00c5c722017-02-02 15:32:19 +000025#include "llvm/Support/Regex.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000026#include <utility>
27
28namespace clang {
29namespace format {
30
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000031/// Checks if \p Token switches formatting, like /* clang-format off */.
Krasimir Georgiev91834222017-01-25 13:58:58 +000032/// \p Token must be a comment.
33bool switchesFormatting(const FormatToken &Token);
34
Manuel Klimek9043c742013-05-27 15:23:34 +000035struct FormatStyle;
36
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000037/// Base class for tokens / ranges of tokens that can allow breaking
Manuel Klimek93699f42017-11-29 14:29:43 +000038/// within the tokens - for example, to avoid whitespace beyond the column
39/// limit, or to reflow text.
Manuel Klimek9043c742013-05-27 15:23:34 +000040///
Manuel Klimek93699f42017-11-29 14:29:43 +000041/// Generally, a breakable token consists of logical lines, addressed by a line
42/// index. For example, in a sequence of line comments, each line comment is its
43/// own logical line; similarly, for a block comment, each line in the block
44/// comment is on its own logical line.
45///
46/// There are two methods to compute the layout of the token:
47/// - getRangeLength measures the number of columns needed for a range of text
48/// within a logical line, and
49/// - getContentStartColumn returns the start column at which we want the
50/// content of a logical line to start (potentially after introducing a line
51/// break).
52///
53/// The mechanism to adapt the layout of the breakable token is organised
54/// around the concept of a \c Split, which is a whitespace range that signifies
55/// a position of the content of a token where a reformatting might be done.
56///
57/// Operating with splits is divided into two operations:
Krasimir Georgiev91834222017-01-25 13:58:58 +000058/// - getSplit, for finding a split starting at a position,
Krasimir Georgiev91834222017-01-25 13:58:58 +000059/// - insertBreak, for executing the split using a whitespace manager.
60///
61/// There is a pair of operations that are used to compress a long whitespace
Manuel Klimek93699f42017-11-29 14:29:43 +000062/// range with a single space if that will bring the line length under the
Krasimir Georgiev91834222017-01-25 13:58:58 +000063/// column limit:
64/// - getLineLengthAfterCompression, for calculating the size in columns of the
65/// line after a whitespace range has been compressed, and
66/// - compressWhitespace, for executing the whitespace compression using a
67/// whitespace manager; note that the compressed whitespace may be in the
68/// middle of the original line and of the reformatted line.
69///
70/// For tokens where the whitespace before each line needs to be also
71/// reformatted, for example for tokens supporting reflow, there are analogous
72/// operations that might be executed before the main line breaking occurs:
Manuel Klimek93699f42017-11-29 14:29:43 +000073/// - getReflowSplit, for finding a split such that the content preceding it
Krasimir Georgiev91834222017-01-25 13:58:58 +000074/// needs to be specially reflown,
Manuel Klimek93699f42017-11-29 14:29:43 +000075/// - reflow, for executing the split using a whitespace manager,
Krasimir Georgiev35599fd2017-10-16 09:08:53 +000076/// - introducesBreakBefore, for checking if reformatting the beginning
77/// of the content introduces a line break before it,
Manuel Klimek93699f42017-11-29 14:29:43 +000078/// - adaptStartOfLine, for executing the reflow using a whitespace
Krasimir Georgiev91834222017-01-25 13:58:58 +000079/// manager.
80///
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +000081/// For tokens that require the whitespace after the last line to be
82/// reformatted, for example in multiline jsdoc comments that require the
83/// trailing '*/' to be on a line of itself, there are analogous operations
84/// that might be executed after the last line has been reformatted:
85/// - getSplitAfterLastLine, for finding a split after the last line that needs
86/// to be reflown,
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +000087/// - replaceWhitespaceAfterLastLine, for executing the reflow using a
88/// whitespace manager.
89///
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000090class BreakableToken {
91public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000092 /// Contains starting character index and length of split.
Manuel Klimek9043c742013-05-27 15:23:34 +000093 typedef std::pair<StringRef::size_type, unsigned> Split;
94
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000095 virtual ~BreakableToken() {}
Manuel Klimek9043c742013-05-27 15:23:34 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 /// Returns the number of lines in this token in the original code.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000098 virtual unsigned getLineCount() const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000099
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000100 /// Returns the number of columns required to format the text in the
Manuel Klimek93699f42017-11-29 14:29:43 +0000101 /// byte range [\p Offset, \p Offset \c + \p Length).
Manuel Klimek9043c742013-05-27 15:23:34 +0000102 ///
Manuel Klimek93699f42017-11-29 14:29:43 +0000103 /// \p Offset is the byte offset from the start of the content of the line
104 /// at \p LineIndex.
105 ///
106 /// \p StartColumn is the column at which the text starts in the formatted
107 /// file, needed to compute tab stops correctly.
108 virtual unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
109 StringRef::size_type Length,
110 unsigned StartColumn) const = 0;
111
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000112 /// Returns the number of columns required to format the text following
Manuel Klimek93699f42017-11-29 14:29:43 +0000113 /// the byte \p Offset in the line \p LineIndex, including potentially
114 /// unbreakable sequences of tokens following after the end of the token.
115 ///
116 /// \p Offset is the byte offset from the start of the content of the line
117 /// at \p LineIndex.
118 ///
119 /// \p StartColumn is the column at which the text starts in the formatted
120 /// file, needed to compute tab stops correctly.
121 ///
122 /// For breakable tokens that never use extra space at the end of a line, this
123 /// is equivalent to getRangeLength with a Length of StringRef::npos.
124 virtual unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
125 unsigned StartColumn) const {
126 return getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn);
127 }
128
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000129 /// Returns the column at which content in line \p LineIndex starts,
Manuel Klimek93699f42017-11-29 14:29:43 +0000130 /// assuming no reflow.
131 ///
132 /// If \p Break is true, returns the column at which the line should start
133 /// after the line break.
134 /// If \p Break is false, returns the column at which the line itself will
135 /// start.
136 virtual unsigned getContentStartColumn(unsigned LineIndex,
137 bool Break) const = 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000138
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000139 /// Returns additional content indent required for the second line after the
140 /// content at line \p LineIndex is broken.
141 ///
142 /// For example, Javadoc @param annotations require and indent of 4 spaces and
143 /// in this example getContentIndex(1) returns 4.
144 /// /**
145 /// * @param loooooooooooooong line
146 /// * continuation
147 /// */
148 virtual unsigned getContentIndent(unsigned LineIndex) const {
149 return 0;
150 }
151
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000152 /// Returns a range (offset, length) at which to break the line at
Manuel Klimek9043c742013-05-27 15:23:34 +0000153 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
Manuel Klimek93699f42017-11-29 14:29:43 +0000154 /// violate \p ColumnLimit, assuming the text starting at \p TailOffset in
155 /// the token is formatted starting at ContentStartColumn in the reformatted
156 /// file.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000157 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Manuel Klimek93699f42017-11-29 14:29:43 +0000158 unsigned ColumnLimit, unsigned ContentStartColumn,
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000159 llvm::Regex &CommentPragmasRegex) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000160
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000161 /// Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000162 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000163 unsigned ContentIndent,
Manuel Klimek93699f42017-11-29 14:29:43 +0000164 WhitespaceManager &Whitespaces) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000165
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000166 /// Returns the number of columns needed to format
Manuel Klimek93699f42017-11-29 14:29:43 +0000167 /// \p RemainingTokenColumns, assuming that Split is within the range measured
168 /// by \p RemainingTokenColumns, and that the whitespace in Split is reduced
169 /// to a single space.
170 unsigned getLengthAfterCompression(unsigned RemainingTokenColumns,
171 Split Split) const;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000172
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000173 /// Replaces the whitespace range described by \p Split with a single
Alexander Kornienko875395f2013-11-12 17:50:13 +0000174 /// space.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000175 virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
176 Split Split,
Manuel Klimek93699f42017-11-29 14:29:43 +0000177 WhitespaceManager &Whitespaces) const = 0;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000178
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000179 /// Returns whether the token supports reflowing text.
Manuel Klimek93699f42017-11-29 14:29:43 +0000180 virtual bool supportsReflow() const { return false; }
181
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000182 /// Returns a whitespace range (offset, length) of the content at \p
Manuel Klimek93699f42017-11-29 14:29:43 +0000183 /// LineIndex such that the content of that line is reflown to the end of the
184 /// previous one.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000185 ///
Manuel Klimek93699f42017-11-29 14:29:43 +0000186 /// Returning (StringRef::npos, 0) indicates reflowing is not possible.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000187 ///
Manuel Klimek93699f42017-11-29 14:29:43 +0000188 /// The range will include any whitespace preceding the specified line's
189 /// content.
190 ///
191 /// If the split is not contained within one token, for example when reflowing
192 /// line comments, returns (0, <length>).
193 virtual Split getReflowSplit(unsigned LineIndex,
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000194 llvm::Regex &CommentPragmasRegex) const {
Krasimir Georgiev91834222017-01-25 13:58:58 +0000195 return Split(StringRef::npos, 0);
196 }
197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// Reflows the current line into the end of the previous one.
Manuel Klimek93699f42017-11-29 14:29:43 +0000199 virtual void reflow(unsigned LineIndex,
200 WhitespaceManager &Whitespaces) const {}
201
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000202 /// Returns whether there will be a line break at the start of the
Manuel Klimek77866142017-11-17 11:17:15 +0000203 /// token.
204 virtual bool introducesBreakBeforeToken() const {
Krasimir Georgiev35599fd2017-10-16 09:08:53 +0000205 return false;
206 }
207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
Manuel Klimek93699f42017-11-29 14:29:43 +0000209 virtual void adaptStartOfLine(unsigned LineIndex,
210 WhitespaceManager &Whitespaces) const {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000212 /// Returns a whitespace range (offset, length) of the content at
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000213 /// the last line that needs to be reformatted after the last line has been
214 /// reformatted.
215 ///
216 /// A result having offset == StringRef::npos means that no reformat is
217 /// necessary.
Manuel Klimek93699f42017-11-29 14:29:43 +0000218 virtual Split getSplitAfterLastLine(unsigned TailOffset) const {
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000219 return Split(StringRef::npos, 0);
220 }
221
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000222 /// Replaces the whitespace from \p SplitAfterLastLine on the last line
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000223 /// after the last line has been formatted by performing a reformatting.
Manuel Klimek93699f42017-11-29 14:29:43 +0000224 void replaceWhitespaceAfterLastLine(unsigned TailOffset,
225 Split SplitAfterLastLine,
226 WhitespaceManager &Whitespaces) const {
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000227 insertBreak(getLineCount() - 1, TailOffset, SplitAfterLastLine,
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000228 /*ContentIndent=*/0, Whitespaces);
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000229 }
230
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000231 /// Updates the next token of \p State to the next token after this
Krasimir Georgiev91834222017-01-25 13:58:58 +0000232 /// one. This can be used when this token manages a set of underlying tokens
233 /// as a unit and is responsible for the formatting of the them.
234 virtual void updateNextToken(LineState &State) const {}
235
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000236protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000237 BreakableToken(const FormatToken &Tok, bool InPPDirective,
238 encoding::Encoding Encoding, const FormatStyle &Style)
239 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
240 Style(Style) {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000241
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000242 const FormatToken &Tok;
Alexander Kornienkobe633902013-06-14 11:46:10 +0000243 const bool InPPDirective;
244 const encoding::Encoding Encoding;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000245 const FormatStyle &Style;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000246};
247
Manuel Klimek93699f42017-11-29 14:29:43 +0000248class BreakableStringLiteral : public BreakableToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000249public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000250 /// Creates a breakable token for a single line string literal.
Manuel Klimek9043c742013-05-27 15:23:34 +0000251 ///
252 /// \p StartColumn specifies the column in which the token will start
253 /// after formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000254 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
255 StringRef Prefix, StringRef Postfix,
Krasimir Georgiev55c23a12018-01-23 11:26:19 +0000256 unsigned UnbreakableTailLength, bool InPPDirective,
257 encoding::Encoding Encoding, const FormatStyle &Style);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000258
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000259 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
Krasimir Georgiev6bbc7062018-04-23 10:02:59 +0000260 unsigned ContentStartColumn,
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000261 llvm::Regex &CommentPragmasRegex) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000262 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000263 unsigned ContentIndent,
Manuel Klimek93699f42017-11-29 14:29:43 +0000264 WhitespaceManager &Whitespaces) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000265 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
Manuel Klimek93699f42017-11-29 14:29:43 +0000266 WhitespaceManager &Whitespaces) const override {}
267 unsigned getLineCount() const override;
268 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
269 StringRef::size_type Length,
270 unsigned StartColumn) const override;
271 unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
272 unsigned StartColumn) const override;
273 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
274
275protected:
276 // The column in which the token starts.
277 unsigned StartColumn;
278 // The prefix a line needs after a break in the token.
279 StringRef Prefix;
280 // The postfix a line needs before introducing a break.
281 StringRef Postfix;
282 // The token text excluding the prefix and postfix.
283 StringRef Line;
284 // Length of the sequence of tokens after this string literal that cannot
285 // contain line breaks.
286 unsigned UnbreakableTailLength;
Manuel Klimek9043c742013-05-27 15:23:34 +0000287};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000288
Krasimir Georgiev91834222017-01-25 13:58:58 +0000289class BreakableComment : public BreakableToken {
290protected:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000291 /// Creates a breakable token for a comment.
Manuel Klimek9043c742013-05-27 15:23:34 +0000292 ///
Krasimir Georgiev4b159222017-02-21 10:54:50 +0000293 /// \p StartColumn specifies the column in which the comment will start after
294 /// formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000295 BreakableComment(const FormatToken &Token, unsigned StartColumn,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000296 bool InPPDirective, encoding::Encoding Encoding,
297 const FormatStyle &Style);
Krasimir Georgiev91834222017-01-25 13:58:58 +0000298
299public:
Manuel Klimek93699f42017-11-29 14:29:43 +0000300 bool supportsReflow() const override { return true; }
Krasimir Georgiev91834222017-01-25 13:58:58 +0000301 unsigned getLineCount() const override;
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000302 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
Krasimir Georgiev6bbc7062018-04-23 10:02:59 +0000303 unsigned ContentStartColumn,
Krasimir Georgiev17725d82017-03-08 08:55:12 +0000304 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000305 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
Manuel Klimek93699f42017-11-29 14:29:43 +0000306 WhitespaceManager &Whitespaces) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000307
308protected:
Krasimir Georgiev91834222017-01-25 13:58:58 +0000309 // Returns the token containing the line at LineIndex.
310 const FormatToken &tokenAt(unsigned LineIndex) const;
311
312 // Checks if the content of line LineIndex may be reflown with the previous
313 // line.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000314 virtual bool mayReflow(unsigned LineIndex,
315 llvm::Regex &CommentPragmasRegex) const = 0;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000316
317 // Contains the original text of the lines of the block comment.
318 //
319 // In case of a block comments, excludes the leading /* in the first line and
320 // trailing */ in the last line. In case of line comments, excludes the
321 // leading // and spaces.
322 SmallVector<StringRef, 16> Lines;
323
324 // Contains the text of the lines excluding all leading and trailing
325 // whitespace between the lines. Note that the decoration (if present) is also
326 // not considered part of the text.
327 SmallVector<StringRef, 16> Content;
328
329 // Tokens[i] contains a reference to the token containing Lines[i] if the
330 // whitespace range before that token is managed by this block.
331 // Otherwise, Tokens[i] is a null pointer.
332 SmallVector<FormatToken *, 16> Tokens;
333
334 // ContentColumn[i] is the target column at which Content[i] should be.
335 // Note that this excludes a leading "* " or "*" in case of block comments
336 // where all lines have a "*" prefix, or the leading "// " or "//" in case of
337 // line comments.
338 //
339 // In block comments, the first line's target column is always positive. The
340 // remaining lines' target columns are relative to the first line to allow
341 // correct indentation of comments in \c WhitespaceManager. Thus they can be
342 // negative as well (in case the first line needs to be unindented more than
343 // there's actual whitespace in another line).
344 SmallVector<int, 16> ContentColumn;
345
346 // The intended start column of the first line of text from this section.
347 unsigned StartColumn;
348
Krasimir Georgiev91834222017-01-25 13:58:58 +0000349 // The prefix to use in front a line that has been reflown up.
350 // For example, when reflowing the second line after the first here:
351 // // comment 1
352 // // comment 2
353 // we expect:
354 // // comment 1 comment 2
355 // and not:
356 // // comment 1comment 2
357 StringRef ReflowPrefix = " ";
358};
359
360class BreakableBlockComment : public BreakableComment {
361public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000362 BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
363 unsigned OriginalStartColumn, bool FirstInLine,
364 bool InPPDirective, encoding::Encoding Encoding,
365 const FormatStyle &Style);
Manuel Klimek9043c742013-05-27 15:23:34 +0000366
Manuel Klimek93699f42017-11-29 14:29:43 +0000367 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
368 StringRef::size_type Length,
369 unsigned StartColumn) const override;
370 unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
371 unsigned StartColumn) const override;
372 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000373 unsigned getContentIndent(unsigned LineIndex) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000374 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000375 unsigned ContentIndent,
Manuel Klimek93699f42017-11-29 14:29:43 +0000376 WhitespaceManager &Whitespaces) const override;
377 Split getReflowSplit(unsigned LineIndex,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000378 llvm::Regex &CommentPragmasRegex) const override;
Manuel Klimek93699f42017-11-29 14:29:43 +0000379 void reflow(unsigned LineIndex,
380 WhitespaceManager &Whitespaces) const override;
Manuel Klimek77866142017-11-17 11:17:15 +0000381 bool introducesBreakBeforeToken() const override;
Manuel Klimek93699f42017-11-29 14:29:43 +0000382 void adaptStartOfLine(unsigned LineIndex,
383 WhitespaceManager &Whitespaces) const override;
384 Split getSplitAfterLastLine(unsigned TailOffset) const override;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000385
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000386 bool mayReflow(unsigned LineIndex,
387 llvm::Regex &CommentPragmasRegex) const override;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000388
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000389 // Contains Javadoc annotations that require additional indent when continued
390 // on multiple lines.
391 static const llvm::StringSet<> ContentIndentingJavadocAnnotations;
392
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000393private:
Krasimir Georgiev91834222017-01-25 13:58:58 +0000394 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
Manuel Klimek9043c742013-05-27 15:23:34 +0000395 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000396 // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
397 // leading and trailing whitespace.
398 //
399 // Sets ContentColumn to the intended column in which the text at
Manuel Klimek9043c742013-05-27 15:23:34 +0000400 // Lines[LineIndex] starts (note that the decoration, if present, is not
401 // considered part of the text).
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000402 void adjustWhitespace(unsigned LineIndex, int IndentDelta);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000403
Manuel Klimek9043c742013-05-27 15:23:34 +0000404 // The column at which the text of a broken line should start.
405 // Note that an optional decoration would go before that column.
406 // IndentAtLineBreak is a uniform position for all lines in a block comment,
407 // regardless of their relative position.
408 // FIXME: Revisit the decision to do this; the main reason was to support
409 // patterns like
410 // /**************//**
411 // * Comment
412 // We could also support such patterns by special casing the first line
413 // instead.
414 unsigned IndentAtLineBreak;
415
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000416 // This is to distinguish between the case when the last line was empty and
417 // the case when it started with a decoration ("*" or "* ").
418 bool LastLineNeedsDecoration;
419
Manuel Klimek9043c742013-05-27 15:23:34 +0000420 // Either "* " if all lines begin with a "*", or empty.
421 StringRef Decoration;
Krasimir Georgievbb99a362017-02-16 12:39:31 +0000422
423 // If this block comment has decorations, this is the column of the start of
424 // the decorations.
425 unsigned DecorationColumn;
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000426
427 // If true, make sure that the opening '/**' and the closing '*/' ends on a
428 // line of itself. Styles like jsdoc require this for multiline comments.
429 bool DelimitersOnNewline;
Manuel Klimek48c930c2017-12-04 08:53:16 +0000430
431 // Length of the sequence of tokens after this string literal that cannot
432 // contain line breaks.
433 unsigned UnbreakableTailLength;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000434};
435
Krasimir Georgiev91834222017-01-25 13:58:58 +0000436class BreakableLineCommentSection : public BreakableComment {
437public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000438 BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000439 unsigned OriginalStartColumn, bool FirstInLine,
440 bool InPPDirective, encoding::Encoding Encoding,
441 const FormatStyle &Style);
442
Manuel Klimek93699f42017-11-29 14:29:43 +0000443 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
444 StringRef::size_type Length,
445 unsigned StartColumn) const override;
446 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000447 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Krasimir Georgiev6a5c95b2018-07-30 08:45:45 +0000448 unsigned ContentIndent,
Manuel Klimek93699f42017-11-29 14:29:43 +0000449 WhitespaceManager &Whitespaces) const override;
450 Split getReflowSplit(unsigned LineIndex,
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000451 llvm::Regex &CommentPragmasRegex) const override;
Manuel Klimek93699f42017-11-29 14:29:43 +0000452 void reflow(unsigned LineIndex,
453 WhitespaceManager &Whitespaces) const override;
454 void adaptStartOfLine(unsigned LineIndex,
455 WhitespaceManager &Whitespaces) const override;
Krasimir Georgieva7a24bf2017-03-08 08:58:44 +0000456 void updateNextToken(LineState &State) const override;
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000457 bool mayReflow(unsigned LineIndex,
458 llvm::Regex &CommentPragmasRegex) const override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000459
460private:
Krasimir Georgiev2091a3a2017-02-08 14:45:19 +0000461 // OriginalPrefix[i] contains the original prefix of line i, including
462 // trailing whitespace before the start of the content. The indentation
463 // preceding the prefix is not included.
464 // For example, if the line is:
465 // // content
466 // then the original prefix is "// ".
467 SmallVector<StringRef, 16> OriginalPrefix;
468
Krasimir Georgiev91834222017-01-25 13:58:58 +0000469 // Prefix[i] contains the intended leading "//" with trailing spaces to
470 // account for the indentation of content within the comment at line i after
471 // formatting. It can be different than the original prefix when the original
472 // line starts like this:
473 // //content
474 // Then the original prefix is "//", but the prefix is "// ".
475 SmallVector<StringRef, 16> Prefix;
476
477 SmallVector<unsigned, 16> OriginalContentColumn;
478
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000479 /// The token to which the last line of this breakable token belongs
Krasimir Georgiev91834222017-01-25 13:58:58 +0000480 /// to; nullptr if that token is the initial token.
481 ///
482 /// The distinction is because if the token of the last line of this breakable
483 /// token is distinct from the initial token, this breakable token owns the
484 /// whitespace before the token of the last line, and the whitespace manager
485 /// must be able to modify it.
486 FormatToken *LastLineTok = nullptr;
487};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000488} // namespace format
489} // namespace clang
490
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000491#endif