summaryrefslogtreecommitdiff
path: root/src/libs/cplusplus/TokenCache.cpp
blob: 64fdb57ce395503e1ae5e33441048ff4da4d5cd5 (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
#include "SimpleLexer.h"
#include "TokenCache.h"

#include <QtCore/QDebug>

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);

    const int documentRevision = m_doc->revision();

    if (documentRevision != m_revision) {
        m_tokensByBlock.clear();
        m_revision = documentRevision;
//        qDebug() << "** revision changed to" << documentRevision;
    }

    const int blockNr = block.blockNumber();

    if (m_tokensByBlock.contains(blockNr)) {
//        qDebug()<<"Cache hit on line" << line;
        return m_tokensByBlock.value(blockNr);
    } else {
//        qDebug()<<"Cache miss on line" << line;

        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;
    }
}

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;
}