Better order of the completion items.

This commit is contained in:
Chao Shen 2017-12-30 15:15:46 +08:00
parent 11aa09fac2
commit 829b2fe18c
5 changed files with 43 additions and 15 deletions

View File

@ -46,6 +46,18 @@ unsigned GetCompletionPriority(const CXCompletionString& str,
return priority; return priority;
} }
bool CompareLsCompletionItem(const lsCompletionItem &item1,
const lsCompletionItem &item2)
{
if (item1.pos_ != item2.pos_)
return item1.pos_ < item2.pos_;
if (item1.priority_ != item2.priority_)
return item1.priority_ < item2.priority_;
if (item1.label.length() != item2.label.length())
return item1.label.length() < item2.label.length();
return item1.label < item2.label;
}
template <typename T> template <typename T>
char* tofixedbase64(T input, char* out) { char* tofixedbase64(T input, char* out) {
const char* digits = const char* digits =
@ -464,15 +476,22 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
ls_completion_item.documentation = ToString( ls_completion_item.documentation = ToString(
clang_getCompletionBriefComment(result.CompletionString)); clang_getCompletionBriefComment(result.CompletionString));
char buf[16]; ls_completion_item.pos_ =
ls_completion_item.sortText = ls_completion_item.label.find(request->existing_text);
tofixedbase64(GetCompletionPriority(result.CompletionString,
result.CursorKind, ls_completion_item.priority_ =
ls_completion_item.label), GetCompletionPriority(result.CompletionString, result.CursorKind,
buf); ls_completion_item.label);
ls_result.push_back(ls_completion_item); ls_result.push_back(ls_completion_item);
} }
// order all items and set |sortText|
std::sort(ls_result.begin(), ls_result.end(), CompareLsCompletionItem);
char buf[16];
for (size_t i = 0; i < ls_result.size(); ++i)
ls_result[i].sortText = tofixedbase64(i, buf);
timer.ResetAndPrint("[complete] Building " + timer.ResetAndPrint("[complete] Building " +
std::to_string(ls_result.size()) + std::to_string(ls_result.size()) +
" completion results"); " completion results");
@ -569,6 +588,7 @@ ClangCompleteManager::~ClangCompleteManager() {}
void ClangCompleteManager::CodeComplete( void ClangCompleteManager::CodeComplete(
const lsTextDocumentPositionParams& completion_location, const lsTextDocumentPositionParams& completion_location,
const std::string &existing_text,
const OnComplete& on_complete) { const OnComplete& on_complete) {
completion_request_.WithLock( completion_request_.WithLock(
[&](std::unique_ptr<CompletionRequest>& request_storage) { [&](std::unique_ptr<CompletionRequest>& request_storage) {
@ -579,6 +599,7 @@ void ClangCompleteManager::CodeComplete(
// Make the request send out code completion information. // Make the request send out code completion information.
request_storage->document = completion_location.textDocument; request_storage->document = completion_location.textDocument;
request_storage->position = completion_location.position; request_storage->position = completion_location.position;
request_storage->existing_text = existing_text;
request_storage->on_complete = on_complete; request_storage->on_complete = on_complete;
}); });
} }

View File

@ -57,6 +57,7 @@ struct ClangCompleteManager {
struct CompletionRequest { struct CompletionRequest {
lsTextDocumentIdentifier document; lsTextDocumentIdentifier document;
optional<lsPosition> position; optional<lsPosition> position;
std::string existing_text;
OnComplete on_complete; // May be null/empty. OnComplete on_complete; // May be null/empty.
bool emit_diagnostics = false; bool emit_diagnostics = false;
}; };
@ -71,6 +72,7 @@ struct ClangCompleteManager {
// Start a code completion at the given location. |on_complete| will run when // Start a code completion at the given location. |on_complete| will run when
// completion results are available. |on_complete| may run on any thread. // completion results are available. |on_complete| may run on any thread.
void CodeComplete(const lsTextDocumentPositionParams& completion_location, void CodeComplete(const lsTextDocumentPositionParams& completion_location,
const std::string &existing_text,
const OnComplete& on_complete); const OnComplete& on_complete);
// Request a diagnostics update. // Request a diagnostics update.
void DiagnosticsUpdate(const lsTextDocumentIdentifier& document); void DiagnosticsUpdate(const lsTextDocumentIdentifier& document);

View File

@ -356,6 +356,10 @@ struct lsCompletionItem {
// A human-readable string that represents a doc-comment. // A human-readable string that represents a doc-comment.
std::string documentation; std::string documentation;
// Internal information to order candidates.
std::string::size_type pos_;
unsigned priority_;
// A string that shoud be used when comparing this item // A string that shoud be used when comparing this item
// with other items. When `falsy` the label is used. // with other items. When `falsy` the label is used.
std::string sortText; std::string sortText;

View File

@ -69,15 +69,16 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
// find the exact text // find the exact text
const bool found = !complete_text.empty() && const bool found = !complete_text.empty() &&
std::find_if(items.begin(), items.end(), std::any_of(items.begin(), items.end(),
[&](const lsCompletionItem& item) { [&](const lsCompletionItem& item) {
return item.label == complete_text; return item.pos_ == 0 &&
}) != items.end(); item.label.length() == complete_text.length();
});
// If found, remove all candidates that do not start with it. // If found, remove all candidates that do not start with it.
if (found) { if (found) {
items.erase(std::remove_if(items.begin(), items.end(), items.erase(std::remove_if(items.begin(), items.end(),
[&](const lsCompletionItem& item) { [&](const lsCompletionItem& item) {
return item.label.find(complete_text) != 0; return item.pos_ != 0;
}), }),
items.end()); items.end());
} }
@ -242,7 +243,7 @@ struct TextDocumentCompletionHandler : MessageHandler {
callback(global_code_complete_cache->cached_results_, callback(global_code_complete_cache->cached_results_,
true /*is_cached_result*/); true /*is_cached_result*/);
}); });
clang_complete->CodeComplete(request->params, freshen_global); clang_complete->CodeComplete(request->params, existing_completion, freshen_global);
} else if (non_global_code_complete_cache->IsCacheValid( } else if (non_global_code_complete_cache->IsCacheValid(
request->params)) { request->params)) {
non_global_code_complete_cache->WithLock([&]() { non_global_code_complete_cache->WithLock([&]() {
@ -250,7 +251,7 @@ struct TextDocumentCompletionHandler : MessageHandler {
true /*is_cached_result*/); true /*is_cached_result*/);
}); });
} else { } else {
clang_complete->CodeComplete(request->params, callback); clang_complete->CodeComplete(request->params, existing_completion, callback);
} }
} }
} }

View File

@ -160,7 +160,7 @@ struct TextDocumentSignatureHelpHandler : MessageHandler {
callback(signature_cache->cached_results_, true /*is_cached_result*/); callback(signature_cache->cached_results_, true /*is_cached_result*/);
}); });
} else { } else {
clang_complete->CodeComplete(params, std::move(callback)); clang_complete->CodeComplete(params, search, std::move(callback));
} }
} }
}; };