2018-08-21 05:27:52 +00:00
|
|
|
/* 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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-11-27 06:29:28 +00:00
|
|
|
#include "query.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-05-17 06:49:21 +00:00
|
|
|
#include <unordered_set>
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-10-28 17:49:31 +00:00
|
|
|
struct ReferenceParam : public TextDocumentPositionParam {
|
|
|
|
struct Context {
|
2017-12-06 04:39:44 +00:00
|
|
|
// Include the declaration of the current symbol.
|
2018-05-17 06:49:21 +00:00
|
|
|
bool includeDeclaration = false;
|
2018-10-28 17:49:31 +00:00
|
|
|
} context;
|
2018-10-30 06:49:52 +00:00
|
|
|
|
|
|
|
// ccls extension
|
|
|
|
// If not empty, restrict to specified folders.
|
|
|
|
std::vector<std::string> folders;
|
|
|
|
// For Type, also return references of base types.
|
|
|
|
bool base = true;
|
|
|
|
// Exclude references with any |Role| bits set.
|
|
|
|
Role excludeRole = Role::None;
|
|
|
|
// Include references with all |Role| bits set.
|
|
|
|
Role role = Role::None;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-12-02 23:53:33 +00:00
|
|
|
REFLECT_STRUCT(ReferenceParam::Context, includeDeclaration);
|
|
|
|
REFLECT_STRUCT(ReferenceParam, textDocument, position, context, folders, base,
|
|
|
|
excludeRole, role);
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-12-02 23:53:33 +00:00
|
|
|
void MessageHandler::textDocument_references(JsonReader &reader,
|
|
|
|
ReplyOnce &reply) {
|
2018-10-28 17:49:31 +00:00
|
|
|
ReferenceParam param;
|
|
|
|
Reflect(reader, param);
|
2018-12-02 00:55:51 +00:00
|
|
|
QueryFile *file = FindFile(param.textDocument.uri.GetPath());
|
2018-12-01 06:44:52 +00:00
|
|
|
WorkingFile *wf = file ? wfiles->GetFile(file->def->path) : nullptr;
|
2018-12-02 00:55:51 +00:00
|
|
|
if (!wf) {
|
|
|
|
reply.NotReady(file);
|
2018-10-28 17:49:31 +00:00
|
|
|
return;
|
2018-12-02 00:55:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 06:49:52 +00:00
|
|
|
for (auto &folder : param.folders)
|
|
|
|
EnsureEndsInSlash(folder);
|
|
|
|
std::vector<uint8_t> file_set = db->GetFileSet(param.folders);
|
2018-11-03 20:52:43 +00:00
|
|
|
std::vector<Location> result;
|
2018-09-23 19:10:40 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
std::unordered_set<Use> seen_uses;
|
|
|
|
int line = param.position.line;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-12-01 06:44:52 +00:00
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
|
2018-10-28 17:49:31 +00:00
|
|
|
// Found symbol. Return references.
|
|
|
|
std::unordered_set<Usr> seen;
|
|
|
|
seen.insert(sym.usr);
|
|
|
|
std::vector<Usr> stack{sym.usr};
|
2018-11-03 20:52:43 +00:00
|
|
|
if (sym.kind != Kind::Func)
|
2018-10-30 06:49:52 +00:00
|
|
|
param.base = false;
|
2018-10-28 17:49:31 +00:00
|
|
|
while (stack.size()) {
|
|
|
|
sym.usr = stack.back();
|
|
|
|
stack.pop_back();
|
2018-11-03 20:52:43 +00:00
|
|
|
auto fn = [&](Use use, SymbolKind parent_kind) {
|
2018-10-30 06:49:52 +00:00
|
|
|
if (file_set[use.file_id] &&
|
|
|
|
Role(use.role & param.role) == param.role &&
|
|
|
|
!(use.role & param.excludeRole) && seen_uses.insert(use).second)
|
|
|
|
if (auto loc = GetLsLocation(db, wfiles, use))
|
2018-10-28 17:49:31 +00:00
|
|
|
result.push_back(*loc);
|
|
|
|
};
|
|
|
|
WithEntity(db, sym, [&](const auto &entity) {
|
2018-11-03 20:52:43 +00:00
|
|
|
SymbolKind parent_kind = SymbolKind::Unknown;
|
2018-10-28 17:49:31 +00:00
|
|
|
for (auto &def : entity.def)
|
|
|
|
if (def.spell) {
|
|
|
|
parent_kind = GetSymbolKind(db, sym);
|
2018-10-30 06:49:52 +00:00
|
|
|
if (param.base)
|
2018-10-28 17:49:31 +00:00
|
|
|
for (Usr usr : def.GetBases())
|
|
|
|
if (!seen.count(usr)) {
|
|
|
|
seen.insert(usr);
|
|
|
|
stack.push_back(usr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (Use use : entity.uses)
|
|
|
|
fn(use, parent_kind);
|
|
|
|
if (param.context.includeDeclaration) {
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &def : entity.def)
|
2018-10-28 17:49:31 +00:00
|
|
|
if (def.spell)
|
|
|
|
fn(*def.spell, parent_kind);
|
|
|
|
for (Use use : entity.declarations)
|
2018-05-17 06:49:21 +00:00
|
|
|
fn(use, parent_kind);
|
2018-01-20 05:11:03 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
});
|
2018-04-08 17:03:50 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-20 05:11:03 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
if (result.empty()) {
|
|
|
|
// |path| is the #include line. If the cursor is not on such line but line
|
|
|
|
// = 0,
|
|
|
|
// use the current filename.
|
|
|
|
std::string path;
|
2018-12-01 06:44:52 +00:00
|
|
|
if (line == 0 || line >= (int)wf->buffer_lines.size() - 1)
|
2018-10-28 17:49:31 +00:00
|
|
|
path = file->def->path;
|
|
|
|
for (const IndexInclude &include : file->def->includes)
|
|
|
|
if (include.line == param.position.line) {
|
|
|
|
path = include.resolved_path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (path.size())
|
|
|
|
for (QueryFile &file1 : db->files)
|
|
|
|
if (file1.def)
|
|
|
|
for (const IndexInclude &include : file1.def->includes)
|
|
|
|
if (include.resolved_path == path) {
|
|
|
|
// Another file |file1| has the same include line.
|
2018-11-03 20:52:43 +00:00
|
|
|
Location &loc = result.emplace_back();
|
|
|
|
loc.uri = DocumentUri::FromPath(file1.def->path);
|
2018-10-28 17:49:31 +00:00
|
|
|
loc.range.start.line = loc.range.end.line = include.line;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
|
|
|
|
if ((int)result.size() >= g_config->xref.maxNum)
|
|
|
|
result.resize(g_config->xref.maxNum);
|
|
|
|
reply(result);
|
|
|
|
}
|
|
|
|
} // namespace ccls
|