Skip to content

Commit cbba220

Browse files
committed
Added information about source file location in code-completions
1 parent 67f2679 commit cbba220

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

CodeCompletion.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ using namespace std;
5151
class CustomCodeCompleteConsumer : public CodeCompleteConsumer {
5252
CodeCompletionTUInfo TUInfo;
5353
json output;
54+
SourceManager &sm;
5455

5556
public:
5657

57-
CustomCodeCompleteConsumer(const CodeCompleteOptions &opts) : CodeCompleteConsumer(opts, false),
58-
TUInfo(std::make_shared<GlobalCodeCompletionAllocator>()), output(json::array()) {
58+
CustomCodeCompleteConsumer(const CodeCompleteOptions &opts, SourceManager &sm) : CodeCompleteConsumer(opts, false),
59+
TUInfo(std::make_shared<GlobalCodeCompletionAllocator>()), output(json::array()), sm(sm) {
5960
}
6061

6162
void ProcessCodeCompleteResults(Sema &s, CodeCompletionContext ctx, CodeCompletionResult *res, unsigned n) override {
@@ -64,7 +65,7 @@ class CustomCodeCompleteConsumer : public CodeCompleteConsumer {
6465
raw_string_ostream OS(ccStr);
6566
CodeCompletionString *ccs = res[i].CreateCodeCompletionString(s, ctx, getAllocator(), TUInfo, includeBriefComments());
6667
//cout << encode(res[i], ccs).dump(2) << "\n";
67-
output.push_back(encode(res[i], ccs));
68+
output.push_back(encode(res[i], ccs, sm));
6869
}
6970

7071
}
@@ -139,12 +140,15 @@ void DoCodeCompletion(const string &filename, const string &code, int line, int
139140
lOpts.Bool = true;
140141
lOpts.GNUMode = true;
141142

143+
ci.createFileManager();
144+
ci.createSourceManager(ci.getFileManager());
145+
142146
CodeCompleteOptions ccOpts;
143147
ccOpts.IncludeMacros = 1;
144148
ccOpts.IncludeCodePatterns = 1;
145149
ccOpts.IncludeGlobals = 1;
146150
ccOpts.IncludeBriefComments = 1;
147-
CustomCodeCompleteConsumer *ccConsumer = new CustomCodeCompleteConsumer(ccOpts);
151+
CustomCodeCompleteConsumer *ccConsumer = new CustomCodeCompleteConsumer(ccOpts, ci.getSourceManager());
148152
ci.setCodeCompletionConsumer(ccConsumer);
149153

150154
FrontendOptions& fOpts = ci.getFrontendOpts();

JsonImpl.hpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,38 @@ inline json encode(const CodeCompletionString *ccs) {
205205
return res;
206206
}
207207

208-
inline json encode(const CodeCompletionResult &cc, const CodeCompletionString *ccs) {
208+
inline json encode(const CodeCompletionResult &cc, const CodeCompletionString *ccs, const SourceManager &sm) {
209209
// TODO: To obtain the complete documentation comment we must
210210
// explore cc.Declaration AST
211211
//cc.Declaration->dump();
212212

213-
string type;
213+
json res = json{
214+
{"completion", encode(ccs)}
215+
};
214216
switch (cc.Kind) {
215217
case CodeCompletionResult::RK_Declaration:
216-
type = "declaration";
218+
{
219+
SourceLocation loc = cc.Declaration->getLocation();
220+
PresumedLoc presumedLoc = sm.getPresumedLoc(loc);
221+
res["location"] = presumedLoc.getFilename();
222+
res["type"] = "Declaration";
217223
break;
224+
}
218225
case CodeCompletionResult::RK_Keyword:
219-
type = "keyword";
226+
{
227+
res["type"] = "Keyword";
220228
break;
229+
}
221230
case CodeCompletionResult::RK_Pattern:
222-
type = "pattern";
231+
{
232+
res["type"] = "Pattern";
223233
break;
234+
}
224235
case CodeCompletionResult::RK_Macro:
225-
type = "macro";
236+
{
237+
res["type"] = "Macro";
226238
break;
239+
}
227240
};
228-
return json{
229-
{"type", type},
230-
{"completion", encode(ccs)}};
241+
return res;
231242
}

0 commit comments

Comments
 (0)