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