This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
text_document_definition.cc
176 lines (161 loc) · 6.21 KB
/
text_document_definition.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
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
#include "lex_utils.h"
#include "message_handler.h"
#include "query_utils.h"
#include "queue_manager.h"
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
namespace {
MethodType kMethodType = "textDocument/definition";
struct In_TextDocumentDefinition : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(In_TextDocumentDefinition, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentDefinition);
std::vector<QueryId::LexicalRef> GetNonDefDeclarationTargets(
QueryDatabase* db,
QueryId::SymbolRef sym) {
switch (sym.kind) {
case SymbolKind::Var: {
std::vector<QueryId::LexicalRef> ret = GetNonDefDeclarations(db, sym);
// If there is no declaration, jump the its type.
if (ret.empty()) {
for (auto& def : db->GetVar(sym).def)
if (def.type) {
if (auto use = GetDefinitionSpell(
db, SymbolIdx{*def.type, SymbolKind::Type})) {
ret.push_back(*use);
break;
}
}
}
return ret;
}
default:
return GetNonDefDeclarations(db, sym);
}
}
struct Handler_TextDocumentDefinition
: BaseMessageHandler<In_TextDocumentDefinition> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_TextDocumentDefinition* request) override {
QueryId::File file_id;
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
request->params.textDocument.uri.GetAbsolutePath(),
&file, &file_id)) {
return;
}
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
Out_LocationList out;
out.id = request->id;
Maybe<QueryId::LexicalRef> on_def;
bool has_symbol = false;
int target_line = request->params.position.line;
int target_column = request->params.position.character;
for (QueryId::SymbolRef sym :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return definition.
has_symbol = true;
// Special cases which are handled:
// - symbol has declaration but no definition (ie, pure virtual)
// - start at spelling but end at extent for better mouse tooltip
// - goto declaration while in definition of recursive type
std::vector<QueryId::LexicalRef> uses;
EachEntityDef(db, sym, [&](const auto& def) {
if (def.spell && def.extent) {
QueryId::LexicalRef spell = *def.spell;
// If on a definition, clear |uses| to find declarations below.
if (spell.file == file_id &&
spell.range.Contains(target_line, target_column)) {
on_def = spell;
uses.clear();
}
// We use spelling start and extent end because this causes vscode
// to highlight the entire definition when previewing / hoving with
// the mouse.
spell.range.end = def.extent->range.end;
uses.push_back(spell);
}
return true;
});
if (uses.empty()) {
// The symbol has no definition or the cursor is on a definition.
uses = GetNonDefDeclarationTargets(db, sym);
// There is no declaration but the cursor is on a definition.
if (uses.empty() && on_def)
uses.push_back(*on_def);
}
AddRange(&out.result, GetLsLocations(db, working_files, uses));
}
// No symbols - check for includes.
if (out.result.empty()) {
for (const IndexInclude& include : file->def->includes) {
if (include.line == target_line) {
lsLocation result;
result.uri = lsDocumentUri::FromPath(include.resolved_path);
out.result.push_back(result);
has_symbol = true;
break;
}
}
// Find the best match of the identifier at point.
if (!has_symbol) {
lsPosition position = request->params.position;
const std::string& buffer = working_file->buffer_content;
std::string_view query = LexIdentifierAroundPos(position, buffer);
std::string_view short_query = query;
{
auto pos = query.rfind(':');
if (pos != std::string::npos)
short_query = query.substr(pos + 1);
}
// For symbols whose short/detailed names contain |query| as a
// substring, we use the tuple <length difference, negative position,
// not in the same file, line distance> to find the best match.
std::tuple<int, int, bool, int> best_score{INT_MAX, 0, true, 0};
int best_i = -1;
for (int i = 0; i < (int)db->symbols.size(); ++i) {
if (db->symbols[i].kind == SymbolKind::Invalid)
continue;
std::string_view name = short_query.size() < query.size()
? db->GetSymbolDetailedName(i)
: db->GetSymbolShortName(i);
auto pos = name.rfind(short_query);
if (pos == std::string::npos)
continue;
if (optional<QueryId::LexicalRef> use =
GetDefinitionSpell(db, db->symbols[i])) {
std::tuple<int, int, bool, int> score{
int(name.size() - short_query.size()), -int(pos),
use->file != file_id,
std::abs(use->range.start.line - position.line)};
// Update the score with qualified name if the qualified name
// occurs in |name|.
pos = name.rfind(query);
if (pos != std::string::npos) {
std::get<0>(score) = int(name.size() - query.size());
std::get<1>(score) = -int(pos);
}
if (score < best_score) {
best_score = score;
best_i = i;
}
}
}
if (best_i != -1) {
optional<QueryId::LexicalRef> use =
GetDefinitionSpell(db, db->symbols[best_i]);
assert(use);
if (auto ls_loc = GetLsLocation(db, working_files, *use))
out.result.push_back(*ls_loc);
}
}
}
QueueManager::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDefinition);
} // namespace