ccls/src/messages/textDocument_definition.cc

206 lines
7.3 KiB
C++
Raw Normal View History

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-28 00:50:02 +00:00
using namespace ccls;
2017-12-06 03:32:33 +00:00
2018-08-09 17:08:14 +00:00
#include <cstdlib>
#include <ctype.h>
#include <limits.h>
2017-12-06 03:32:33 +00:00
namespace {
MethodType kMethodType = "textDocument/definition";
2017-12-06 03:32:33 +00:00
2018-03-22 05:01:21 +00:00
struct In_TextDocumentDefinition : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
2017-12-06 04:39:44 +00:00
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(In_TextDocumentDefinition, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentDefinition);
2017-12-06 04:39:44 +00:00
struct Out_TextDocumentDefinition
: public lsOutMessage<Out_TextDocumentDefinition> {
lsRequestId id;
std::vector<lsLocationEx> result;
2017-12-06 04:39:44 +00:00
};
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
2018-08-09 17:08:14 +00:00
std::vector<Use> GetNonDefDeclarationTargets(DB *db, SymbolRef sym) {
2018-02-09 17:42:10 +00:00
switch (sym.kind) {
2018-08-09 17:08:14 +00:00
case SymbolKind::Var: {
std::vector<Use> 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 (Maybe<Use> use = GetDefinitionSpell(
db, SymbolIdx{def.type, SymbolKind::Type})) {
ret.push_back(*use);
break;
}
2018-08-09 17:08:14 +00:00
}
}
2018-08-09 17:08:14 +00:00
return ret;
}
default:
return GetNonDefDeclarations(db, sym);
}
}
struct Handler_TextDocumentDefinition
: BaseMessageHandler<In_TextDocumentDefinition> {
MethodType GetMethodType() const override { return kMethodType; }
2018-08-09 17:08:14 +00:00
void Run(In_TextDocumentDefinition *request) override {
auto &params = request->params;
int file_id;
2018-08-09 17:08:14 +00:00
QueryFile *file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetPath(), &file, &file_id))
2017-12-06 03:32:33 +00:00
return;
Out_TextDocumentDefinition out;
out.id = request->id;
Maybe<Range> range;
Maybe<Use> on_def;
2018-08-09 17:08:14 +00:00
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
lsPosition &ls_pos = params.position;
2017-12-06 03:32:33 +00:00
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, ls_pos)) {
if (!range)
range = sym.range;
else if (!(*range == sym.range))
break;
2017-12-06 03:32:33 +00:00
// Found symbol. Return definition.
// 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<Use> uses;
2018-08-09 17:08:14 +00:00
EachEntityDef(db, sym, [&](const auto &def) {
if (def.spell && def.extent) {
Use spell = *def.spell;
// If on a definition, clear |uses| to find declarations below.
if (spell.file_id == file_id &&
spell.range.Contains(ls_pos.line, ls_pos.character)) {
on_def = spell;
uses.clear();
return false;
}
// 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);
2017-12-06 03:32:33 +00:00
}
return true;
});
2017-12-06 03:32:33 +00:00
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);
}
auto locs = GetLsLocationExs(db, working_files, uses);
out.result.insert(out.result.end(), locs.begin(), locs.end());
2017-12-06 03:32:33 +00:00
}
if (out.result.size()) {
std::sort(out.result.begin(), out.result.end());
out.result.erase(std::unique(out.result.begin(), out.result.end()),
out.result.end());
} else {
// Check #include
2018-08-09 17:08:14 +00:00
for (const IndexInclude &include : file->def->includes) {
if (include.line == ls_pos.line) {
lsLocationEx result;
2017-12-06 03:32:33 +00:00
result.uri = lsDocumentUri::FromPath(include.resolved_path);
out.result.push_back(result);
range = {{0, 0}, {0, 0}};
2017-12-06 03:32:33 +00:00
break;
}
}
// Find the best match of the identifier at point.
if (!range) {
lsPosition position = request->params.position;
2018-08-09 17:08:14 +00:00
const std::string &buffer = wfile->buffer_content;
2018-02-22 23:49:16 +00:00
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 = SymbolKind::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<Use> use = GetDefinitionSpell(db, sym)) {
std::tuple<int, int, bool, int> score{
int(name.size() - short_query.size()), 0,
use->file_id != file_id,
std::abs(use->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;
}
}
};
2018-08-09 17:08:14 +00:00
for (auto &func : db->funcs)
fn({func.usr, SymbolKind::Func});
2018-08-09 17:08:14 +00:00
for (auto &type : db->types)
fn({type.usr, SymbolKind::Type});
2018-08-09 17:08:14 +00:00
for (auto &var : db->vars)
if (var.def.size() && !var.def[0].is_local())
fn({var.usr, SymbolKind::Var});
if (best_sym.kind != SymbolKind::Invalid) {
Maybe<Use> use = GetDefinitionSpell(db, best_sym);
assert(use);
if (auto ls_loc = GetLsLocationEx(db, working_files, *use,
2018-04-04 06:05:41 +00:00
g_config->xref.container))
out.result.push_back(*ls_loc);
}
}
2017-12-06 03:32:33 +00:00
}
2018-05-28 00:50:02 +00:00
pipeline::WriteStdout(kMethodType, out);
2017-12-06 03:32:33 +00:00
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDefinition);
2018-08-09 17:08:14 +00:00
} // namespace