2018-02-12 09:01:02 +00:00
|
|
|
#include "fuzzy_match.h"
|
|
|
|
#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-12 05:21:03 +00:00
|
|
|
void PushBack(std::vector<lsLocation>* result, optional<lsLocation> location) {
|
2017-12-06 03:32:33 +00:00
|
|
|
if (location)
|
|
|
|
result->push_back(*location);
|
|
|
|
}
|
|
|
|
|
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;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsLocation> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
|
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> GetGotoDefinitionTargets(QueryDatabase* db,
|
|
|
|
SymbolRef sym) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-20 06:20:44 +00:00
|
|
|
// Returns GetDeclarationsOfSymbolForGotoDefinition and
|
|
|
|
// variable type definition.
|
|
|
|
case SymbolKind::Var: {
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> ret =
|
2018-02-09 17:42:10 +00:00
|
|
|
GetDeclarationsOfSymbolForGotoDefinition(db, sym);
|
2018-02-18 05:52:14 +00:00
|
|
|
QueryVar::Def* def = db->GetVar(sym).AnyDef();
|
|
|
|
if (def && def->type) {
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> types = GetDeclarationsOfSymbolForGotoDefinition(
|
2018-02-18 05:52:14 +00:00
|
|
|
db, SymbolIdx{*def->type, SymbolKind::Type});
|
2017-12-20 06:20:44 +00:00
|
|
|
ret.insert(ret.end(), types.begin(), types.end());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
default:
|
2018-02-09 17:42:10 +00:00
|
|
|
return GetDeclarationsOfSymbolForGotoDefinition(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-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-11 04:30:27 +00:00
|
|
|
Maybe<Use> def_loc = GetDefinitionSpellingOfSymbol(db, sym);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// We use spelling start and extent end because this causes vscode to
|
|
|
|
// highlight the entire definition when previewing / hoving with the
|
|
|
|
// mouse.
|
2018-02-11 04:01:10 +00:00
|
|
|
Maybe<Use> extent = GetDefinitionExtentOfSymbol(db, sym);
|
|
|
|
if (def_loc && extent)
|
|
|
|
def_loc->range.end = extent->range.end;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// If the cursor is currently at or in the definition we should goto
|
|
|
|
// the declaration if possible. We also want to use declarations if
|
|
|
|
// we're pointing to, ie, a pure virtual function which has no
|
|
|
|
// definition.
|
2018-02-12 04:22:47 +00:00
|
|
|
if (!def_loc || (def_loc->file == file_id &&
|
2017-12-06 03:32:33 +00:00
|
|
|
def_loc->range.Contains(target_line, target_column))) {
|
|
|
|
// Goto declaration.
|
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> targets = GetGotoDefinitionTargets(db, sym);
|
|
|
|
for (Use target : targets) {
|
2017-12-20 06:20:44 +00:00
|
|
|
optional<lsLocation> ls_target =
|
|
|
|
GetLsLocation(db, working_files, target);
|
|
|
|
if (ls_target)
|
|
|
|
out.result.push_back(*ls_target);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
// We found some declarations. Break so we don't add the definition
|
|
|
|
// location.
|
|
|
|
if (!out.result.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def_loc) {
|
|
|
|
PushBack(&out.result, GetLsLocation(db, working_files, *def_loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
lsLocation result;
|
|
|
|
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-13 19:12:08 +00:00
|
|
|
std::string query = LexWordAroundPos(position, buffer);
|
2018-02-12 09:01:02 +00:00
|
|
|
|
2018-02-13 21:01:55 +00:00
|
|
|
// For symbols whose detailed names contain |query| as a substring,
|
|
|
|
// we use the tuple <length difference, not in the same file, line
|
|
|
|
// distance> to find the best match.
|
2018-02-14 05:35:08 +00:00
|
|
|
std::tuple<int, bool, int> best_score{INT_MAX, 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-13 07:20:08 +00:00
|
|
|
std::string_view detailed_name = db->GetSymbolDetailedName(i);
|
2018-02-13 19:12:08 +00:00
|
|
|
auto idx = detailed_name.find(query);
|
2018-02-13 18:23:56 +00:00
|
|
|
if (idx == std::string::npos)
|
|
|
|
continue;
|
2018-02-13 19:12:08 +00:00
|
|
|
Maybe<Use> use = GetDefinitionSpellingOfSymbol(db, db->symbols[i]);
|
|
|
|
if (!use)
|
|
|
|
continue;
|
2018-02-13 18:23:56 +00:00
|
|
|
|
2018-02-14 05:35:08 +00:00
|
|
|
std::tuple<int, bool, int> score{
|
2018-02-13 19:12:08 +00:00
|
|
|
int(detailed_name.size() - query.size()), use->file != file_id,
|
|
|
|
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) {
|
|
|
|
Maybe<Use> use = GetDefinitionSpellingOfSymbol(db, db->symbols[best_i]);
|
2018-02-13 19:12:08 +00:00
|
|
|
assert(use);
|
|
|
|
optional<lsLocation> ls_loc = GetLsLocation(db, working_files, *use);
|
|
|
|
if (ls_loc)
|
|
|
|
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
|