blob: 03904c2a4680379546b9be5ed6ea669a3ad4ba47 [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
20#include "TokenAnnotator.h"
21#include "WhitespaceManager.h"
22#include <utility>
23
24namespace clang {
25namespace format {
26
Manuel Klimekde008c02013-05-27 15:23:34 +000027struct FormatStyle;
28
29/// \brief Base class for strategies on how to break tokens.
30///
31/// FIXME: The interface seems set in stone, so we might want to just pull the
32/// strategy into the class, instead of controlling it from the outside.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000033class BreakableToken {
34public:
Manuel Klimekde008c02013-05-27 15:23:34 +000035 // Contains starting character index and length of split.
36 typedef std::pair<StringRef::size_type, unsigned> Split;
37
Alexander Kornienko70ce7882013-04-15 14:28:00 +000038 virtual ~BreakableToken() {}
Manuel Klimekde008c02013-05-27 15:23:34 +000039
40 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000041 virtual unsigned getLineCount() const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000042
43 /// \brief Returns the rest of the length of the line at \p LineIndex,
44 /// when broken at \p TailOffset.
45 ///
46 /// Note that previous breaks are not taken into account. \p TailOffset
47 /// is always specified from the start of the (original) line.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000048 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko919398b2013-04-17 17:34:05 +000049 unsigned TailOffset) const = 0;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000050
Manuel Klimekde008c02013-05-27 15:23:34 +000051 /// \brief Returns a range (offset, length) at which to break the line at
52 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
53 /// violate \p ColumnLimit.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000054 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko919398b2013-04-17 17:34:05 +000055 unsigned ColumnLimit) const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000056
57 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000058 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
59 bool InPPDirective,
60 WhitespaceManager &Whitespaces) = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000061
62 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
63 virtual void replaceWhitespaceBefore(unsigned LineIndex,
64 unsigned InPPDirective,
65 WhitespaceManager &Whitespaces) {}
66
Alexander Kornienko919398b2013-04-17 17:34:05 +000067protected:
Manuel Klimekde008c02013-05-27 15:23:34 +000068 BreakableToken(const FormatToken &Tok) : Tok(Tok) {}
69
Alexander Kornienko919398b2013-04-17 17:34:05 +000070 const FormatToken &Tok;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000071};
72
Manuel Klimekde008c02013-05-27 15:23:34 +000073/// \brief Base class for single line tokens that can be broken.
74///
75/// \c getSplit() needs to be implemented by child classes.
76class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +000077public:
Manuel Klimekde008c02013-05-27 15:23:34 +000078 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000079 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +000080 unsigned TailOffset) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +000081 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
82 bool InPPDirective, WhitespaceManager &Whitespaces);
83
84protected:
Manuel Klimekde008c02013-05-27 15:23:34 +000085 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
86 StringRef Prefix, StringRef Postfix);
Alexander Kornienko919398b2013-04-17 17:34:05 +000087
Manuel Klimekde008c02013-05-27 15:23:34 +000088 // The column in which the token starts.
89 unsigned StartColumn;
90 // The prefix a line needs after a break in the token.
91 StringRef Prefix;
92 // The postfix a line needs before introducing a break.
93 StringRef Postfix;
94 // The token text excluding the prefix and postfix.
95 StringRef Line;
Alexander Kornienko919398b2013-04-17 17:34:05 +000096};
97
Manuel Klimekde008c02013-05-27 15:23:34 +000098class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +000099public:
Manuel Klimekde008c02013-05-27 15:23:34 +0000100 /// \brief Creates a breakable token for a single line string literal.
101 ///
102 /// \p StartColumn specifies the column in which the token will start
103 /// after formatting.
104 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000105
Manuel Klimekde008c02013-05-27 15:23:34 +0000106 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
107 unsigned ColumnLimit) const;
108};
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000109
Manuel Klimekde008c02013-05-27 15:23:34 +0000110class BreakableLineComment : public BreakableSingleLineToken {
111public:
112 /// \brief Creates a breakable token for a line comment.
113 ///
114 /// \p StartColumn specifies the column in which the comment will start
115 /// after formatting.
116 BreakableLineComment(const FormatToken &Token, unsigned StartColumn);
117
118 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
119 unsigned ColumnLimit) const;
120};
121
122class BreakableBlockComment : public BreakableToken {
123public:
124 /// \brief Creates a breakable token for a block comment.
125 ///
126 /// \p StartColumn specifies the column in which the comment will start
127 /// after formatting, while \p OriginalStartColumn specifies in which
128 /// column the comment started before formatting.
129 /// If the comment starts a line after formatting, set \p FirstInLine to true.
130 BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token,
131 unsigned StartColumn, unsigned OriginaStartColumn,
132 bool FirstInLine);
133
134 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000135 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +0000136 unsigned TailOffset) const;
137 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
138 unsigned ColumnLimit) const;
139 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
140 bool InPPDirective, WhitespaceManager &Whitespaces);
141 virtual void replaceWhitespaceBefore(unsigned LineIndex,
142 unsigned InPPDirective,
143 WhitespaceManager &Whitespaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000144
145private:
Manuel Klimekde008c02013-05-27 15:23:34 +0000146 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
147 // so that all whitespace between the lines is accounted to Lines[LineIndex]
148 // as leading whitespace:
149 // - Lines[LineIndex] points to the text after that whitespace
150 // - Lines[LineIndex-1] shrinks by its trailing whitespace
151 // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
152 // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
153 //
154 // Sets StartOfLineColumn to the intended column in which the text at
155 // Lines[LineIndex] starts (note that the decoration, if present, is not
156 // considered part of the text).
157 void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex,
158 int IndentDelta);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000159
Manuel Klimekde008c02013-05-27 15:23:34 +0000160 // Returns the column at which the text in line LineIndex starts, when broken
161 // at TailOffset. Note that the decoration (if present) is not considered part
162 // of the text.
163 unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000164
Manuel Klimekde008c02013-05-27 15:23:34 +0000165 // Contains the text of the lines of the block comment, excluding the leading
166 // /* in the first line and trailing */ in the last line, and excluding all
167 // trailing whitespace between the lines. Note that the decoration (if
168 // present) is also not considered part of the text.
169 SmallVector<StringRef, 16> Lines;
170
171 // LeadingWhitespace[i] is the number of characters regarded as whitespace in
172 // front of Lines[i]. Note that this can include "* " sequences, which we
173 // regard as whitespace when all lines have a "*" prefix.
174 SmallVector<unsigned, 16> LeadingWhitespace;
175
176 // StartOfLineColumn[i] is the target column at which Line[i] should be.
177 // Note that this excludes a leading "* " or "*" in case all lines have
178 // a "*" prefix.
179 SmallVector<unsigned, 16> StartOfLineColumn;
180
181 // The column at which the text of a broken line should start.
182 // Note that an optional decoration would go before that column.
183 // IndentAtLineBreak is a uniform position for all lines in a block comment,
184 // regardless of their relative position.
185 // FIXME: Revisit the decision to do this; the main reason was to support
186 // patterns like
187 // /**************//**
188 // * Comment
189 // We could also support such patterns by special casing the first line
190 // instead.
191 unsigned IndentAtLineBreak;
192
193 // Either "* " if all lines begin with a "*", or empty.
194 StringRef Decoration;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000195};
196
197} // namespace format
198} // namespace clang
199
200#endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H