blob: 100bb19dc5df8826229eab9f51dbfb043d398cd2 [file] [log] [blame]
Alexander Kornienko70ce7882013-04-15 14:28:00 +00001//===--- BreakableToken.h - Format C++ code -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
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
17#ifndef LLVM_CLANG_FORMAT_BREAKABLETOKEN_H
18#define LLVM_CLANG_FORMAT_BREAKABLETOKEN_H
19
Alexander Kornienko00895102013-06-05 14:09:10 +000020#include "Encoding.h"
Alexander Kornienko70ce7882013-04-15 14:28:00 +000021#include "TokenAnnotator.h"
22#include "WhitespaceManager.h"
23#include <utility>
24
25namespace clang {
26namespace format {
27
Manuel Klimekde008c02013-05-27 15:23:34 +000028struct FormatStyle;
29
30/// \brief Base class for strategies on how to break tokens.
31///
32/// FIXME: The interface seems set in stone, so we might want to just pull the
33/// strategy into the class, instead of controlling it from the outside.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000034class BreakableToken {
35public:
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000036 /// \brief Contains starting character index and length of split.
Manuel Klimekde008c02013-05-27 15:23:34 +000037 typedef std::pair<StringRef::size_type, unsigned> Split;
38
Alexander Kornienko70ce7882013-04-15 14:28:00 +000039 virtual ~BreakableToken() {}
Manuel Klimekde008c02013-05-27 15:23:34 +000040
41 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000042 virtual unsigned getLineCount() const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000043
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000044 /// \brief Returns the number of columns required to format the piece of line
45 /// at \p LineIndex, from byte offset \p Offset with length \p Length.
Manuel Klimekde008c02013-05-27 15:23:34 +000046 ///
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000047 /// Note that previous breaks are not taken into account. \p Offset is always
48 /// specified from the start of the (original) line.
49 /// \p Length can be set to StringRef::npos, which means "to the end of line".
50 virtual unsigned
51 getLineLengthAfterSplit(unsigned LineIndex, unsigned Offset,
52 StringRef::size_type Length) const = 0;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000053
Manuel Klimekde008c02013-05-27 15:23:34 +000054 /// \brief Returns a range (offset, length) at which to break the line at
55 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
56 /// violate \p ColumnLimit.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000057 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko919398b2013-04-17 17:34:05 +000058 unsigned ColumnLimit) const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000059
60 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000061 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
62 bool InPPDirective,
63 WhitespaceManager &Whitespaces) = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000064
65 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
66 virtual void replaceWhitespaceBefore(unsigned LineIndex,
67 unsigned InPPDirective,
68 WhitespaceManager &Whitespaces) {}
69
Alexander Kornienko919398b2013-04-17 17:34:05 +000070protected:
Alexander Kornienko00895102013-06-05 14:09:10 +000071 BreakableToken(const FormatToken &Tok, encoding::Encoding Encoding)
72 : Tok(Tok), Encoding(Encoding) {}
Manuel Klimekde008c02013-05-27 15:23:34 +000073
Alexander Kornienko919398b2013-04-17 17:34:05 +000074 const FormatToken &Tok;
Alexander Kornienko00895102013-06-05 14:09:10 +000075 encoding::Encoding Encoding;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000076};
77
Manuel Klimekde008c02013-05-27 15:23:34 +000078/// \brief Base class for single line tokens that can be broken.
79///
80/// \c getSplit() needs to be implemented by child classes.
81class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +000082public:
Manuel Klimekde008c02013-05-27 15:23:34 +000083 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000084 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000085 unsigned TailOffset,
86 StringRef::size_type Length) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +000087 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
88 bool InPPDirective, WhitespaceManager &Whitespaces);
89
90protected:
Manuel Klimekde008c02013-05-27 15:23:34 +000091 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +000092 StringRef Prefix, StringRef Postfix,
93 encoding::Encoding Encoding);
Alexander Kornienko919398b2013-04-17 17:34:05 +000094
Manuel Klimekde008c02013-05-27 15:23:34 +000095 // The column in which the token starts.
96 unsigned StartColumn;
97 // The prefix a line needs after a break in the token.
98 StringRef Prefix;
99 // The postfix a line needs before introducing a break.
100 StringRef Postfix;
101 // The token text excluding the prefix and postfix.
102 StringRef Line;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000103};
104
Manuel Klimekde008c02013-05-27 15:23:34 +0000105class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000106public:
Manuel Klimekde008c02013-05-27 15:23:34 +0000107 /// \brief Creates a breakable token for a single line string literal.
108 ///
109 /// \p StartColumn specifies the column in which the token will start
110 /// after formatting.
Alexander Kornienko00895102013-06-05 14:09:10 +0000111 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
112 encoding::Encoding Encoding);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000113
Manuel Klimekde008c02013-05-27 15:23:34 +0000114 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
115 unsigned ColumnLimit) const;
116};
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000117
Manuel Klimekde008c02013-05-27 15:23:34 +0000118class BreakableLineComment : public BreakableSingleLineToken {
119public:
120 /// \brief Creates a breakable token for a line comment.
121 ///
122 /// \p StartColumn specifies the column in which the comment will start
123 /// after formatting.
Alexander Kornienko00895102013-06-05 14:09:10 +0000124 BreakableLineComment(const FormatToken &Token, unsigned StartColumn,
125 encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000126
127 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
128 unsigned ColumnLimit) const;
129};
130
131class BreakableBlockComment : public BreakableToken {
132public:
133 /// \brief Creates a breakable token for a block comment.
134 ///
135 /// \p StartColumn specifies the column in which the comment will start
136 /// after formatting, while \p OriginalStartColumn specifies in which
137 /// column the comment started before formatting.
138 /// If the comment starts a line after formatting, set \p FirstInLine to true.
139 BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token,
140 unsigned StartColumn, unsigned OriginaStartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +0000141 bool FirstInLine, encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000142
143 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000144 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000145 unsigned TailOffset,
146 StringRef::size_type Length) const;
Manuel Klimekde008c02013-05-27 15:23:34 +0000147 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
148 unsigned ColumnLimit) const;
149 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
150 bool InPPDirective, WhitespaceManager &Whitespaces);
151 virtual void replaceWhitespaceBefore(unsigned LineIndex,
152 unsigned InPPDirective,
153 WhitespaceManager &Whitespaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000154
155private:
Manuel Klimekde008c02013-05-27 15:23:34 +0000156 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
157 // so that all whitespace between the lines is accounted to Lines[LineIndex]
158 // as leading whitespace:
159 // - Lines[LineIndex] points to the text after that whitespace
160 // - Lines[LineIndex-1] shrinks by its trailing whitespace
161 // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
162 // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
163 //
164 // Sets StartOfLineColumn to the intended column in which the text at
165 // Lines[LineIndex] starts (note that the decoration, if present, is not
166 // considered part of the text).
167 void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex,
168 int IndentDelta);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000169
Manuel Klimekde008c02013-05-27 15:23:34 +0000170 // 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;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000174
Manuel Klimekde008c02013-05-27 15:23:34 +0000175 // Contains the text of the lines of the block comment, excluding the leading
176 // /* in the first line and trailing */ in the last line, and excluding all
177 // trailing whitespace between the lines. Note that the decoration (if
178 // present) is also not considered part of the text.
179 SmallVector<StringRef, 16> Lines;
180
181 // LeadingWhitespace[i] is the number of characters regarded as whitespace in
182 // front of Lines[i]. Note that this can include "* " sequences, which we
183 // regard as whitespace when all lines have a "*" prefix.
184 SmallVector<unsigned, 16> LeadingWhitespace;
185
186 // StartOfLineColumn[i] is the target column at which Line[i] should be.
187 // Note that this excludes a leading "* " or "*" in case all lines have
188 // a "*" prefix.
189 SmallVector<unsigned, 16> StartOfLineColumn;
190
191 // The column at which the text of a broken line should start.
192 // Note that an optional decoration would go before that column.
193 // IndentAtLineBreak is a uniform position for all lines in a block comment,
194 // regardless of their relative position.
195 // FIXME: Revisit the decision to do this; the main reason was to support
196 // patterns like
197 // /**************//**
198 // * Comment
199 // We could also support such patterns by special casing the first line
200 // instead.
201 unsigned IndentAtLineBreak;
202
203 // Either "* " if all lines begin with a "*", or empty.
204 StringRef Decoration;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000205};
206
207} // namespace format
208} // namespace clang
209
210#endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H