forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_document_hover.cc
123 lines (109 loc) · 3.72 KB
/
text_document_hover.cc
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
#include "message_handler.h"
#include "query_utils.h"
#include "queue_manager.h"
namespace {
MethodType kMethodType = "textDocument/hover";
// Find the comments for |sym|, if any.
optional<lsMarkedString> GetComments(QueryDatabase* db, SymbolRef sym) {
auto make = [](std::string_view comment) -> optional<lsMarkedString> {
lsMarkedString result;
result.value = std::string(comment.data(), comment.length());
return result;
};
optional<lsMarkedString> result;
WithEntity(db, sym, [&](const auto& entity) {
if (const auto* def = entity.AnyDef()) {
if (!def->comments.empty())
result = make(def->comments);
}
});
return result;
}
// Returns the hover or detailed name for `sym`, if any.
optional<lsMarkedString> GetHoverOrName(QueryDatabase* db,
const std::string& language,
SymbolRef sym) {
auto make = [&](std::string_view comment) {
lsMarkedString result;
result.language = language;
result.value = std::string(comment.data(), comment.length());
return result;
};
optional<lsMarkedString> result;
WithEntity(db, sym, [&](const auto& entity) {
if (const auto* def = entity.AnyDef()) {
if (!def->hover.empty())
result = make(def->hover);
else if (!def->detailed_name.empty())
result = make(def->detailed_name);
}
});
return result;
}
struct In_TextDocumentHover : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(In_TextDocumentHover, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentHover);
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
struct Result {
std::vector<lsMarkedString> contents;
optional<lsRange> range;
};
lsRequestId id;
optional<Result> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range);
void Reflect(Writer& visitor, Out_TextDocumentHover& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
if (value.result)
REFLECT_MEMBER(result);
else {
// Empty optional<> is elided by the default serializer, we need to write
// |null| to be compliant with the LSP.
visitor.Key("result");
visitor.Null();
}
REFLECT_MEMBER_END();
}
struct Handler_TextDocumentHover : BaseMessageHandler<In_TextDocumentHover> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_TextDocumentHover* request) override {
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
request->params.textDocument.uri.GetAbsolutePath(),
&file)) {
return;
}
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
Out_TextDocumentHover out;
out.id = request->id;
for (SymbolRef sym :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return hover.
optional<lsRange> ls_range = GetLsRange(
working_files->GetFileByFilename(file->def->path), sym.range);
if (!ls_range)
continue;
optional<lsMarkedString> comments = GetComments(db, sym);
optional<lsMarkedString> hover =
GetHoverOrName(db, file->def->language, sym);
if (comments || hover) {
out.result = Out_TextDocumentHover::Result();
out.result->range = *ls_range;
if (comments)
out.result->contents.push_back(*comments);
if (hover)
out.result->contents.push_back(*hover);
break;
}
}
QueueManager::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentHover);
} // namespace