forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompletionThread.h
205 lines (183 loc) · 6.03 KB
/
CompletionThread.h
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* This file is part of RTags (http://rtags.net).
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#ifndef CompletionThread_h
#define CompletionThread_h
#include <clang-c/Index.h>
#include <condition_variable>
#include <memory>
#include <mutex>
#include "Location.h"
#include "rct/Connection.h"
#include "rct/EmbeddedLinkedList.h"
#include "rct/Flags.h"
#include "rct/LinkedList.h"
#include "rct/Map.h"
#include "rct/Thread.h"
#include "Source.h"
#include "RTags.h"
class CompletionThread : public Thread
{
public:
CompletionThread(int cacheSize);
~CompletionThread();
virtual void run() override;
enum Flag {
None = 0x00,
Elisp = 0x01,
XML = 0x02,
JSON = 0x04,
IncludeMacros = 0x08,
WarmUp = 0x10,
NoWait = 0x20
};
bool isCached(uint32_t fileId, const std::shared_ptr<Project> &project) const;
void completeAt(Source &&source, Location location, Flags<Flag> flags,
String &&unsaved, const String &prefix,
const std::shared_ptr<Connection> &conn);
void prepare(Source &&source, String &&unsaved);
Source findSource(const Set<uint32_t> &deps) const;
void stop();
String dump();
private:
struct Request;
void process(Request *request);
Set<uint32_t> mWatched;
bool mShutdown;
const size_t mCacheSize;
struct Request {
~Request()
{
if (conn)
conn->finish();
}
String toString() const;
Source source;
Location location;
Flags<Flag> flags;
String unsaved, prefix;
std::shared_ptr<Connection> conn;
};
LinkedList<Request*> mPending;
struct Dump {
bool done;
std::mutex mutex;
std::condition_variable cond;
String string;
} *mDump;
struct Completions {
Completions(Location loc) : location(loc), next(0), prev(0) {}
struct Candidate {
String completion, signature, annotation, parent, briefComment;
int priority = 0;
#ifdef RTAGS_COMPLETION_TOKENS_ENABLED
int distance = 0;
#endif
CXCursorKind cursorKind;
struct Chunk {
Chunk()
: kind(CXCompletionChunk_Optional)
{}
Chunk(String &&t, CXCompletionChunkKind k)
: text(std::forward<String>(t)), kind(k)
{}
String text;
CXCompletionChunkKind kind;
};
List<Chunk> chunks;
enum Flag {
None = 0x0,
IncludeChunks = 0x1
};
Value toValue(unsigned int flags) const;
};
List<Candidate> candidates;
const Location location;
Flags<Flag> flags;
Completions *next, *prev;
};
void printCompletions(const List<const Completions::Candidate *> &completions, Request *request);
static bool compareCompletionCandidates(const Completions::Candidate *l,
const Completions::Candidate *r);
struct SourceFile {
SourceFile()
: lastModified(0), parseTime(0), reparseTime(0), codeCompleteTime(0), completions(0), next(0), prev(0)
{}
std::shared_ptr<RTags::TranslationUnit> translationUnit;
String unsaved;
uint64_t lastModified;
uint64_t parseTime, reparseTime, codeCompleteTime; // ms
size_t completions;
Source source;
SourceFile *next, *prev;
};
#ifdef RTAGS_COMPLETION_TOKENS_ENABLED
struct Token
{
Token(const char *bytes = 0, int size = 0)
: data(bytes), length(size)
{}
inline bool operator==(const Token &other) const
{
return length == other.length && !strncmp(data, other.data, length);
}
inline bool operator<(const Token &other) const
{
if (!data)
return !other.data ? 0 : -1;
if (!other.data)
return 1;
const int minLength = std::min(length, other.length);
int ret = memcmp(data, other.data, minLength);
if (!ret) {
if (length < other.length) {
ret = -1;
} else if (other.length < length) {
ret = 1;
}
}
return ret;
}
const char *data;
int length;
static inline Map<Token, int> tokenize(const char *data, int size)
{
Map<Token, int> tokens;
int tokenEnd = -1;
for (int i=size - 1; i>=0; --i) {
if (RTags::isSymbol(data[i])) {
if (tokenEnd == -1)
tokenEnd = i;
} else if (tokenEnd != -1) {
addToken(data, i + 1, tokenEnd - i, tokens);
tokenEnd = -1;
}
}
if (tokenEnd != -1)
addToken(data, 0, tokenEnd + 1, tokens);
return tokens;
}
private:
static inline void addToken(const char *data, int pos, int len, Map<Token, int> &tokens)
{
int &val = tokens[Token(data + pos, len)];
if (!val)
val = pos;
}
};
#endif
Hash<uint32_t, SourceFile*> mCacheMap;
EmbeddedLinkedList<SourceFile*> mCacheList;
mutable std::mutex mMutex;
std::condition_variable mCondition;
};
RCT_FLAGS(CompletionThread::Flag);
#endif