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
#include "cppcodeformatter.h"
#include <Token.h>
#include <Lexer.h>
#include <texteditor/basetextdocumentlayout.h>
#include <QtCore/QDebug>
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
#include <QtGui/QTextBlock>
namespace CppTools {
namespace Internal {
class CppCodeFormatterData: public TextEditor::CodeFormatterData
{
public:
CppCodeFormatterData(int blockRevision,
const QStack<CodeFormatter::State> &beginState,
const QStack<CodeFormatter::State> &endState,
int indentDepth)
: CodeFormatterData(blockRevision)
, m_beginState(beginState)
, m_endState(endState)
, m_indentDepth(indentDepth)
{}
QStack<CodeFormatter::State> m_beginState;
QStack<CodeFormatter::State> m_endState;
int m_indentDepth;
};
}
}
using namespace CPlusPlus;
using namespace CppTools;
using namespace TextEditor;
using namespace CppTools::Internal;
CodeFormatter::CodeFormatter()
: m_indentDepth(0)
, m_tabSize(4)
{
}
CodeFormatter::~CodeFormatter()
{
}
void CodeFormatter::setTabSize(int tabSize)
{
m_tabSize = tabSize;
}
void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
{
restoreBlockState(block.previous());
bool endedJoined = false;
const int lexerState = tokenizeBlock(block, &endedJoined);
m_tokenIndex = 0;
m_newStates.clear();
if (tokenAt(0).kind() == T_POUND) {
enter(cpp_macro_start);
m_tokenIndex = 1;
}
for (; m_tokenIndex < m_tokens.size(); ) {
m_currentToken = tokenAt(m_tokenIndex);
const int kind = m_currentToken.kind();
switch (m_currentState.top().type) {
case topmost_intro:
if (tryDeclaration())
break;
switch (kind) {
case T_NAMESPACE: enter(namespace_start); break;
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break;
case namespace_start:
switch (kind) {
case T_LBRACE: enter(namespace_open); break;
case T_RBRACE: leave(); break;
} break;
case namespace_open:
if (tryDeclaration())
break;
switch (kind) {
case T_NAMESPACE: enter(namespace_start); break;
case T_RBRACE: leave(); continue; // always nested in namespace_start
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break;
case class_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
case T_LBRACE: enter(class_open); break;
} break;
case class_open:
if (tryDeclaration())
break;
switch (kind) {
case T_RBRACE: leave(); continue; // always nested in class_start
case T_STRUCT:
case T_UNION:
case T_CLASS: enter(class_start); break;
case T_ENUM: enter(enum_start); break;
case T_USING: enter(using_start); break;
} break;
case enum_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
case T_LBRACE: enter(enum_open); break;
} break;
case enum_open:
switch (kind) {
case T_RBRACE: leave(); continue; // always nested in enum_start
case T_LBRACE: enter(brace_list_open); break;
} break;
case brace_list_open:
switch (kind) {
case T_RBRACE: leave(); break;
case T_LBRACE: enter(brace_list_open); break;
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
} break;
case using_start:
switch (kind) {
case T_SEMICOLON: leave(); break;
} break;
case template_start:
switch (kind) {
case T_LESS: turnInto(template_param); break;
} break;
case template_param:
switch (kind) {
case T_LESS: enter(template_param); break;
case T_GREATER: leave(); break;
} break;
case operator_declaration:
switch (kind) {
case T_LPAREN: break;
default: leave(); break;
} break;
case declaration_start:
if (tryExpression(true))
break;
switch (kind) {
case T_RBRACE:
case T_SEMICOLON: leave(true); break;
case T_EQUAL: enter(initializer); break;
case T_LBRACE: enter(defun_open); break;
case T_COLON: enter(member_init_open); break;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
case T_OPERATOR: enter(operator_declaration); break;
} break;
case initializer:
switch (kind) {
case T_LBRACE: enter(brace_list_open); break;
default: turnInto(expression); continue;
} break;
case expression:
if (tryExpression())
break;
switch (kind) {
case T_SEMICOLON: leave(); continue;
case T_LBRACE:
case T_COLON:
if (m_currentState.at(m_currentState.size() - 2).type == declaration_start) {
// oops, the expression was a function declaration argument list, hand lbrace/colon to declaration_start
leave();
continue;
}
break;
} break;
case arglist_open:
if (tryExpression())
break;
switch (kind) {
case T_RPAREN: leave(); break;
Loading
Loading full blame...