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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "query_utils.h"
|
|
|
|
|
2018-05-17 06:49:21 +00:00
|
|
|
#include <unordered_set>
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-23 19:10:40 +00:00
|
|
|
using namespace ccls;
|
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/references";
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentReferences : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
struct lsReferenceContext {
|
2018-05-17 06:49:21 +00:00
|
|
|
bool base = true;
|
2018-07-05 02:16:56 +00:00
|
|
|
// Exclude references with any |Role| bits set.
|
|
|
|
Role excludeRole = Role::None;
|
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-07-05 02:16:56 +00:00
|
|
|
// Include references with all |Role| bits set.
|
|
|
|
Role role = Role::None;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-01-20 05:11:03 +00:00
|
|
|
struct Params {
|
2017-12-06 04:39:44 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
|
|
|
lsReferenceContext context;
|
|
|
|
};
|
|
|
|
|
2018-01-20 05:11:03 +00:00
|
|
|
Params params;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::lsReferenceContext, base,
|
|
|
|
excludeRole, includeDeclaration, role);
|
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::Params, textDocument, position,
|
2017-12-06 04:39:44 +00:00
|
|
|
context);
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentReferences);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentReferences
|
|
|
|
: BaseMessageHandler<In_TextDocumentReferences> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void Run(In_TextDocumentReferences *request) override {
|
|
|
|
auto ¶ms = request->params;
|
|
|
|
QueryFile *file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2018-04-08 17:03:50 +00:00
|
|
|
params.textDocument.uri.GetPath(), &file))
|
2017-12-06 03:32:33 +00:00
|
|
|
return;
|
2018-10-04 23:13:30 +00:00
|
|
|
Out_LocationList out;
|
2017-12-06 03:32:33 +00:00
|
|
|
out.id = request->id;
|
2018-09-23 19:10:40 +00:00
|
|
|
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
|
|
|
|
if (!file) {
|
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 01:00:43 +00:00
|
|
|
std::unordered_set<Use> seen_uses;
|
2018-09-23 19:10:40 +00:00
|
|
|
int line = params.position.line;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-04-08 17:03:50 +00:00
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, params.position)) {
|
2017-12-06 03:32:33 +00:00
|
|
|
// Found symbol. Return references.
|
2018-05-17 06:49:21 +00:00
|
|
|
std::unordered_set<Usr> seen;
|
|
|
|
seen.insert(sym.usr);
|
|
|
|
std::vector<Usr> stack{sym.usr};
|
|
|
|
if (sym.kind != SymbolKind::Func)
|
|
|
|
params.context.base = false;
|
|
|
|
while (stack.size()) {
|
|
|
|
sym.usr = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
auto fn = [&](Use use, lsSymbolKind parent_kind) {
|
2018-07-05 02:16:56 +00:00
|
|
|
if (Role(use.role & params.context.role) == params.context.role &&
|
2018-07-31 01:00:43 +00:00
|
|
|
!(use.role & params.context.excludeRole) &&
|
|
|
|
seen_uses.insert(use).second)
|
2018-10-04 23:13:30 +00:00
|
|
|
if (auto loc = GetLsLocation(db, working_files, use)) {
|
|
|
|
out.result.push_back(*loc);
|
2018-05-17 06:49:21 +00:00
|
|
|
}
|
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
WithEntity(db, sym, [&](const auto &entity) {
|
2018-05-17 06:49:21 +00:00
|
|
|
lsSymbolKind parent_kind = lsSymbolKind::Unknown;
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &def : entity.def)
|
2018-05-17 06:49:21 +00:00
|
|
|
if (def.spell) {
|
|
|
|
parent_kind = GetSymbolKind(db, sym);
|
|
|
|
if (params.context.base)
|
|
|
|
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 (params.context.includeDeclaration) {
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &def : entity.def)
|
2018-05-17 06:49:21 +00:00
|
|
|
if (def.spell)
|
|
|
|
fn(*def.spell, parent_kind);
|
|
|
|
for (Use use : entity.declarations)
|
|
|
|
fn(use, parent_kind);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:03:50 +00:00
|
|
|
if (out.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-09-23 19:10:40 +00:00
|
|
|
if (line == 0 || line >= (int)wfile->buffer_lines.size() - 1)
|
2018-04-08 17:03:50 +00:00
|
|
|
path = file->def->path;
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const IndexInclude &include : file->def->includes)
|
2018-04-08 17:03:50 +00:00
|
|
|
if (include.line == params.position.line) {
|
|
|
|
path = include.resolved_path;
|
2018-01-20 05:11:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-04-08 17:03:50 +00:00
|
|
|
if (path.size())
|
2018-08-09 17:08:14 +00:00
|
|
|
for (QueryFile &file1 : db->files)
|
2018-04-08 17:03:50 +00:00
|
|
|
if (file1.def)
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const IndexInclude &include : file1.def->includes)
|
2018-04-08 17:03:50 +00:00
|
|
|
if (include.resolved_path == path) {
|
|
|
|
// Another file |file1| has the same include line.
|
2018-10-04 23:13:30 +00:00
|
|
|
lsLocation &loc = out.result.emplace_back();
|
|
|
|
loc.uri = lsDocumentUri::FromPath(file1.def->path);
|
|
|
|
loc.range.start.line = loc.range.end.line = include.line;
|
2018-04-08 17:03:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 05:11:03 +00:00
|
|
|
|
2018-04-04 06:05:41 +00:00
|
|
|
if ((int)out.result.size() >= g_config->xref.maxNum)
|
|
|
|
out.result.resize(g_config->xref.maxNum);
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentReferences);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|