mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-31 18:00:26 +00:00
Filter some completion results based on the input.
This commit is contained in:
parent
3df71f4145
commit
a84c863e5e
@ -50,10 +50,35 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto &items = complete_response->result.items;
|
||||||
|
|
||||||
|
// If the text doesn't start with underscore,
|
||||||
|
// remove all candidates that start with underscore.
|
||||||
|
if (!complete_text.empty() && complete_text[0] != '_') {
|
||||||
|
items.erase(std::remove_if(items.begin(), items.end(),
|
||||||
|
[](const lsCompletionItem& item) {
|
||||||
|
return item.label[0] == '_';
|
||||||
|
}), items.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the exact text
|
||||||
|
const bool found = !complete_text.empty() &&
|
||||||
|
std::find_if(items.begin(), items.end(),
|
||||||
|
[&](const lsCompletionItem& item) {
|
||||||
|
return item.label == complete_text;
|
||||||
|
}) != items.end();
|
||||||
|
// If found, remove all candidates that do not start with it.
|
||||||
|
if (found) {
|
||||||
|
items.erase(std::remove_if(items.begin(), items.end(),
|
||||||
|
[&](const lsCompletionItem& item) {
|
||||||
|
return item.label.find(complete_text) != 0;
|
||||||
|
}), items.end());
|
||||||
|
}
|
||||||
|
|
||||||
const size_t kMaxResultSize = 100u;
|
const size_t kMaxResultSize = 100u;
|
||||||
if (complete_response->result.items.size() > kMaxResultSize) {
|
if (items.size() > kMaxResultSize) {
|
||||||
if (complete_text.empty()) {
|
if (complete_text.empty()) {
|
||||||
complete_response->result.items.resize(kMaxResultSize);
|
items.resize(kMaxResultSize);
|
||||||
} else {
|
} else {
|
||||||
std::vector<lsCompletionItem> filtered_result;
|
std::vector<lsCompletionItem> filtered_result;
|
||||||
filtered_result.reserve(kMaxResultSize);
|
filtered_result.reserve(kMaxResultSize);
|
||||||
@ -62,7 +87,7 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
|
|||||||
inserted.reserve(kMaxResultSize);
|
inserted.reserve(kMaxResultSize);
|
||||||
|
|
||||||
// Find literal matches first.
|
// Find literal matches first.
|
||||||
for (const lsCompletionItem& item : complete_response->result.items) {
|
for (const auto& item : items) {
|
||||||
if (item.label.find(complete_text) != std::string::npos) {
|
if (item.label.find(complete_text) != std::string::npos) {
|
||||||
// Don't insert the same completion entry.
|
// Don't insert the same completion entry.
|
||||||
if (!inserted.insert(item.InsertedContent()).second)
|
if (!inserted.insert(item.InsertedContent()).second)
|
||||||
@ -76,7 +101,7 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
|
|||||||
|
|
||||||
// Find fuzzy matches if we haven't found all of the literal matches.
|
// Find fuzzy matches if we haven't found all of the literal matches.
|
||||||
if (filtered_result.size() < kMaxResultSize) {
|
if (filtered_result.size() < kMaxResultSize) {
|
||||||
for (const lsCompletionItem& item : complete_response->result.items) {
|
for (const auto& item : items) {
|
||||||
if (SubstringMatch(complete_text, item.label)) {
|
if (SubstringMatch(complete_text, item.label)) {
|
||||||
// Don't insert the same completion entry.
|
// Don't insert the same completion entry.
|
||||||
if (!inserted.insert(item.InsertedContent()).second)
|
if (!inserted.insert(item.InsertedContent()).second)
|
||||||
@ -89,14 +114,14 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
complete_response->result.items = filtered_result;
|
items = filtered_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assuming the client does not support out-of-order completion (ie, ao
|
// Assuming the client does not support out-of-order completion (ie, ao
|
||||||
// matches against oa), our filtering is guaranteed to contain any
|
// matches against oa), our filtering is guaranteed to contain any
|
||||||
// potential matches, so the completion is only incomplete if we have the
|
// potential matches, so the completion is only incomplete if we have the
|
||||||
// max number of emitted matches.
|
// max number of emitted matches.
|
||||||
if (complete_response->result.items.size() >= kMaxResultSize) {
|
if (items.size() >= kMaxResultSize) {
|
||||||
LOG_S(INFO) << "Marking completion results as incomplete";
|
LOG_S(INFO) << "Marking completion results as incomplete";
|
||||||
complete_response->result.isIncomplete = true;
|
complete_response->result.isIncomplete = true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user