-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathIrony.cpp
317 lines (257 loc) · 9.17 KB
/
Irony.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
/**
* \file
* \author Guillaume Papin <guillaume.papin@epitech.eu>
*
* \brief irony-server "API" definitions.
*
* \sa Irony.h for more information.
*
* This file is distributed under the GNU General Public License. See
* COPYING for details.
*/
#include "Irony.h"
#include "support/iomanip_quoted.h"
#include <algorithm>
#include <array>
#include <iostream>
#include <sstream>
#include <stdexcept>
#if defined(CINDEX_VERSION_MAJOR) && defined(CINDEX_VERSION_MINOR) && \
(CINDEX_VERSION_MAJOR > 0 || CINDEX_VERSION_MINOR >= 6)
#define HAS_BRIEF_COMMENTS_IN_COMPLETION 1
#else
#define HAS_BRIEF_COMMENTS_IN_COMPLETION 0
#endif
namespace {
std::string cxStringToStd(CXString cxString) {
std::string stdStr;
if (const char *cstr = clang_getCString(cxString)) {
stdStr = cstr;
}
clang_disposeString(cxString);
return stdStr;
}
} // unnamed namespace
Irony::Irony() : debug_(false) {
}
void Irony::check(const std::string &file,
const std::vector<std::string> &flags,
const std::vector<CXUnsavedFile> &unsavedFiles) {
std::cout << "(";
unsigned numDiag = 0;
int fatals = 0;
int errors = 0;
int warnings = 0;
CXTranslationUnit tu = tuManager_.parse(file, flags, unsavedFiles);
if (tu) {
numDiag = clang_getNumDiagnostics(tu);
} else {
fatals = 1;
}
for (unsigned i = 0; i < numDiag; ++i) {
CXDiagnostic diagnostic = clang_getDiagnostic(tu, i);
switch (clang_getDiagnosticSeverity(diagnostic)) {
case CXDiagnostic_Fatal:
fatals++;
break;
case CXDiagnostic_Error:
errors++;
break;
case CXDiagnostic_Warning:
warnings++;
break;
default:
break;
}
clang_disposeDiagnostic(diagnostic);
}
if (fatals > 0)
std::cout << " :fatals " << fatals;
if (errors > 0)
std::cout << " :errors " << errors;
if (warnings > 0)
std::cout << " :warnings " << warnings;
std::cout << ")\n";
}
namespace {
class CompletionChunk {
public:
explicit CompletionChunk(CXCompletionString completionString)
: completionString_(completionString)
, numChunks_(clang_getNumCompletionChunks(completionString_))
, chunkIdx_(0) {
}
bool hasNext() const {
return chunkIdx_ < numChunks_;
}
void next() {
if (!hasNext()) {
throw std::out_of_range("out of range completion chunk");
}
++chunkIdx_;
}
CXCompletionChunkKind kind() const {
return clang_getCompletionChunkKind(completionString_, chunkIdx_);
}
std::string text() const {
return cxStringToStd(
clang_getCompletionChunkText(completionString_, chunkIdx_));
}
private:
CXCompletionString completionString_;
unsigned int numChunks_;
unsigned chunkIdx_;
};
} // unnamed namespace
void Irony::complete(const std::string &file,
unsigned line,
unsigned col,
const std::vector<std::string> &flags,
const std::vector<CXUnsavedFile> &unsavedFiles) {
// Register the settings the first time, to enable optimization of code
// completion and request brief comments if available.
TUManager::Settings settings;
settings.parseTUOptions |= CXTranslationUnit_CacheCompletionResults;
#if HAS_BRIEF_COMMENTS_IN_COMPLETION
settings.parseTUOptions |=
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
#endif
(void)tuManager_.registerSettings(settings);
std::cout << "(\n";
CXTranslationUnit tu = tuManager_.getOrCreateTU(file, flags, unsavedFiles);
if (tu == nullptr) {
std::cout << ")\n";
return;
}
if (CXCodeCompleteResults *completions =
clang_codeCompleteAt(tu,
file.c_str(),
line,
col,
const_cast<CXUnsavedFile *>(unsavedFiles.data()),
unsavedFiles.size(),
(clang_defaultCodeCompleteOptions() &
~CXCodeComplete_IncludeCodePatterns)
#if HAS_BRIEF_COMMENTS_IN_COMPLETION
|
CXCodeComplete_IncludeBriefComments
#endif
)) {
if (debug_) {
unsigned numDiags = clang_codeCompleteGetNumDiagnostics(completions);
std::clog << "debug: complete: " << numDiags << " diagnostic(s)\n";
for (unsigned i = 0; i < numDiags; ++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);
}
}
clang_sortCodeCompletionResults(completions->Results,
completions->NumResults);
// re-use the same buffers to avoid unnecessary allocations
std::string typedtext, brief, resultType, prototype, postCompCar;
std::vector<unsigned> postCompCdr;
for (unsigned i = 0; i < completions->NumResults; ++i) {
CXCompletionResult candidate = completions->Results[i];
unsigned priority =
clang_getCompletionPriority(candidate.CompletionString);
unsigned annotationStart = 0;
bool typedTextSet = false;
typedtext.clear();
brief.clear();
resultType.clear();
prototype.clear();
postCompCar.clear();
postCompCdr.clear();
for (CompletionChunk chunk(candidate.CompletionString); chunk.hasNext();
chunk.next()) {
char ch = 0;
auto chunkKind = chunk.kind();
switch (chunkKind) {
case CXCompletionChunk_ResultType:
resultType = chunk.text();
break;
case CXCompletionChunk_TypedText:
case CXCompletionChunk_Text:
case CXCompletionChunk_Placeholder:
case CXCompletionChunk_Informative:
case CXCompletionChunk_CurrentParameter:
prototype += chunk.text();
break;
case CXCompletionChunk_LeftParen: ch = '('; break;
case CXCompletionChunk_RightParen: ch = ')'; break;
case CXCompletionChunk_LeftBracket: ch = '['; break;
case CXCompletionChunk_RightBracket: ch = ']'; break;
case CXCompletionChunk_LeftBrace: ch = '{'; break;
case CXCompletionChunk_RightBrace: ch = '}'; break;
case CXCompletionChunk_LeftAngle: ch = '<'; break;
case CXCompletionChunk_RightAngle: ch = '>'; break;
case CXCompletionChunk_Comma: ch = ','; break;
case CXCompletionChunk_Colon: ch = ':'; break;
case CXCompletionChunk_SemiColon: ch = ';'; break;
case CXCompletionChunk_Equal: ch = '='; break;
case CXCompletionChunk_HorizontalSpace: ch = ' '; break;
case CXCompletionChunk_VerticalSpace: ch = '\n'; break;
case CXCompletionChunk_Optional:
// ignored for now
break;
}
if (ch != 0) {
prototype += ch;
// commas look better followed by a space
if (ch == ',') {
prototype += ' ';
}
}
if (typedTextSet) {
if (ch != 0) {
postCompCar += ch;
if (ch == ',') {
postCompCar += ' ';
}
} else if (chunkKind == CXCompletionChunk_Text ||
chunkKind == CXCompletionChunk_TypedText) {
postCompCar += chunk.text();
} else if (chunkKind == CXCompletionChunk_Placeholder ||
chunkKind == CXCompletionChunk_CurrentParameter) {
postCompCdr.push_back(postCompCar.size());
postCompCar += chunk.text();
postCompCdr.push_back(postCompCar.size());
}
}
// Consider only the first typed text. The CXCompletionChunk_TypedText
// doc suggests that exactly one typed text will be given but at least
// in Objective-C it seems that more than one can appear, see:
// https://github.com/Sarcasm/irony-mode/pull/78#issuecomment-37115538
if (chunkKind == CXCompletionChunk_TypedText && !typedTextSet) {
typedtext = chunk.text();
// annotation is what comes after the typedtext
annotationStart = prototype.size();
typedTextSet = true;
}
}
#if HAS_BRIEF_COMMENTS_IN_COMPLETION
brief = cxStringToStd(
clang_getCompletionBriefComment(candidate.CompletionString));
#endif
// see irony-completion.el#irony-completion-candidates
std::cout << '(' << support::quoted(typedtext) //
<< ' ' << priority //
<< ' ' << support::quoted(resultType) //
<< ' ' << support::quoted(brief) //
<< ' ' << support::quoted(prototype) //
<< ' ' << annotationStart //
<< " (" << support::quoted(postCompCar);
for (unsigned index : postCompCdr)
std::cout << ' ' << index;
std::cout << ")"
<< ")\n";
}
clang_disposeCodeCompleteResults(completions);
}
std::cout << ")\n";
}