-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathCodeCompletion.cpp
418 lines (344 loc) · 12.9 KB
/
CodeCompletion.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* \file
* \author Guillaume Papin <[email protected]>
*
* \brief Completion plugin implementation.
*
* This file is distributed under the GNU General Public License. See
* COPYING for details.
*
*/
#include "CodeCompletion.h"
#include "ClangString.h"
#include "util/arraysize.h"
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
class CompletionChunk {
public:
explicit CompletionChunk(CXCompletionString completionString)
: completionString_(completionString)
, numChunks_(clang_getNumCompletionChunks(completionString_))
, chunkIdx_(0) {
}
CXCompletionChunkKind kind() const {
return clang_getCompletionChunkKind(completionString_, chunkIdx_);
}
std::string escapedContent() const {
return ClangString(text(), ClangString::AddQuotes).asString();
}
std::string escapedUnquotedContent() const {
return ClangString(text(), ClangString::Escape).asString();
}
CompletionChunk optionalChunks() const {
CXCompletionString completionString =
clang_getCompletionChunkCompletionString(completionString_, chunkIdx_);
return CompletionChunk(completionString);
}
bool hasNext() const {
return chunkIdx_ < numChunks_;
}
void next() {
if (!hasNext()) {
throw std::out_of_range("out of range completion chunk");
}
chunkIdx_++;
}
private:
CXString text() const {
return clang_getCompletionChunkText(completionString_, chunkIdx_);
}
private:
CXCompletionString completionString_;
unsigned int numChunks_;
unsigned chunkIdx_;
};
class Candidate {
public:
explicit Candidate(const CXCompletionResult &completionResult)
: completionResult_(completionResult), priority_(-1), typedText_() {
}
Candidate() : completionResult_(), priority_(-1), typedText_() {
}
int priority() const {
if (priority_ == -1) {
priority_ =
clang_getCompletionPriority(completionResult_.CompletionString);
}
return priority_;
}
const std::string &getTypedText() const {
if (typedText_.empty()) {
for (CompletionChunk chunk = firstChunk(); chunk.hasNext();
chunk.next()) {
if (chunk.kind() == CXCompletionChunk_TypedText) {
typedText_ = chunk.escapedContent();
break;
}
}
}
return typedText_;
}
/// \brief Generate a short documentation for the candidate. Contains at least
/// the prototype of the function and if available the brief comment.
const std::string &briefDoc() const {
if (briefDoc_.empty()) {
std::ostringstream os;
os << '"';
formatCompletionChunks(firstChunk(), os);
#if defined(CINDEX_VERSION_MAJOR) && defined(CINDEX_VERSION_MINOR) && \
(CINDEX_VERSION_MAJOR > 0 || CINDEX_VERSION_MINOR >= 6)
CXString brief =
clang_getCompletionBriefComment(completionResult_.CompletionString);
ClangString briefStr(brief, ClangString::Escape);
if (!briefStr.isNull())
os << "\n\n" << briefStr.asString();
#endif
os << '"';
briefDoc_ = os.str();
}
return briefDoc_;
}
void formatCompletionChunks(const CompletionChunk &start,
std::ostream &os) const {
for (CompletionChunk chunk(start); chunk.hasNext(); chunk.next()) {
switch (chunk.kind()) {
case CXCompletionChunk_ResultType:
os << chunk.escapedUnquotedContent() << " ";
break;
case CXCompletionChunk_TypedText:
case CXCompletionChunk_Placeholder:
case CXCompletionChunk_Text:
case CXCompletionChunk_Informative:
case CXCompletionChunk_CurrentParameter:
os << chunk.escapedUnquotedContent();
break;
case CXCompletionChunk_LeftParen: os << '('; break;
case CXCompletionChunk_RightParen: os << ')'; break;
case CXCompletionChunk_LeftBracket: os << '['; break;
case CXCompletionChunk_RightBracket: os << ']'; break;
case CXCompletionChunk_LeftBrace: os << '{'; break;
case CXCompletionChunk_RightBrace: os << '}'; break;
case CXCompletionChunk_LeftAngle: os << '<'; break;
case CXCompletionChunk_RightAngle: os << '>'; break;
case CXCompletionChunk_Comma: os << ", "; break;
case CXCompletionChunk_Colon: os << ':'; break;
case CXCompletionChunk_SemiColon: os << ';'; break;
case CXCompletionChunk_Equal: os << '='; break;
case CXCompletionChunk_HorizontalSpace: os << ' '; break;
case CXCompletionChunk_VerticalSpace: os << '\n'; break;
case CXCompletionChunk_Optional:
os << '[';
formatCompletionChunks(chunk.optionalChunks(), os);
os << ']';
break;
default:
break;
}
}
}
CompletionChunk firstChunk() const {
return CompletionChunk(completionResult_.CompletionString);
}
void setCompletionResult(const CXCompletionResult &completionResult) {
completionResult_ = completionResult;
priority_ = -1;
}
bool operator<(const Candidate &rhs) const {
const std::string &a = getTypedText();
const std::string &b = rhs.getTypedText();
for (std::string::size_type i = 0, e = std::min(a.length(), b.length());
i != e;
++i) {
const char low_a = std::tolower(a[i]);
const char low_b = std::tolower(b[i]);
// compare lower first
if (low_a != low_b)
return low_a < low_b;
}
if (a.length() == b.length()) {
// compare case sensitive so similar cases are ordered together
return std::string::traits_type::compare(a.data(), b.data(), a.length()) <
0;
}
return a.length() < b.length();
}
private:
CXCompletionResult completionResult_;
mutable int priority_;
mutable std::string typedText_;
mutable std::string briefDoc_;
};
CodeCompletion::CodeCompletion(TUManager &tuManager, bool detailedCompletions)
: tuManager_(tuManager), detailedCompletions_(detailedCompletions) {
TUManager::Settings settings;
settings.parseTUOptions |= CXTranslationUnit_CacheCompletionResults;
#if defined(CINDEX_VERSION_MAJOR) && defined(CINDEX_VERSION_MINOR) && \
(CINDEX_VERSION_MAJOR > 0 || CINDEX_VERSION_MINOR >= 6)
settings.parseTUOptions |=
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
#endif
settingsID_ = tuManager.registerSettings(settings);
}
CodeCompletion::~CodeCompletion() {
tuManager_.unregisterSettings(settingsID_);
}
std::string CodeCompletion::handleRequest(const JSONObjectWrapper &data,
std::ostream &out) {
bool valid = true;
const std::string &file = data.check(L"file", valid);
unsigned line = data.check(L"line", valid);
unsigned column = data.check(L"column", valid);
const std::vector<std::string> &flags = data.get(L"flags");
out << ":results (";
if (!valid) {
std::clog << "Invalid completion request \"file\" and/or \"line\""
" and/or \"column\" are invalid." << std::endl;
} else if (CXTranslationUnit tu = tuManager_.parse(file, flags)) {
// TODO: enhance ? actually the function return false on error,
// we can display the error to the user maybe ?
(void)complete(tu, file, line, column, out);
}
out << ")";
return (detailedCompletions_ ? ":completion" : ":completion-simple");
}
void printSimpleResults(const CompletionResults &completions,
std::ostream &out) {
for (CompletionResults::const_iterator it = completions.begin(),
end = completions.end(),
previous = completions.end();
it != end;
previous = it++) {
const std::string &typedText(it->getTypedText());
if (!typedText.empty()) {
// XXX: Since results are sorted we avoid duplicates by looking at
// the previous value.
if (previous == end || previous->getTypedText() != typedText) {
out << typedText << " ";
}
}
}
}
bool CodeCompletion::complete(CXTranslationUnit &tu,
const std::string &filename,
unsigned line,
unsigned column,
std::ostream &out) {
if (CXCodeCompleteResults *completions =
clang_codeCompleteAt(tu,
filename.c_str(),
line,
column,
0,
0,
#if defined(CINDEX_VERSION_MAJOR) && defined(CINDEX_VERSION_MINOR) && \
(CINDEX_VERSION_MAJOR > 0 || CINDEX_VERSION_MINOR >= 6)
CXCodeComplete_IncludeBriefComments |
#endif
// CXCodeComplete_IncludeCodePatterns |
clang_defaultCodeCompleteOptions())) {
handleDiagnostics(completions);
CompletionResults candidates(completions->NumResults);
for (unsigned i = 0; i != candidates.size(); ++i) {
candidates[i].setCompletionResult(completions->Results[i]);
}
std::sort(candidates.begin(), candidates.end());
if (detailedCompletions_) {
printDetailedResults(candidates, out);
} else {
printSimpleResults(candidates, out);
}
clang_disposeCodeCompleteResults(completions);
return true;
}
// FIXME: really an error or just no results ?
return false;
}
void
CodeCompletion::handleDiagnostics(CXCodeCompleteResults *completions) const {
if (unsigned numErrors = clang_codeCompleteGetNumDiagnostics(completions)) {
std::clog << numErrors << " errors/warnings found during completion.\n";
// TODO: do not log warnings, only errors?
for (unsigned i = 0; i < numErrors; i++) {
CXDiagnostic diagnostic = clang_codeCompleteGetDiagnostic(completions, i);
CXString s = clang_formatDiagnostic(
diagnostic, clang_defaultDiagnosticDisplayOptions());
std::clog << clang_getCString(s) << std::endl;
clang_disposeString(s);
clang_disposeDiagnostic(diagnostic);
}
}
}
namespace {
inline void chunkContentCell(const char *key,
const CompletionChunk &chunk,
std::ostream &out) {
out << " (" << key << " . " << chunk.escapedContent() << ")";
}
void formatCompletionChunks(const CompletionChunk &start,
std::ostream &out,
bool *hasOptional = 0) {
out << "(";
if (hasOptional) {
*hasOptional = false;
}
for (CompletionChunk chunk(start); chunk.hasNext(); chunk.next()) {
switch (chunk.kind()) {
case CXCompletionChunk_TypedText:
out << " " << chunk.escapedContent();
break;
case CXCompletionChunk_ResultType: chunkContentCell("r", chunk, out); break;
case CXCompletionChunk_Placeholder: chunkContentCell("ph", chunk, out); break;
case CXCompletionChunk_Text: chunkContentCell("t", chunk, out); break;
case CXCompletionChunk_Informative: chunkContentCell("i", chunk, out); break;
case CXCompletionChunk_CurrentParameter: chunkContentCell("p", chunk, out); break;
case CXCompletionChunk_LeftParen: out << " ?("; break;
case CXCompletionChunk_RightParen: out << " ?)"; break;
case CXCompletionChunk_LeftBracket: out << " ?["; break;
case CXCompletionChunk_RightBracket: out << " ?]"; break;
case CXCompletionChunk_LeftBrace: out << " ?{"; break;
case CXCompletionChunk_RightBrace: out << " ?}"; break;
case CXCompletionChunk_LeftAngle: out << " ?<"; break;
case CXCompletionChunk_RightAngle: out << " ?>"; break;
case CXCompletionChunk_Comma: out << " ?,"; break;
case CXCompletionChunk_Colon: out << " ?:"; break;
case CXCompletionChunk_SemiColon: out << " ?;"; break;
case CXCompletionChunk_Equal: out << " ?="; break;
case CXCompletionChunk_HorizontalSpace: out << " ? "; break;
case CXCompletionChunk_VerticalSpace: out << " ?\n"; break;
case CXCompletionChunk_Optional:
if (hasOptional) {
*hasOptional = true;
}
out << "(opt . ";
formatCompletionChunks(chunk.optionalChunks(), out);
out << ")";
break;
default:
break;
}
}
out << ")";
}
} // unnamed namespace
void CodeCompletion::printDetailedResults(const CompletionResults &completions,
std::ostream &out) {
for (CompletionResults::const_iterator it = completions.begin(),
end = completions.end();
it != end;
++it) {
bool hasOptional = false;
out << "\n(";
formatCompletionChunks(it->firstChunk(), out, &hasOptional);
if (hasOptional) {
out << " (opt . t)";
}
const std::string &briefDoc = it->briefDoc();
if (!briefDoc.empty())
out << " (b . " << briefDoc << ")";
out << " (p . " << it->priority() << "))";
}
}