Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef CPPCODEFORMATTER_H
#define CPPCODEFORMATTER_H
#include "cpptools_global.h"
#include <cplusplus/SimpleLexer.h>
#include <Token.h>
#include <QtCore/QChar>
#include <QtCore/QStack>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtCore/QPointer>
QT_BEGIN_NAMESPACE
class QTextDocument;
class QTextBlock;
QT_END_NAMESPACE
namespace CppTools {
namespace Internal {
class CppCodeFormatterData;
}
class CPPTOOLS_EXPORT CodeFormatter
{
public:
CodeFormatter();
virtual ~CodeFormatter();
void setDocument(QTextDocument *document);
int indentFor(const QTextBlock &block);
protected:
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const = 0;
virtual void adjustIndent(const QList<CPlusPlus::Token> &tokens, int lexerState, int *indentDepth) const = 0;
protected:
enum StateType {
invalid = 0,
topmost_intro, // The first line in a "topmost" definition.
multiline_comment_start, // Inside the first line of a multi-line C style block comment.
multiline_comment_cont, // Inside the following lines of a multi-line C style block comment.
cpp_macro_start, // After the '#' token
cpp_macro, // The start of a C preprocessor macro definition.
cpp_macro_cont, // Subsequent lines of a multi-line C preprocessor macro definition.
cpp_macro_conditional, // Special marker used for separating saved from current state when dealing with #ifdef
qt_like_macro, // after an identifier starting with Q_ or QT_ at the beginning of the line
defun_open, // Brace that opens a top-level function definition.
using_start, // right after the "using" token
class_start, // after the 'class' token
class_open, // Brace that opens a class definition.
member_init_open, // After ':' that starts a member initialization list.
enum_start, // After 'enum'
brace_list_open, // Open brace of an enum or static array list.
namespace_start, // after the namespace token, before the opening brace.
namespace_open, // Brace that opens a C++ namespace block.
declaration_start, // shifted a token which could start a declaration.
operator_declaration, // after 'operator' in declaration_start
template_start, // after the 'template' token
template_param, // after the '<' in a template_start
if_statement, // After 'if'
maybe_else, // after the first substatement in an if
else_clause, // The else line of an if-else construct.
for_statement, // After the 'for' token
for_statement_paren_open, // While inside the (...)
for_statement_init, // The initializer part of the for statement
for_statement_condition, // The condition part of the for statement
for_statement_expression, // The expression part of the for statement
switch_statement, // After 'switch' token
case_start, // after a 'case' or 'default' token
case_cont, // after the colon in a case/default
statement_with_condition, // A statement that takes a condition after the start token.
do_statement, // After 'do' token
return_statement, // After 'return'
block_open, // Statement block open brace.
substatement, // The first line after a conditional or loop construct.
substatement_open, // The brace that opens a substatement block.
arglist_open, // after the lparen. TODO: check if this is enough.
stream_op, // Lines continuing a stream operator (C++ only).
ternary_op, // The ? : operator
condition_open, // Start of a condition in 'if', 'while', entered after opening paren
condition_paren_open, // After an lparen in a condition
assign_open, // after an assignment token
expression, // after a '=' in a declaration_start once we're sure it's not '= {'
initializer, // after a '=' in a declaration start
};
struct State {
State()
: savedIndentDepth(0)
, type(0)
{}
State(quint8 ty, quint16 savedDepth)
: savedIndentDepth(savedDepth)
, type(ty)
{}
quint16 savedIndentDepth;
quint8 type;
bool operator==(const State &other) const {
return type == other.type
&& savedIndentDepth == other.savedIndentDepth;
}
};
State state(int belowTop = 0) const;
int tokenIndex() const;
int tokenIndexFromEnd() const;
const CPlusPlus::Token ¤tToken() const;
const CPlusPlus::Token &tokenAt(int idx) const;
bool isBracelessState(int type) const;
void invalidateCache();
private:
void requireStatesUntil(const QTextBlock &block);
void recalculateStateAfter(const QTextBlock &block);
void storeBlockState(const QTextBlock &block);
void restoreBlockState(const QTextBlock &block);
QStringRef currentTokenText() const;
int tokenizeBlock(const QTextBlock &block, bool *endedJoined = 0);
void turnInto(int newState);
bool tryExpression(bool alsoExpression = false);
bool tryDeclaration();
bool tryStatement();
void enter(int newState);
void leave(bool statementDone = false);
void correctIndentation(const QTextBlock &block);
void dump();
private:
static QStack<State> initialState();
QPointer<QTextDocument> m_document;
QStack<State> m_beginState;
QStack<State> m_currentState;
QList<CPlusPlus::Token> m_tokens;
QString m_currentLine;
CPlusPlus::Token m_currentToken;
int m_tokenIndex;
// should store indent level and padding instead
int m_indentDepth;
friend class Internal::CppCodeFormatterData;
};
class CPPTOOLS_EXPORT QtStyleCodeFormatter : public CodeFormatter
{
public:
QtStyleCodeFormatter();
void setIndentSize(int size);
enum CompoundStyle {
QtStyle, // don't indent braces, add indent for contained statements
WhitesmithsStyle, // add indent for braces, don't for the contained statements
GnuStyle // add indent for braces and again for contained statements
};
void setCompoundStyle(CompoundStyle style);
protected:
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const;
virtual void adjustIndent(const QList<CPlusPlus::Token> &tokens, int lexerState, int *indentDepth) const;
private:
int m_indentSize;
CompoundStyle m_style;
};
} // namespace CppTools
#endif // CPPCODEFORMATTER_H