BreakableToken.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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
  11. /// \brief Declares BreakableToken, BreakableStringLiteral, and
  12. /// BreakableBlockComment classes, that contain token type-specific logic to
  13. /// break long lines in tokens.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
  17. #define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
  18. #include "Encoding.h"
  19. #include "TokenAnnotator.h"
  20. #include "WhitespaceManager.h"
  21. #include <utility>
  22. namespace clang {
  23. namespace format {
  24. struct FormatStyle;
  25. /// \brief Base class for strategies on how to break tokens.
  26. ///
  27. /// FIXME: The interface seems set in stone, so we might want to just pull the
  28. /// strategy into the class, instead of controlling it from the outside.
  29. class BreakableToken {
  30. public:
  31. /// \brief Contains starting character index and length of split.
  32. typedef std::pair<StringRef::size_type, unsigned> Split;
  33. virtual ~BreakableToken() {}
  34. /// \brief Returns the number of lines in this token in the original code.
  35. virtual unsigned getLineCount() const = 0;
  36. /// \brief Returns the number of columns required to format the piece of line
  37. /// at \p LineIndex, from byte offset \p Offset with length \p Length.
  38. ///
  39. /// Note that previous breaks are not taken into account. \p Offset is always
  40. /// specified from the start of the (original) line.
  41. /// \p Length can be set to StringRef::npos, which means "to the end of line".
  42. virtual unsigned
  43. getLineLengthAfterSplit(unsigned LineIndex, unsigned Offset,
  44. StringRef::size_type Length) const = 0;
  45. /// \brief Returns a range (offset, length) at which to break the line at
  46. /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
  47. /// violate \p ColumnLimit.
  48. virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
  49. unsigned ColumnLimit) const = 0;
  50. /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
  51. virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
  52. WhitespaceManager &Whitespaces) = 0;
  53. /// \brief Replaces the whitespace range described by \p Split with a single
  54. /// space.
  55. virtual void replaceWhitespace(unsigned LineIndex, unsigned TailOffset,
  56. Split Split,
  57. WhitespaceManager &Whitespaces) = 0;
  58. /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
  59. virtual void replaceWhitespaceBefore(unsigned LineIndex,
  60. WhitespaceManager &Whitespaces) {}
  61. protected:
  62. BreakableToken(const FormatToken &Tok, unsigned IndentLevel,
  63. bool InPPDirective, encoding::Encoding Encoding,
  64. const FormatStyle &Style)
  65. : Tok(Tok), IndentLevel(IndentLevel), InPPDirective(InPPDirective),
  66. Encoding(Encoding), Style(Style) {}
  67. const FormatToken &Tok;
  68. const unsigned IndentLevel;
  69. const bool InPPDirective;
  70. const encoding::Encoding Encoding;
  71. const FormatStyle &Style;
  72. };
  73. /// \brief Base class for single line tokens that can be broken.
  74. ///
  75. /// \c getSplit() needs to be implemented by child classes.
  76. class BreakableSingleLineToken : public BreakableToken {
  77. public:
  78. unsigned getLineCount() const override;
  79. unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
  80. StringRef::size_type Length) const override;
  81. protected:
  82. BreakableSingleLineToken(const FormatToken &Tok, unsigned IndentLevel,
  83. unsigned StartColumn, StringRef Prefix,
  84. StringRef Postfix, bool InPPDirective,
  85. encoding::Encoding Encoding,
  86. const FormatStyle &Style);
  87. // The column in which the token starts.
  88. unsigned StartColumn;
  89. // The prefix a line needs after a break in the token.
  90. StringRef Prefix;
  91. // The postfix a line needs before introducing a break.
  92. StringRef Postfix;
  93. // The token text excluding the prefix and postfix.
  94. StringRef Line;
  95. };
  96. class BreakableStringLiteral : public BreakableSingleLineToken {
  97. public:
  98. /// \brief Creates a breakable token for a single line string literal.
  99. ///
  100. /// \p StartColumn specifies the column in which the token will start
  101. /// after formatting.
  102. BreakableStringLiteral(const FormatToken &Tok, unsigned IndentLevel,
  103. unsigned StartColumn, StringRef Prefix,
  104. StringRef Postfix, bool InPPDirective,
  105. encoding::Encoding Encoding, const FormatStyle &Style);
  106. Split getSplit(unsigned LineIndex, unsigned TailOffset,
  107. unsigned ColumnLimit) const override;
  108. void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
  109. WhitespaceManager &Whitespaces) override;
  110. void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
  111. WhitespaceManager &Whitespaces) override {}
  112. };
  113. class BreakableLineComment : public BreakableSingleLineToken {
  114. public:
  115. /// \brief Creates a breakable token for a line comment.
  116. ///
  117. /// \p StartColumn specifies the column in which the comment will start
  118. /// after formatting.
  119. BreakableLineComment(const FormatToken &Token, unsigned IndentLevel,
  120. unsigned StartColumn, bool InPPDirective,
  121. encoding::Encoding Encoding, const FormatStyle &Style);
  122. Split getSplit(unsigned LineIndex, unsigned TailOffset,
  123. unsigned ColumnLimit) const override;
  124. void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
  125. WhitespaceManager &Whitespaces) override;
  126. void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
  127. WhitespaceManager &Whitespaces) override;
  128. void replaceWhitespaceBefore(unsigned LineIndex,
  129. WhitespaceManager &Whitespaces) override;
  130. private:
  131. // The prefix without an additional space if one was added.
  132. StringRef OriginalPrefix;
  133. };
  134. class BreakableBlockComment : public BreakableToken {
  135. public:
  136. /// \brief Creates a breakable token for a block comment.
  137. ///
  138. /// \p StartColumn specifies the column in which the comment will start
  139. /// after formatting, while \p OriginalStartColumn specifies in which
  140. /// column the comment started before formatting.
  141. /// If the comment starts a line after formatting, set \p FirstInLine to true.
  142. BreakableBlockComment(const FormatToken &Token, unsigned IndentLevel,
  143. unsigned StartColumn, unsigned OriginaStartColumn,
  144. bool FirstInLine, bool InPPDirective,
  145. encoding::Encoding Encoding, const FormatStyle &Style);
  146. unsigned getLineCount() const override;
  147. unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
  148. StringRef::size_type Length) const override;
  149. Split getSplit(unsigned LineIndex, unsigned TailOffset,
  150. unsigned ColumnLimit) const override;
  151. void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
  152. WhitespaceManager &Whitespaces) override;
  153. void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
  154. WhitespaceManager &Whitespaces) override;
  155. void replaceWhitespaceBefore(unsigned LineIndex,
  156. WhitespaceManager &Whitespaces) override;
  157. private:
  158. // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
  159. // so that all whitespace between the lines is accounted to Lines[LineIndex]
  160. // as leading whitespace:
  161. // - Lines[LineIndex] points to the text after that whitespace
  162. // - Lines[LineIndex-1] shrinks by its trailing whitespace
  163. // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
  164. // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
  165. //
  166. // Sets StartOfLineColumn to the intended column in which the text at
  167. // Lines[LineIndex] starts (note that the decoration, if present, is not
  168. // considered part of the text).
  169. void adjustWhitespace(unsigned LineIndex, int IndentDelta);
  170. // Returns the column at which the text in line LineIndex starts, when broken
  171. // at TailOffset. Note that the decoration (if present) is not considered part
  172. // of the text.
  173. unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const;
  174. // Contains the text of the lines of the block comment, excluding the leading
  175. // /* in the first line and trailing */ in the last line, and excluding all
  176. // trailing whitespace between the lines. Note that the decoration (if
  177. // present) is also not considered part of the text.
  178. SmallVector<StringRef, 16> Lines;
  179. // LeadingWhitespace[i] is the number of characters regarded as whitespace in
  180. // front of Lines[i]. Note that this can include "* " sequences, which we
  181. // regard as whitespace when all lines have a "*" prefix.
  182. SmallVector<unsigned, 16> LeadingWhitespace;
  183. // StartOfLineColumn[i] is the target column at which Line[i] should be.
  184. // Note that this excludes a leading "* " or "*" in case all lines have
  185. // a "*" prefix.
  186. // The first line's target column is always positive. The remaining lines'
  187. // target columns are relative to the first line to allow correct indentation
  188. // of comments in \c WhitespaceManager. Thus they can be negative as well (in
  189. // case the first line needs to be unindented more than there's actual
  190. // whitespace in another line).
  191. SmallVector<int, 16> StartOfLineColumn;
  192. // The column at which the text of a broken line should start.
  193. // Note that an optional decoration would go before that column.
  194. // IndentAtLineBreak is a uniform position for all lines in a block comment,
  195. // regardless of their relative position.
  196. // FIXME: Revisit the decision to do this; the main reason was to support
  197. // patterns like
  198. // /**************//**
  199. // * Comment
  200. // We could also support such patterns by special casing the first line
  201. // instead.
  202. unsigned IndentAtLineBreak;
  203. // This is to distinguish between the case when the last line was empty and
  204. // the case when it started with a decoration ("*" or "* ").
  205. bool LastLineNeedsDecoration;
  206. // Either "* " if all lines begin with a "*", or empty.
  207. StringRef Decoration;
  208. };
  209. } // namespace format
  210. } // namespace clang
  211. #endif