Optimize textDocument/definition for comments

This commit is contained in:
Jacob Dufault 2018-02-13 10:23:56 -08:00
parent aae6f456de
commit 2fab426369

View File

@ -133,21 +133,26 @@ struct TextDocumentDefinitionHandler
const std::string& buffer = working_file->buffer_content; const std::string& buffer = working_file->buffer_content;
std::string query = LexWordAroundPos(request->params.position, buffer); std::string query = LexWordAroundPos(request->params.position, buffer);
int best_score = kMinScore; int best_score = INT_MAX;
int best_i = 0; int best_i = -1;
std::vector<int> score, dp;
for (int i = 0; i < (int)db->symbols.size(); ++i) { for (int i = 0; i < (int)db->symbols.size(); ++i) {
if (db->symbols[i].kind == SymbolKind::Invalid)
continue;
std::string_view detailed_name = db->GetSymbolDetailedName(i); std::string_view detailed_name = db->GetSymbolDetailedName(i);
if (detailed_name.size() > score.size()) { size_t idx = detailed_name.find(query);
score.resize(detailed_name.size()); if (idx == std::string::npos)
dp.resize(detailed_name.size()); continue;
}
int t = FuzzyEvaluate(query, detailed_name, score, dp); int score = detailed_name.size() - query.size();
if (t > best_score) { assert(score >= 0);
best_score = t; if (score < best_score) {
best_score = score;
best_i = i; best_i = i;
} }
if (score == 0)
break;
} }
if (best_i != -1) {
Maybe<Use> use = GetDefinitionSpellingOfSymbol(db, db->symbols[best_i]); Maybe<Use> use = GetDefinitionSpellingOfSymbol(db, db->symbols[best_i]);
if (use) { if (use) {
optional<lsLocation> ls_loc = GetLsLocation(db, working_files, *use); optional<lsLocation> ls_loc = GetLsLocation(db, working_files, *use);
@ -156,6 +161,7 @@ struct TextDocumentDefinitionHandler
} }
} }
} }
}
QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out); QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out);
} }