blob: 0be110100df8d82debfb11050ce5d6d9f1276f4a (
plain)
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
|
#include "SimpleLexer.h"
#include "TokenCache.h"
using namespace CPlusPlus;
TokenCache::TokenCache()
: m_doc(0)
, m_revision(-1)
{}
void TokenCache::setDocument(QTextDocument *doc)
{
m_doc = doc;
m_revision = -1;
}
QList<SimpleToken> TokenCache::tokensForBlock(const QTextBlock &block) const
{
Q_ASSERT(m_doc);
Q_ASSERT(m_doc == block.document());
const int documentRevision = m_doc->revision();
if (documentRevision != m_revision) {
m_tokensByBlock.clear();
m_revision = documentRevision;
}
const int blockNr = block.blockNumber();
if (m_tokensByBlock.contains(blockNr)) {
return m_tokensByBlock.value(blockNr);
} else {
SimpleLexer tokenize;
tokenize.setObjCEnabled(true);
tokenize.setQtMocRunEnabled(true);
tokenize.setSkipComments(false);
const int prevState = previousBlockState(block);
QList<SimpleToken> tokens = tokenize(block.text(), prevState);
m_tokensByBlock.insert(blockNr, tokens);
return tokens;
}
}
SimpleToken TokenCache::tokenUnderCursor(const QTextCursor &cursor) const
{
const QTextBlock block = cursor.block();
const QList<SimpleToken> tokens = tokensForBlock(block);
const int column = cursor.position() - block.position();
for (int index = tokens.size() - 1; index >= 0; --index) {
const SimpleToken &tk = tokens.at(index);
if (tk.position() < column)
return tk;
}
return SimpleToken();
}
QString TokenCache::text(const QTextBlock &block, int tokenIndex) const
{
SimpleToken tk = tokensForBlock(block).at(tokenIndex);
return block.text().mid(tk.position(), tk.length());
}
int TokenCache::previousBlockState(const QTextBlock &block)
{
const QTextBlock prevBlock = block.previous();
if (prevBlock.isValid()) {
int state = prevBlock.userState();
if (state != -1)
return state;
}
return 0;
}
|