-
Notifications
You must be signed in to change notification settings - Fork 0
/
textDocument_definition.cc
225 lines (206 loc) · 7.33 KB
/
textDocument_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
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
/* Copyright 2017-2018 ccls Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "message_handler.hh"
#include "query.hh"
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
namespace ccls {
namespace {
std::vector<DeclRef> GetNonDefDeclarationTargets(DB *db, SymbolRef sym) {
switch (sym.kind) {
case Kind::Var: {
std::vector<DeclRef> ret = GetNonDefDeclarations(db, sym);
// If there is no declaration, jump to its type.
if (ret.empty()) {
for (auto &def : db->GetVar(sym).def)
if (def.type) {
if (Maybe<DeclRef> use =
GetDefinitionSpell(db, SymbolIdx{def.type, Kind::Type})) {
ret.push_back(*use);
break;
}
}
}
return ret;
}
default:
return GetNonDefDeclarations(db, sym);
}
}
} // namespace
void MessageHandler::textDocument_declaration(TextDocumentPositionParam ¶m,
ReplyOnce &reply) {
int file_id;
auto [file, wf] = FindOrFail(param.textDocument.uri.GetPath(), reply, &file_id);
if (!wf)
return;
std::vector<LocationLink> result;
Position &ls_pos = param.position;
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position))
for (DeclRef dr : GetNonDefDeclarations(db, sym))
if (!(dr.file_id == file_id &&
dr.range.Contains(ls_pos.line, ls_pos.character)))
if (auto loc = GetLocationLink(db, wfiles, dr))
result.push_back(loc);
reply.ReplyLocationLink(result);
}
void MessageHandler::textDocument_definition(TextDocumentPositionParam ¶m,
ReplyOnce &reply) {
int file_id;
auto [file, wf] = FindOrFail(param.textDocument.uri.GetPath(), reply, &file_id);
if (!wf)
return;
std::vector<LocationLink> result;
Maybe<DeclRef> on_def;
Position &ls_pos = param.position;
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, ls_pos, true)) {
// Special cases which are handled:
// - symbol has declaration but no definition (ie, pure virtual)
// - goto declaration while in definition of recursive type
std::vector<DeclRef> drs;
EachEntityDef(db, sym, [&](const auto &def) {
if (def.spell) {
DeclRef spell = *def.spell;
if (spell.file_id == file_id &&
spell.range.Contains(ls_pos.line, ls_pos.character)) {
on_def = spell;
drs.clear();
return false;
}
drs.push_back(spell);
}
return true;
});
// |uses| is empty if on a declaration/definition, otherwise it includes
// all declarations/definitions.
if (drs.empty()) {
for (DeclRef dr : GetNonDefDeclarationTargets(db, sym))
if (!(dr.file_id == file_id &&
dr.range.Contains(ls_pos.line, ls_pos.character)))
drs.push_back(dr);
// There is no declaration but the cursor is on a definition.
if (drs.empty() && on_def)
drs.push_back(*on_def);
}
for (DeclRef dr : drs)
if (auto loc = GetLocationLink(db, wfiles, dr))
result.push_back(loc);
}
if (result.empty()) {
Maybe<Range> range;
// Check #include
for (const IndexInclude &include : file->def->includes) {
if (include.line == ls_pos.line) {
result.push_back(
{DocumentUri::FromPath(include.resolved_path).raw_uri});
range = {{0, 0}, {0, 0}};
break;
}
}
// Find the best match of the identifier at point.
if (!range) {
Position position = param.position;
const std::string &buffer = wf->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};
SymbolIdx best_sym;
best_sym.kind = Kind::Invalid;
auto fn = [&](SymbolIdx sym) {
std::string_view short_name = db->GetSymbolName(sym, false),
name = short_query.size() < query.size()
? db->GetSymbolName(sym, true)
: short_name;
if (short_name != short_query)
return;
if (Maybe<DeclRef> dr = GetDefinitionSpell(db, sym)) {
std::tuple<int, int, bool, int> score{
int(name.size() - short_query.size()), 0, dr->file_id != file_id,
std::abs(dr->range.start.line - position.line)};
// Update the score with qualified name if the qualified name
// occurs in |name|.
auto 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_sym = sym;
}
}
};
for (auto &func : db->funcs)
fn({func.usr, Kind::Func});
for (auto &type : db->types)
fn({type.usr, Kind::Type});
for (auto &var : db->vars)
if (var.def.size() && !var.def[0].is_local())
fn({var.usr, Kind::Var});
if (best_sym.kind != Kind::Invalid) {
Maybe<DeclRef> dr = GetDefinitionSpell(db, best_sym);
assert(dr);
if (auto loc = GetLocationLink(db, wfiles, *dr))
result.push_back(loc);
}
}
}
reply.ReplyLocationLink(result);
}
void MessageHandler::textDocument_typeDefinition(
TextDocumentPositionParam ¶m, ReplyOnce &reply) {
auto [file, wf] = FindOrFail(param.textDocument.uri.GetPath(), reply);
if (!file)
return;
std::vector<LocationLink> result;
auto Add = [&](const QueryType &type) {
for (const auto &def : type.def)
if (def.spell)
if (auto loc = GetLocationLink(db, wfiles, *def.spell))
result.push_back(loc);
if (result.empty())
for (const DeclRef &dr : type.declarations)
if (auto loc = GetLocationLink(db, wfiles, dr))
result.push_back(loc);
};
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
switch (sym.kind) {
case Kind::Var: {
const QueryVar::Def *def = db->GetVar(sym).AnyDef();
if (def && def->type)
Add(db->Type(def->type));
break;
}
case Kind::Type: {
for (auto &def : db->GetType(sym).def)
if (def.alias_of) {
Add(db->Type(def.alias_of));
break;
}
break;
}
default:
break;
}
}
reply.ReplyLocationLink(result);
}
} // namespace ccls