Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- 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 Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 11 | /// \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 Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 18 | #ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H |
| 19 | #define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 20 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 21 | #include "Encoding.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 22 | #include "TokenAnnotator.h" |
| 23 | #include "WhitespaceManager.h" |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Regex.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 25 | #include <utility> |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace format { |
| 29 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 30 | /// \brief Checks if \p Token switches formatting, like /* clang-format off */. |
| 31 | /// \p Token must be a comment. |
| 32 | bool switchesFormatting(const FormatToken &Token); |
| 33 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 34 | struct FormatStyle; |
| 35 | |
| 36 | /// \brief Base class for strategies on how to break tokens. |
| 37 | /// |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 38 | /// 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 | /// |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 67 | /// FIXME: The interface seems set in stone, so we might want to just pull the |
| 68 | /// strategy into the class, instead of controlling it from the outside. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 69 | class BreakableToken { |
| 70 | public: |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 71 | /// \brief Contains starting character index and length of split. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 72 | typedef std::pair<StringRef::size_type, unsigned> Split; |
| 73 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 74 | virtual ~BreakableToken() {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 75 | |
| 76 | /// \brief Returns the number of lines in this token in the original code. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 77 | virtual unsigned getLineCount() const = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 78 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 79 | /// \brief Returns the number of columns required to format the piece of line |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 80 | /// at \p LineIndex, from byte offset \p TailOffset with length \p Length. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 81 | /// |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 82 | /// Note that previous breaks are not taken into account. \p TailOffset is |
| 83 | /// always specified from the start of the (original) line. |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 84 | /// \p Length can be set to StringRef::npos, which means "to the end of line". |
| 85 | virtual unsigned |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 86 | getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset, |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 87 | StringRef::size_type Length) const = 0; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 88 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 89 | /// \brief Returns a range (offset, length) at which to break the line at |
| 90 | /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not |
| 91 | /// violate \p ColumnLimit. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 92 | virtual Split getSplit(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 93 | unsigned ColumnLimit) const = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 94 | |
| 95 | /// \brief Emits the previously retrieved \p Split via \p Whitespaces. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 96 | virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 97 | WhitespaceManager &Whitespaces) = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 98 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 99 | /// \brief Returns the number of columns required to format the piece of line |
| 100 | /// at \p LineIndex, from byte offset \p TailOffset after the whitespace range |
| 101 | /// \p Split has been compressed into a single space. |
| 102 | unsigned getLineLengthAfterCompression(unsigned RemainingTokenColumns, |
| 103 | Split Split) const; |
| 104 | |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 105 | /// \brief Replaces the whitespace range described by \p Split with a single |
| 106 | /// space. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 107 | virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset, |
| 108 | Split Split, |
| 109 | WhitespaceManager &Whitespaces) = 0; |
| 110 | |
| 111 | /// \brief Returns a whitespace range (offset, length) of the content at |
| 112 | /// \p LineIndex such that the content preceding this range needs to be |
| 113 | /// reformatted before any breaks are made to this line. |
| 114 | /// |
| 115 | /// \p PreviousEndColumn is the end column of the previous line after |
| 116 | /// formatting. |
| 117 | /// |
| 118 | /// A result having offset == StringRef::npos means that no piece of the line |
| 119 | /// needs to be reformatted before any breaks are made. |
| 120 | virtual Split getSplitBefore(unsigned LineIndex, |
| 121 | unsigned PreviousEndColumn, |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 122 | unsigned ColumnLimit, |
| 123 | llvm::Regex& CommentPragmasRegex) const { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 124 | return Split(StringRef::npos, 0); |
| 125 | } |
| 126 | |
| 127 | /// \brief Returns the number of columns required to format the piece of line |
| 128 | /// at \p LineIndex after the content preceding the whitespace range specified |
| 129 | /// \p SplitBefore has been reformatted, but before any breaks are made to |
| 130 | /// this line. |
| 131 | virtual unsigned getLineLengthAfterSplitBefore(unsigned LineIndex, |
| 132 | unsigned TailOffset, |
| 133 | unsigned PreviousEndColumn, |
| 134 | unsigned ColumnLimit, |
| 135 | Split SplitBefore) const { |
| 136 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 137 | } |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 138 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 139 | /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 140 | /// Performs a reformatting of the content at \p LineIndex preceding the |
| 141 | /// whitespace range \p SplitBefore. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 142 | virtual void replaceWhitespaceBefore(unsigned LineIndex, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 143 | unsigned PreviousEndColumn, |
| 144 | unsigned ColumnLimit, |
| 145 | Split SplitBefore, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 146 | WhitespaceManager &Whitespaces) {} |
| 147 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 148 | /// \brief Updates the next token of \p State to the next token after this |
| 149 | /// one. This can be used when this token manages a set of underlying tokens |
| 150 | /// as a unit and is responsible for the formatting of the them. |
| 151 | virtual void updateNextToken(LineState &State) const {} |
| 152 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 153 | protected: |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 154 | BreakableToken(const FormatToken &Tok, bool InPPDirective, |
| 155 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 156 | : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding), |
| 157 | Style(Style) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 158 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 159 | const FormatToken &Tok; |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 160 | const bool InPPDirective; |
| 161 | const encoding::Encoding Encoding; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 162 | const FormatStyle &Style; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 165 | /// \brief Base class for single line tokens that can be broken. |
| 166 | /// |
| 167 | /// \c getSplit() needs to be implemented by child classes. |
| 168 | class BreakableSingleLineToken : public BreakableToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 169 | public: |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 170 | unsigned getLineCount() const override; |
| 171 | unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset, |
| 172 | StringRef::size_type Length) const override; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 173 | |
| 174 | protected: |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 175 | BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn, |
| 176 | StringRef Prefix, StringRef Postfix, |
| 177 | bool InPPDirective, encoding::Encoding Encoding, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 178 | const FormatStyle &Style); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 179 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 180 | // The column in which the token starts. |
| 181 | unsigned StartColumn; |
| 182 | // The prefix a line needs after a break in the token. |
| 183 | StringRef Prefix; |
| 184 | // The postfix a line needs before introducing a break. |
| 185 | StringRef Postfix; |
| 186 | // The token text excluding the prefix and postfix. |
| 187 | StringRef Line; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 190 | class BreakableStringLiteral : public BreakableSingleLineToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 191 | public: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 192 | /// \brief Creates a breakable token for a single line string literal. |
| 193 | /// |
| 194 | /// \p StartColumn specifies the column in which the token will start |
| 195 | /// after formatting. |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 196 | BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn, |
| 197 | StringRef Prefix, StringRef Postfix, |
| 198 | bool InPPDirective, encoding::Encoding Encoding, |
| 199 | const FormatStyle &Style); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 200 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 201 | Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 202 | unsigned ColumnLimit) const override; |
| 203 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 204 | WhitespaceManager &Whitespaces) override; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 205 | void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 206 | WhitespaceManager &Whitespaces) override {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 207 | }; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 208 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 209 | class BreakableComment : public BreakableToken { |
| 210 | protected: |
| 211 | /// \brief Creates a breakable token for a comment. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 212 | /// |
Krasimir Georgiev | 4b15922 | 2017-02-21 10:54:50 +0000 | [diff] [blame^] | 213 | /// \p StartColumn specifies the column in which the comment will start after |
| 214 | /// formatting. |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 215 | BreakableComment(const FormatToken &Token, unsigned StartColumn, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 216 | bool InPPDirective, encoding::Encoding Encoding, |
| 217 | const FormatStyle &Style); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 218 | |
| 219 | public: |
| 220 | unsigned getLineCount() const override; |
| 221 | Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 222 | unsigned ColumnLimit) const override; |
| 223 | void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 224 | WhitespaceManager &Whitespaces) override; |
| 225 | |
| 226 | protected: |
| 227 | virtual unsigned getContentStartColumn(unsigned LineIndex, |
| 228 | unsigned TailOffset) const = 0; |
| 229 | |
| 230 | // Returns a split that divides Text into a left and right parts, such that |
| 231 | // the left part is suitable for reflowing after PreviousEndColumn. |
| 232 | Split getReflowSplit(StringRef Text, StringRef ReflowPrefix, |
| 233 | unsigned PreviousEndColumn, unsigned ColumnLimit) const; |
| 234 | |
| 235 | // Returns the token containing the line at LineIndex. |
| 236 | const FormatToken &tokenAt(unsigned LineIndex) const; |
| 237 | |
| 238 | // Checks if the content of line LineIndex may be reflown with the previous |
| 239 | // line. |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 240 | virtual bool mayReflow(unsigned LineIndex, |
| 241 | llvm::Regex &CommentPragmasRegex) const = 0; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 242 | |
| 243 | // Contains the original text of the lines of the block comment. |
| 244 | // |
| 245 | // In case of a block comments, excludes the leading /* in the first line and |
| 246 | // trailing */ in the last line. In case of line comments, excludes the |
| 247 | // leading // and spaces. |
| 248 | SmallVector<StringRef, 16> Lines; |
| 249 | |
| 250 | // Contains the text of the lines excluding all leading and trailing |
| 251 | // whitespace between the lines. Note that the decoration (if present) is also |
| 252 | // not considered part of the text. |
| 253 | SmallVector<StringRef, 16> Content; |
| 254 | |
| 255 | // Tokens[i] contains a reference to the token containing Lines[i] if the |
| 256 | // whitespace range before that token is managed by this block. |
| 257 | // Otherwise, Tokens[i] is a null pointer. |
| 258 | SmallVector<FormatToken *, 16> Tokens; |
| 259 | |
| 260 | // ContentColumn[i] is the target column at which Content[i] should be. |
| 261 | // Note that this excludes a leading "* " or "*" in case of block comments |
| 262 | // where all lines have a "*" prefix, or the leading "// " or "//" in case of |
| 263 | // line comments. |
| 264 | // |
| 265 | // In block comments, the first line's target column is always positive. The |
| 266 | // remaining lines' target columns are relative to the first line to allow |
| 267 | // correct indentation of comments in \c WhitespaceManager. Thus they can be |
| 268 | // negative as well (in case the first line needs to be unindented more than |
| 269 | // there's actual whitespace in another line). |
| 270 | SmallVector<int, 16> ContentColumn; |
| 271 | |
| 272 | // The intended start column of the first line of text from this section. |
| 273 | unsigned StartColumn; |
| 274 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 275 | // The prefix to use in front a line that has been reflown up. |
| 276 | // For example, when reflowing the second line after the first here: |
| 277 | // // comment 1 |
| 278 | // // comment 2 |
| 279 | // we expect: |
| 280 | // // comment 1 comment 2 |
| 281 | // and not: |
| 282 | // // comment 1comment 2 |
| 283 | StringRef ReflowPrefix = " "; |
| 284 | }; |
| 285 | |
| 286 | class BreakableBlockComment : public BreakableComment { |
| 287 | public: |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 288 | BreakableBlockComment(const FormatToken &Token, unsigned StartColumn, |
| 289 | unsigned OriginalStartColumn, bool FirstInLine, |
| 290 | bool InPPDirective, encoding::Encoding Encoding, |
| 291 | const FormatStyle &Style); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 292 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 293 | unsigned getLineLengthAfterSplit(unsigned LineIndex, |
| 294 | unsigned TailOffset, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 295 | StringRef::size_type Length) const override; |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 296 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 297 | WhitespaceManager &Whitespaces) override; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 298 | Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn, |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 299 | unsigned ColumnLimit, |
| 300 | llvm::Regex &CommentPragmasRegex) const override; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 301 | unsigned getLineLengthAfterSplitBefore(unsigned LineIndex, |
| 302 | unsigned TailOffset, |
| 303 | unsigned PreviousEndColumn, |
| 304 | unsigned ColumnLimit, |
| 305 | Split SplitBefore) const override; |
| 306 | void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn, |
| 307 | unsigned ColumnLimit, |
| 308 | Split SplitBefore, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 309 | WhitespaceManager &Whitespaces) override; |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 310 | bool mayReflow(unsigned LineIndex, |
| 311 | llvm::Regex &CommentPragmasRegex) const override; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 312 | |
| 313 | private: |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 314 | // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex]. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 315 | // |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 316 | // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off |
| 317 | // leading and trailing whitespace. |
| 318 | // |
| 319 | // Sets ContentColumn to the intended column in which the text at |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 320 | // Lines[LineIndex] starts (note that the decoration, if present, is not |
| 321 | // considered part of the text). |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 322 | void adjustWhitespace(unsigned LineIndex, int IndentDelta); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 323 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 324 | // Computes the end column if the full Content from LineIndex gets reflown |
| 325 | // after PreviousEndColumn. |
| 326 | unsigned getReflownColumn(StringRef Content, |
| 327 | unsigned LineIndex, |
| 328 | unsigned PreviousEndColumn) const; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 329 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 330 | unsigned getContentStartColumn(unsigned LineIndex, |
| 331 | unsigned TailOffset) const override; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 332 | |
| 333 | // The column at which the text of a broken line should start. |
| 334 | // Note that an optional decoration would go before that column. |
| 335 | // IndentAtLineBreak is a uniform position for all lines in a block comment, |
| 336 | // regardless of their relative position. |
| 337 | // FIXME: Revisit the decision to do this; the main reason was to support |
| 338 | // patterns like |
| 339 | // /**************//** |
| 340 | // * Comment |
| 341 | // We could also support such patterns by special casing the first line |
| 342 | // instead. |
| 343 | unsigned IndentAtLineBreak; |
| 344 | |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 345 | // This is to distinguish between the case when the last line was empty and |
| 346 | // the case when it started with a decoration ("*" or "* "). |
| 347 | bool LastLineNeedsDecoration; |
| 348 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 349 | // Either "* " if all lines begin with a "*", or empty. |
| 350 | StringRef Decoration; |
Krasimir Georgiev | bb99a36 | 2017-02-16 12:39:31 +0000 | [diff] [blame] | 351 | |
| 352 | // If this block comment has decorations, this is the column of the start of |
| 353 | // the decorations. |
| 354 | unsigned DecorationColumn; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 357 | class BreakableLineCommentSection : public BreakableComment { |
| 358 | public: |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 359 | BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 360 | unsigned OriginalStartColumn, bool FirstInLine, |
| 361 | bool InPPDirective, encoding::Encoding Encoding, |
| 362 | const FormatStyle &Style); |
| 363 | |
| 364 | unsigned getLineLengthAfterSplit(unsigned LineIndex, |
| 365 | unsigned TailOffset, |
| 366 | StringRef::size_type Length) const override; |
| 367 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 368 | WhitespaceManager &Whitespaces) override; |
| 369 | Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn, |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 370 | unsigned ColumnLimit, |
| 371 | llvm::Regex &CommentPragmasRegex) const override; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 372 | unsigned getLineLengthAfterSplitBefore(unsigned LineIndex, unsigned TailOffset, |
| 373 | unsigned PreviousEndColumn, |
| 374 | unsigned ColumnLimit, |
| 375 | Split SplitBefore) const override; |
| 376 | void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn, |
| 377 | unsigned ColumnLimit, Split SplitBefore, |
| 378 | WhitespaceManager &Whitespaces) override; |
| 379 | void updateNextToken(LineState& State) const override; |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 380 | bool mayReflow(unsigned LineIndex, |
| 381 | llvm::Regex &CommentPragmasRegex) const override; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 382 | |
| 383 | private: |
| 384 | unsigned getContentStartColumn(unsigned LineIndex, |
| 385 | unsigned TailOffset) const override; |
| 386 | |
Krasimir Georgiev | 2091a3a | 2017-02-08 14:45:19 +0000 | [diff] [blame] | 387 | // OriginalPrefix[i] contains the original prefix of line i, including |
| 388 | // trailing whitespace before the start of the content. The indentation |
| 389 | // preceding the prefix is not included. |
| 390 | // For example, if the line is: |
| 391 | // // content |
| 392 | // then the original prefix is "// ". |
| 393 | SmallVector<StringRef, 16> OriginalPrefix; |
| 394 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 395 | // Prefix[i] contains the intended leading "//" with trailing spaces to |
| 396 | // account for the indentation of content within the comment at line i after |
| 397 | // formatting. It can be different than the original prefix when the original |
| 398 | // line starts like this: |
| 399 | // //content |
| 400 | // Then the original prefix is "//", but the prefix is "// ". |
| 401 | SmallVector<StringRef, 16> Prefix; |
| 402 | |
| 403 | SmallVector<unsigned, 16> OriginalContentColumn; |
| 404 | |
| 405 | /// \brief The token to which the last line of this breakable token belongs |
| 406 | /// to; nullptr if that token is the initial token. |
| 407 | /// |
| 408 | /// The distinction is because if the token of the last line of this breakable |
| 409 | /// token is distinct from the initial token, this breakable token owns the |
| 410 | /// whitespace before the token of the last line, and the whitespace manager |
| 411 | /// must be able to modify it. |
| 412 | FormatToken *LastLineTok = nullptr; |
| 413 | }; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 414 | } // namespace format |
| 415 | } // namespace clang |
| 416 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 417 | #endif |