This repository has been archived by the owner on May 30, 2023. It is now read-only.
/
replcompletable.cpp
86 lines (73 loc) · 2.65 KB
/
replcompletable.cpp
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
#include "replcompletable.h"
// public:
REPLCompletable::REPLCompletable(QObject *parent)
: QObject(parent),
mCompletionsInitialised(false)
{ }
REPLCompletable::~REPLCompletable()
{ }
bool REPLCompletable::_isCompletable()
{
return true;
}
QStringList REPLCompletable::_getCompletions(const QString &prefix)
{
// First time this method is invoked, initialise the completions (lazy init)
if (!mCompletionsInitialised) {
initCompletions();
mCompletionsInitialised = true;
}
// If no prefix provided?
if (prefix.isEmpty()) {
// Return all the possible completions
return REPLCompletable::getCompletionsIndex()->values(
this->metaObject()->className());
}
// make a key to store the (new) completions list
QString cacheKey = QString("%1-%2").arg(this->metaObject()->className()).arg(prefix);
// If a list of completion withi this key is not already in the cache
if (!getCompletionsCache()->contains(cacheKey)) {
// Loop over the completions and pick the one that match the given prefix
QStringList allCompletions = REPLCompletable::getCompletionsIndex()->values(
this->metaObject()->className());
QStringList *matchingPrefixCompletions = new QStringList();
QStringList::iterator i;
for (i = allCompletions.begin(); i != allCompletions.end(); ++i) {
if (((QString) *i).startsWith(prefix)) {
matchingPrefixCompletions->append((QString) *i);
}
}
// Store the result in the cache
getCompletionsCache()->insert(cacheKey, matchingPrefixCompletions);
}
return *(getCompletionsCache()->object(cacheKey));
}
// protected:
void REPLCompletable::addCompletion(const char *completion)
{
addCompletion(QString(completion));
}
void REPLCompletable::addCompletion(QString completion)
{
// Accept a completion only if it's unique per MetaObject ClassName
if (!REPLCompletable::getCompletionsIndex()->contains(this->metaObject()->className(), completion)) {
REPLCompletable::getCompletionsIndex()->insert(this->metaObject()->className(), completion);
}
}
// private:
QMultiHash<const char *, QString> *REPLCompletable::getCompletionsIndex()
{
static QMultiHash<const char *, QString> *compIndex = NULL;
if (!compIndex) {
compIndex = new QMultiHash<const char *, QString>();
}
return compIndex;
}
QCache<QString, QStringList> *REPLCompletable::getCompletionsCache()
{
static QCache<QString, QStringList> *compCache = NULL;
if (!compCache) {
compCache = new QCache<QString, QStringList>();
}
return compCache;
}