blob: 157bff4c42fffb6f4cd476d25a6184920052178c [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:
Manuel Klimekde008c02013-05-27 15:23:34 +000036 // Contains starting character index and length of split.
37 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
44 /// \brief Returns the rest of the length of the line at \p LineIndex,
45 /// when broken at \p TailOffset.
46 ///
47 /// Note that previous breaks are not taken into account. \p TailOffset
48 /// is always specified from the start of the (original) line.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000049 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko919398b2013-04-17 17:34:05 +000050 unsigned TailOffset) const = 0;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000051
Manuel Klimekde008c02013-05-27 15:23:34 +000052 /// \brief Returns a range (offset, length) at which to break the line at
53 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
54 /// violate \p ColumnLimit.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000055 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko919398b2013-04-17 17:34:05 +000056 unsigned ColumnLimit) const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000057
58 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000059 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
60 bool InPPDirective,
61 WhitespaceManager &Whitespaces) = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000062
63 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
64 virtual void replaceWhitespaceBefore(unsigned LineIndex,
65 unsigned InPPDirective,
66 WhitespaceManager &Whitespaces) {}
67
Alexander Kornienko919398b2013-04-17 17:34:05 +000068protected:
Alexander Kornienko00895102013-06-05 14:09:10 +000069 BreakableToken(const FormatToken &Tok, encoding::Encoding Encoding)
70 : Tok(Tok), Encoding(Encoding) {}
Manuel Klimekde008c02013-05-27 15:23:34 +000071
Alexander Kornienko919398b2013-04-17 17:34:05 +000072 const FormatToken &Tok;
Alexander Kornienko00895102013-06-05 14:09:10 +000073 encoding::Encoding Encoding;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000074};
75
Manuel Klimekde008c02013-05-27 15:23:34 +000076/// \brief Base class for single line tokens that can be broken.
77///
78/// \c getSplit() needs to be implemented by child classes.
79class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +000080public:
Manuel Klimekde008c02013-05-27 15:23:34 +000081 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000082 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +000083 unsigned TailOffset) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +000084 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
85 bool InPPDirective, WhitespaceManager &Whitespaces);
86
87protected:
Manuel Klimekde008c02013-05-27 15:23:34 +000088 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +000089 StringRef Prefix, StringRef Postfix,
90 encoding::Encoding Encoding);
Alexander Kornienko919398b2013-04-17 17:34:05 +000091
Manuel Klimekde008c02013-05-27 15:23:34 +000092 // The column in which the token starts.
93 unsigned StartColumn;
94 // The prefix a line needs after a break in the token.
95 StringRef Prefix;
96 // The postfix a line needs before introducing a break.
97 StringRef Postfix;
98 // The token text excluding the prefix and postfix.
99 StringRef Line;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000100};
101
Manuel Klimekde008c02013-05-27 15:23:34 +0000102class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000103public:
Manuel Klimekde008c02013-05-27 15:23:34 +0000104 /// \brief Creates a breakable token for a single line string literal.
105 ///
106 /// \p StartColumn specifies the column in which the token will start
107 /// after formatting.
Alexander Kornienko00895102013-06-05 14:09:10 +0000108 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
109 encoding::Encoding Encoding);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000110
Manuel Klimekde008c02013-05-27 15:23:34 +0000111 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
112 unsigned ColumnLimit) const;
113};
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000114
Manuel Klimekde008c02013-05-27 15:23:34 +0000115class BreakableLineComment : public BreakableSingleLineToken {
116public:
117 /// \brief Creates a breakable token for a line comment.
118 ///
119 /// \p StartColumn specifies the column in which the comment will start
120 /// after formatting.
Alexander Kornienko00895102013-06-05 14:09:10 +0000121 BreakableLineComment(const FormatToken &Token, unsigned StartColumn,
122 encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000123
124 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
125 unsigned ColumnLimit) const;
126};
127
128class BreakableBlockComment : public BreakableToken {
129public:
130 /// \brief Creates a breakable token for a block comment.
131 ///
132 /// \p StartColumn specifies the column in which the comment will start
133 /// after formatting, while \p OriginalStartColumn specifies in which
134 /// column the comment started before formatting.
135 /// If the comment starts a line after formatting, set \p FirstInLine to true.
136 BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token,
137 unsigned StartColumn, unsigned OriginaStartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +0000138 bool FirstInLine, encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000139
140 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000141 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +0000142 unsigned TailOffset) const;
143 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
144 unsigned ColumnLimit) const;
145 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
146 bool InPPDirective, WhitespaceManager &Whitespaces);
147 virtual void replaceWhitespaceBefore(unsigned LineIndex,
148 unsigned InPPDirective,
149 WhitespaceManager &Whitespaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000150
151private:
Manuel Klimekde008c02013-05-27 15:23:34 +0000152 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
153 // so that all whitespace between the lines is accounted to Lines[LineIndex]
154 // as leading whitespace:
155 // - Lines[LineIndex] points to the text after that whitespace
156 // - Lines[LineIndex-1] shrinks by its trailing whitespace
157 // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
158 // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
159 //
160 // Sets StartOfLineColumn to the intended column in which the text at
161 // Lines[LineIndex] starts (note that the decoration, if present, is not
162 // considered part of the text).
163 void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex,
164 int IndentDelta);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000165
Manuel Klimekde008c02013-05-27 15:23:34 +0000166 // Returns the column at which the text in line LineIndex starts, when broken
167 // at TailOffset. Note that the decoration (if present) is not considered part
168 // of the text.
169 unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000170
Manuel Klimekde008c02013-05-27 15:23:34 +0000171 // Contains the text of the lines of the block comment, excluding the leading
172 // /* in the first line and trailing */ in the last line, and excluding all
173 // trailing whitespace between the lines. Note that the decoration (if
174 // present) is also not considered part of the text.
175 SmallVector<StringRef, 16> Lines;
176
177 // LeadingWhitespace[i] is the number of characters regarded as whitespace in
178 // front of Lines[i]. Note that this can include "* " sequences, which we
179 // regard as whitespace when all lines have a "*" prefix.
180 SmallVector<unsigned, 16> LeadingWhitespace;
181
182 // StartOfLineColumn[i] is the target column at which Line[i] should be.
183 // Note that this excludes a leading "* " or "*" in case all lines have
184 // a "*" prefix.
185 SmallVector<unsigned, 16> StartOfLineColumn;
186
187 // The column at which the text of a broken line should start.
188 // Note that an optional decoration would go before that column.
189 // IndentAtLineBreak is a uniform position for all lines in a block comment,
190 // regardless of their relative position.
191 // FIXME: Revisit the decision to do this; the main reason was to support
192 // patterns like
193 // /**************//**
194 // * Comment
195 // We could also support such patterns by special casing the first line
196 // instead.
197 unsigned IndentAtLineBreak;
198
199 // Either "* " if all lines begin with a "*", or empty.
200 StringRef Decoration;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000201};
202
203} // namespace format
204} // namespace clang
205
206#endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H