2018-02-12 09:01:02 +00:00
|
|
|
#include "fuzzy_match.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "lex_utils.h"
|
|
|
|
#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
|
|
|
|
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-12-29 16:29:47 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2017-12-19 05:31:19 +00:00
|
|
|
|
|
|
|
// Lookup |symbol| in |db| and insert the value into |result|.
|
2017-12-24 03:23:29 +00:00
|
|
|
bool InsertSymbolIntoResult(QueryDatabase* db,
|
2017-12-19 05:31:19 +00:00
|
|
|
WorkingFiles* working_files,
|
|
|
|
SymbolIdx symbol,
|
|
|
|
std::vector<lsSymbolInformation>* result) {
|
2017-12-23 16:01:43 +00:00
|
|
|
optional<lsSymbolInformation> info =
|
|
|
|
GetSymbolInfo(db, working_files, symbol, false /*use_short_name*/);
|
2017-12-19 05:31:19 +00:00
|
|
|
if (!info)
|
2017-12-24 03:23:29 +00:00
|
|
|
return false;
|
2017-12-19 05:31:19 +00:00
|
|
|
|
2018-02-24 00:12:39 +00:00
|
|
|
Maybe<Use> location = GetDefinitionExtent(db, symbol);
|
2018-02-11 04:01:10 +00:00
|
|
|
Use loc;
|
2018-02-09 05:11:35 +00:00
|
|
|
if (location)
|
|
|
|
loc = *location;
|
|
|
|
else {
|
2018-02-23 23:27:21 +00:00
|
|
|
auto decls = GetNonDefDeclarations(db, symbol);
|
2017-12-19 05:31:19 +00:00
|
|
|
if (decls.empty())
|
2017-12-24 03:23:29 +00:00
|
|
|
return false;
|
2018-02-09 05:11:35 +00:00
|
|
|
loc = decls[0];
|
2017-12-19 05:31:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 05:11:35 +00:00
|
|
|
optional<lsLocation> ls_location = GetLsLocation(db, working_files, loc);
|
2017-12-19 05:31:19 +00:00
|
|
|
if (!ls_location)
|
2017-12-24 03:23:29 +00:00
|
|
|
return false;
|
2017-12-19 05:31:19 +00:00
|
|
|
info->location = *ls_location;
|
|
|
|
result->push_back(*info);
|
2017-12-24 03:23:29 +00:00
|
|
|
return true;
|
2017-12-19 05:31:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 08:56:09 +00:00
|
|
|
struct Ipc_WorkspaceSymbol : public RequestMessage<Ipc_WorkspaceSymbol> {
|
2017-12-06 04:39:44 +00:00
|
|
|
const static IpcId kIpcId = IpcId::WorkspaceSymbol;
|
2018-01-19 08:56:09 +00:00
|
|
|
struct Params {
|
|
|
|
std::string query;
|
|
|
|
};
|
|
|
|
Params params;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-01-19 08:56:09 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_WorkspaceSymbol::Params, query);
|
2017-12-06 04:39:44 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_WorkspaceSymbol, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_WorkspaceSymbol);
|
|
|
|
|
|
|
|
struct Out_WorkspaceSymbol : public lsOutMessage<Out_WorkspaceSymbol> {
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsSymbolInformation> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_WorkspaceSymbol, jsonrpc, id, result);
|
|
|
|
|
2017-12-24 03:23:29 +00:00
|
|
|
///// Fuzzy matching
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct WorkspaceSymbolHandler : BaseMessageHandler<Ipc_WorkspaceSymbol> {
|
|
|
|
void Run(Ipc_WorkspaceSymbol* request) override {
|
|
|
|
Out_WorkspaceSymbol out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-02 01:59:01 +00:00
|
|
|
LOG_S(INFO) << "[querydb] Considering " << db->symbols.size()
|
2017-12-06 03:32:33 +00:00
|
|
|
<< " candidates for query " << request->params.query;
|
|
|
|
|
|
|
|
std::string query = request->params.query;
|
|
|
|
|
|
|
|
std::unordered_set<std::string> inserted_results;
|
2017-12-24 03:23:29 +00:00
|
|
|
// db->detailed_names indices of each lsSymbolInformation in out.result
|
|
|
|
std::vector<int> result_indices;
|
2017-12-24 05:23:01 +00:00
|
|
|
std::vector<lsSymbolInformation> unsorted_results;
|
2018-02-22 05:50:36 +00:00
|
|
|
inserted_results.reserve(config->workspaceSymbol.maxNum);
|
|
|
|
result_indices.reserve(config->workspaceSymbol.maxNum);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-02-13 07:20:08 +00:00
|
|
|
// We use detailed_names without parameters for matching.
|
2018-01-05 18:06:15 +00:00
|
|
|
|
2017-12-24 05:23:01 +00:00
|
|
|
// Find exact substring matches.
|
2018-02-02 01:59:01 +00:00
|
|
|
for (int i = 0; i < db->symbols.size(); ++i) {
|
|
|
|
std::string_view detailed_name = db->GetSymbolDetailedName(i);
|
|
|
|
if (detailed_name.find(query) != std::string::npos) {
|
2017-12-06 03:32:33 +00:00
|
|
|
// Do not show the same entry twice.
|
2018-02-02 01:59:01 +00:00
|
|
|
if (!inserted_results.insert(std::string(detailed_name)).second)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
2017-12-27 15:53:35 +00:00
|
|
|
if (InsertSymbolIntoResult(db, working_files, db->symbols[i],
|
|
|
|
&unsorted_results)) {
|
2017-12-24 03:23:29 +00:00
|
|
|
result_indices.push_back(i);
|
2018-02-22 05:50:36 +00:00
|
|
|
if (unsorted_results.size() >= config->workspaceSymbol.maxNum)
|
2017-12-24 03:23:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 05:23:01 +00:00
|
|
|
// Find subsequence matches.
|
2018-02-22 05:50:36 +00:00
|
|
|
if (unsorted_results.size() < config->workspaceSymbol.maxNum) {
|
2017-12-24 03:23:29 +00:00
|
|
|
std::string query_without_space;
|
|
|
|
query_without_space.reserve(query.size());
|
2017-12-27 15:53:35 +00:00
|
|
|
for (char c : query)
|
2017-12-24 03:23:29 +00:00
|
|
|
if (!isspace(c))
|
|
|
|
query_without_space += c;
|
|
|
|
|
2018-01-31 05:55:53 +00:00
|
|
|
for (int i = 0; i < (int)db->symbols.size(); ++i) {
|
2018-02-13 07:20:08 +00:00
|
|
|
std::string_view detailed_name = db->GetSymbolDetailedName(i);
|
2018-03-18 20:04:59 +00:00
|
|
|
if (CaseFoldingSubsequenceMatch(query_without_space, detailed_name).first) {
|
2017-12-06 03:32:33 +00:00
|
|
|
// Do not show the same entry twice.
|
2018-02-13 07:20:08 +00:00
|
|
|
if (!inserted_results.insert(std::string(detailed_name)).second)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
2017-12-27 15:53:35 +00:00
|
|
|
if (InsertSymbolIntoResult(db, working_files, db->symbols[i],
|
|
|
|
&unsorted_results)) {
|
2017-12-24 03:23:29 +00:00
|
|
|
result_indices.push_back(i);
|
2018-02-22 05:50:36 +00:00
|
|
|
if (unsorted_results.size() >= config->workspaceSymbol.maxNum)
|
2017-12-24 03:23:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 07:19:49 +00:00
|
|
|
if (config->workspaceSymbol.sort && query.size() <= FuzzyMatcher::kMaxPat) {
|
2018-01-07 21:08:18 +00:00
|
|
|
// Sort results with a fuzzy matching algorithm.
|
|
|
|
int longest = 0;
|
|
|
|
for (int i : result_indices)
|
2018-02-13 07:20:08 +00:00
|
|
|
longest = std::max(longest, int(db->GetSymbolDetailedName(i).size()));
|
2018-03-16 07:19:49 +00:00
|
|
|
FuzzyMatcher fuzzy(query);
|
2018-01-07 21:08:18 +00:00
|
|
|
std::vector<std::pair<int, int>> permutation(result_indices.size());
|
|
|
|
for (int i = 0; i < int(result_indices.size()); i++) {
|
|
|
|
permutation[i] = {
|
2018-03-16 07:19:49 +00:00
|
|
|
fuzzy.Match(db->GetSymbolDetailedName(result_indices[i])), i};
|
2018-01-07 21:08:18 +00:00
|
|
|
}
|
|
|
|
std::sort(permutation.begin(), permutation.end(),
|
|
|
|
std::greater<std::pair<int, int>>());
|
|
|
|
out.result.reserve(result_indices.size());
|
2018-03-18 19:17:40 +00:00
|
|
|
// Discard awful candidates.
|
|
|
|
for (int i = 0; i < int(result_indices.size()) &&
|
|
|
|
permutation[i].first > FuzzyMatcher::kMinScore;
|
|
|
|
i++)
|
2018-01-11 02:43:01 +00:00
|
|
|
out.result.push_back(
|
|
|
|
std::move(unsorted_results[permutation[i].second]));
|
|
|
|
} else {
|
2018-01-07 21:08:18 +00:00
|
|
|
out.result.reserve(unsorted_results.size());
|
|
|
|
for (const auto& entry : unsorted_results)
|
|
|
|
out.result.push_back(std::move(entry));
|
2017-12-24 06:49:45 +00:00
|
|
|
}
|
2018-01-07 21:08:18 +00:00
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
LOG_S(INFO) << "[querydb] Found " << out.result.size()
|
|
|
|
<< " results for query " << query;
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::WorkspaceSymbol, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(WorkspaceSymbolHandler);
|
2017-12-24 03:23:29 +00:00
|
|
|
} // namespace
|