2018-02-12 09:01:02 +00:00
|
|
|
#include "lex_utils.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-02-12 09:01:02 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2018-02-13 19:12:08 +00:00
|
|
|
#include <cstdlib>
|
2018-02-12 09:01:02 +00:00
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Ipc_TextDocumentDefinition
|
2018-01-19 08:47:52 +00:00
|
|
|
: public RequestMessage<Ipc_TextDocumentDefinition> {
|
2017-12-06 04:39:44 +00:00
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentDefinition;
|
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDefinition, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentDefinition);
|
|
|
|
|
|
|
|
struct Out_TextDocumentDefinition
|
|
|
|
: public lsOutMessage<Out_TextDocumentDefinition> {
|
|
|
|
lsRequestId id;
|
2018-02-21 04:26:17 +00:00
|
|
|
std::vector<lsLocationEx> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
|
|
|
|
|
2018-02-24 00:12:39 +00:00
|
|
|
std::vector<Use> GetNonDefDeclarationTargets(QueryDatabase* db, SymbolRef sym) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-20 06:20:44 +00:00
|
|
|
case SymbolKind::Var: {
|
2018-02-23 23:27:21 +00:00
|
|
|
std::vector<Use> ret = GetNonDefDeclarations(db, sym);
|
2018-02-21 04:26:17 +00:00
|
|
|
// If there is no declaration, jump the its type.
|
2018-02-20 21:56:56 +00:00
|
|
|
if (ret.empty()) {
|
|
|
|
for (auto& def : db->GetVar(sym).def)
|
|
|
|
if (def.type) {
|
2018-02-24 00:12:39 +00:00
|
|
|
if (Maybe<Use> use = GetDefinitionSpell(
|
2018-02-20 21:56:56 +00:00
|
|
|
db, SymbolIdx{*def.type, SymbolKind::Type})) {
|
|
|
|
ret.push_back(*use);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 06:20:44 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
default:
|
2018-02-23 23:27:21 +00:00
|
|
|
return GetNonDefDeclarations(db, sym);
|
2017-12-20 06:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentDefinitionHandler
|
|
|
|
: BaseMessageHandler<Ipc_TextDocumentDefinition> {
|
|
|
|
void Run(Ipc_TextDocumentDefinition* request) override {
|
|
|
|
QueryFileId file_id;
|
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file,
|
|
|
|
&file_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_TextDocumentDefinition out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-21 04:26:17 +00:00
|
|
|
Maybe<Use> on_def;
|
2018-02-12 09:01:02 +00:00
|
|
|
bool has_symbol = false;
|
2018-01-14 19:39:29 +00:00
|
|
|
int target_line = request->params.position.line;
|
|
|
|
int target_column = request->params.position.character;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-02-10 06:51:58 +00:00
|
|
|
for (SymbolRef sym :
|
2017-12-06 03:32:33 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
|
|
|
// Found symbol. Return definition.
|
2018-02-12 09:01:02 +00:00
|
|
|
has_symbol = true;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// 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
|
2018-02-20 23:40:31 +00:00
|
|
|
std::vector<Use> uses;
|
2018-02-21 01:50:48 +00:00
|
|
|
EachEntityDef(db, sym, [&](const auto& def) {
|
2018-02-20 23:40:31 +00:00
|
|
|
if (def.spell && def.extent) {
|
|
|
|
Use 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)) {
|
2018-02-21 04:26:17 +00:00
|
|
|
on_def = spell;
|
2018-02-20 23:40:31 +00:00
|
|
|
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
|
|
|
}
|
2018-02-20 23:40:31 +00:00
|
|
|
return true;
|
|
|
|
});
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-02-21 04:26:17 +00:00
|
|
|
if (uses.empty()) {
|
|
|
|
// The symbol has no definition or the cursor is on a definition.
|
2018-02-24 00:12:39 +00:00
|
|
|
uses = GetNonDefDeclarationTargets(db, sym);
|
2018-02-21 04:26:17 +00:00
|
|
|
// There is no declaration but the cursor is on a definition.
|
|
|
|
if (uses.empty() && on_def)
|
|
|
|
uses.push_back(*on_def);
|
|
|
|
}
|
2018-02-20 23:40:31 +00:00
|
|
|
AddRange(&out.result,
|
2018-02-21 04:26:17 +00:00
|
|
|
GetLsLocationExs(db, working_files, uses, config->xref.container,
|
|
|
|
config->xref.maxNum));
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!out.result.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No symbols - check for includes.
|
|
|
|
if (out.result.empty()) {
|
|
|
|
for (const IndexInclude& include : file->def->includes) {
|
|
|
|
if (include.line == target_line) {
|
2018-02-21 04:26:17 +00:00
|
|
|
lsLocationEx result;
|
2017-12-06 03:32:33 +00:00
|
|
|
result.uri = lsDocumentUri::FromPath(include.resolved_path);
|
|
|
|
out.result.push_back(result);
|
2018-02-13 21:01:55 +00:00
|
|
|
has_symbol = true;
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 09:01:02 +00:00
|
|
|
// Find the best match of the identifier at point.
|
2018-02-13 19:12:08 +00:00
|
|
|
if (!has_symbol) {
|
|
|
|
lsPosition position = request->params.position;
|
2018-02-12 09:01:02 +00:00
|
|
|
const std::string& buffer = working_file->buffer_content;
|
2018-02-22 23:49:16 +00:00
|
|
|
std::string_view query = LexIdentifierAroundPos(position, buffer);
|
2018-02-20 20:43:02 +00:00
|
|
|
bool has_scope = query.find(':') != std::string::npos;
|
2018-02-12 09:01:02 +00:00
|
|
|
|
2018-02-20 20:43:02 +00:00
|
|
|
// For symbols whose short/detailed names contain |query| as a
|
|
|
|
// substring, we use the tuple <length difference, matching 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};
|
2018-02-13 18:23:56 +00:00
|
|
|
int best_i = -1;
|
2018-02-12 09:01:02 +00:00
|
|
|
for (int i = 0; i < (int)db->symbols.size(); ++i) {
|
2018-02-13 18:23:56 +00:00
|
|
|
if (db->symbols[i].kind == SymbolKind::Invalid)
|
|
|
|
continue;
|
2018-02-20 20:43:02 +00:00
|
|
|
|
|
|
|
std::string_view name = has_scope ? db->GetSymbolDetailedName(i)
|
|
|
|
: db->GetSymbolShortName(i);
|
|
|
|
auto pos = name.find(query);
|
|
|
|
if (pos == std::string::npos)
|
2018-02-13 18:23:56 +00:00
|
|
|
continue;
|
2018-02-24 00:12:39 +00:00
|
|
|
Maybe<Use> use = GetDefinitionSpell(db, db->symbols[i]);
|
2018-02-13 19:12:08 +00:00
|
|
|
if (!use)
|
|
|
|
continue;
|
2018-02-13 18:23:56 +00:00
|
|
|
|
2018-02-20 20:43:02 +00:00
|
|
|
std::tuple<int, int, bool, int> score{
|
|
|
|
int(name.size() - query.size()), int(pos), use->file != file_id,
|
2018-02-13 19:12:08 +00:00
|
|
|
std::abs(use->range.start.line - position.line)};
|
2018-02-13 18:23:56 +00:00
|
|
|
if (score < best_score) {
|
|
|
|
best_score = score;
|
2018-02-12 09:01:02 +00:00
|
|
|
best_i = i;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 18:23:56 +00:00
|
|
|
if (best_i != -1) {
|
2018-03-20 02:51:42 +00:00
|
|
|
Maybe<Use> use = GetDefinitionSpell(db, db->symbols[best_i]);
|
2018-02-13 19:12:08 +00:00
|
|
|
assert(use);
|
2018-02-21 04:26:17 +00:00
|
|
|
if (auto ls_loc = GetLsLocationEx(db, working_files, *use,
|
|
|
|
config->xref.container))
|
2018-02-13 19:12:08 +00:00
|
|
|
out.result.push_back(*ls_loc);
|
2018-02-12 09:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentDefinitionHandler);
|
2017-12-20 06:20:44 +00:00
|
|
|
} // namespace
|