blob: 4c8755b95b50fd711ac3dbc7adb563fe4d4be939 [file] [log] [blame]
Alexander Kornienkocb45bc12013-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 Kornienkoffcc0102013-06-05 14:09:10 +000020#include "Encoding.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000021#include "TokenAnnotator.h"
22#include "WhitespaceManager.h"
23#include <utility>
24
25namespace clang {
26namespace format {
27
Manuel Klimek9043c742013-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 Kornienkocb45bc12013-04-15 14:28:00 +000034class BreakableToken {
35public:
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000036 /// \brief Contains starting character index and length of split.
Manuel Klimek9043c742013-05-27 15:23:34 +000037 typedef std::pair<StringRef::size_type, unsigned> Split;
38
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000039 virtual ~BreakableToken() {}
Manuel Klimek9043c742013-05-27 15:23:34 +000040
41 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000042 virtual unsigned getLineCount() const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000043
Alexander Kornienkodd7ece52013-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 Klimek9043c742013-05-27 15:23:34 +000046 ///
Alexander Kornienkodd7ece52013-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 Kornienkocb45bc12013-04-15 14:28:00 +000053
Manuel Klimek9043c742013-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 Kornienkocb45bc12013-04-15 14:28:00 +000057 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko9e90b622013-04-17 17:34:05 +000058 unsigned ColumnLimit) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000059
60 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000061 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
62 bool InPPDirective,
63 WhitespaceManager &Whitespaces) = 0;
Manuel Klimek9043c742013-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 Kornienko9e90b622013-04-17 17:34:05 +000070protected:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000071 BreakableToken(const FormatToken &Tok, encoding::Encoding Encoding)
72 : Tok(Tok), Encoding(Encoding) {}
Manuel Klimek9043c742013-05-27 15:23:34 +000073
Alexander Kornienko9e90b622013-04-17 17:34:05 +000074 const FormatToken &Tok;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000075 encoding::Encoding Encoding;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000076};
77
Manuel Klimek9043c742013-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 Kornienkocb45bc12013-04-15 14:28:00 +000082public:
Manuel Klimek9043c742013-05-27 15:23:34 +000083 virtual unsigned getLineCount() const;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000084 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000085 unsigned TailOffset,
86 StringRef::size_type Length) const;
Alexander Kornienko9e90b622013-04-17 17:34:05 +000087
88protected:
Manuel Klimek9043c742013-05-27 15:23:34 +000089 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000090 StringRef Prefix, StringRef Postfix,
91 encoding::Encoding Encoding);
Alexander Kornienko9e90b622013-04-17 17:34:05 +000092
Manuel Klimek9043c742013-05-27 15:23:34 +000093 // The column in which the token starts.
94 unsigned StartColumn;
95 // The prefix a line needs after a break in the token.
96 StringRef Prefix;
97 // The postfix a line needs before introducing a break.
98 StringRef Postfix;
99 // The token text excluding the prefix and postfix.
100 StringRef Line;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000101};
102
Manuel Klimek9043c742013-05-27 15:23:34 +0000103class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000104public:
Manuel Klimek9043c742013-05-27 15:23:34 +0000105 /// \brief Creates a breakable token for a single line string literal.
106 ///
107 /// \p StartColumn specifies the column in which the token will start
108 /// after formatting.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000109 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
110 encoding::Encoding Encoding);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000111
Manuel Klimek9043c742013-05-27 15:23:34 +0000112 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
113 unsigned ColumnLimit) const;
Alexander Kornienko555efc32013-06-11 16:01:49 +0000114 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
115 bool InPPDirective,
116 WhitespaceManager &Whitespaces);
Manuel Klimek9043c742013-05-27 15:23:34 +0000117};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000118
Manuel Klimek9043c742013-05-27 15:23:34 +0000119class BreakableLineComment : public BreakableSingleLineToken {
120public:
121 /// \brief Creates a breakable token for a line comment.
122 ///
123 /// \p StartColumn specifies the column in which the comment will start
124 /// after formatting.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000125 BreakableLineComment(const FormatToken &Token, unsigned StartColumn,
126 encoding::Encoding Encoding);
Manuel Klimek9043c742013-05-27 15:23:34 +0000127
128 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
129 unsigned ColumnLimit) const;
Alexander Kornienko555efc32013-06-11 16:01:49 +0000130 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
131 bool InPPDirective, WhitespaceManager &Whitespaces);
132 virtual void replaceWhitespaceBefore(unsigned LineIndex,
133 unsigned InPPDirective,
134 WhitespaceManager &Whitespaces);
135
136private:
137 // The prefix without an additional space if one was added.
138 StringRef OriginalPrefix;
Manuel Klimek9043c742013-05-27 15:23:34 +0000139};
140
141class BreakableBlockComment : public BreakableToken {
142public:
143 /// \brief Creates a breakable token for a block comment.
144 ///
145 /// \p StartColumn specifies the column in which the comment will start
146 /// after formatting, while \p OriginalStartColumn specifies in which
147 /// column the comment started before formatting.
148 /// If the comment starts a line after formatting, set \p FirstInLine to true.
149 BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token,
150 unsigned StartColumn, unsigned OriginaStartColumn,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000151 bool FirstInLine, encoding::Encoding Encoding);
Manuel Klimek9043c742013-05-27 15:23:34 +0000152
153 virtual unsigned getLineCount() const;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000154 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000155 unsigned TailOffset,
156 StringRef::size_type Length) const;
Manuel Klimek9043c742013-05-27 15:23:34 +0000157 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
158 unsigned ColumnLimit) const;
159 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
160 bool InPPDirective, WhitespaceManager &Whitespaces);
161 virtual void replaceWhitespaceBefore(unsigned LineIndex,
162 unsigned InPPDirective,
163 WhitespaceManager &Whitespaces);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000164
165private:
Manuel Klimek9043c742013-05-27 15:23:34 +0000166 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
167 // so that all whitespace between the lines is accounted to Lines[LineIndex]
168 // as leading whitespace:
169 // - Lines[LineIndex] points to the text after that whitespace
170 // - Lines[LineIndex-1] shrinks by its trailing whitespace
171 // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
172 // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
173 //
174 // Sets StartOfLineColumn to the intended column in which the text at
175 // Lines[LineIndex] starts (note that the decoration, if present, is not
176 // considered part of the text).
177 void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex,
178 int IndentDelta);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000179
Manuel Klimek9043c742013-05-27 15:23:34 +0000180 // Returns the column at which the text in line LineIndex starts, when broken
181 // at TailOffset. Note that the decoration (if present) is not considered part
182 // of the text.
183 unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000184
Manuel Klimek9043c742013-05-27 15:23:34 +0000185 // Contains the text of the lines of the block comment, excluding the leading
186 // /* in the first line and trailing */ in the last line, and excluding all
187 // trailing whitespace between the lines. Note that the decoration (if
188 // present) is also not considered part of the text.
189 SmallVector<StringRef, 16> Lines;
190
191 // LeadingWhitespace[i] is the number of characters regarded as whitespace in
192 // front of Lines[i]. Note that this can include "* " sequences, which we
193 // regard as whitespace when all lines have a "*" prefix.
194 SmallVector<unsigned, 16> LeadingWhitespace;
195
196 // StartOfLineColumn[i] is the target column at which Line[i] should be.
197 // Note that this excludes a leading "* " or "*" in case all lines have
198 // a "*" prefix.
199 SmallVector<unsigned, 16> StartOfLineColumn;
200
201 // The column at which the text of a broken line should start.
202 // Note that an optional decoration would go before that column.
203 // IndentAtLineBreak is a uniform position for all lines in a block comment,
204 // regardless of their relative position.
205 // FIXME: Revisit the decision to do this; the main reason was to support
206 // patterns like
207 // /**************//**
208 // * Comment
209 // We could also support such patterns by special casing the first line
210 // instead.
211 unsigned IndentAtLineBreak;
212
213 // Either "* " if all lines begin with a "*", or empty.
214 StringRef Decoration;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000215};
216
217} // namespace format
218} // namespace clang
219
220#endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H