Add strict to FuzzyMatcher::Match

In completion, underscore prefixed builtin macros may be annoying when the first type character is not an underscore.

When `strict` is true, `Match` enforces the first characters should be loosely of the same category.
This commit is contained in:
Fangrui Song 2018-12-20 00:00:42 -08:00
parent 836caba19b
commit 6e8a45b25e
4 changed files with 9 additions and 5 deletions

View File

@ -95,7 +95,9 @@ FuzzyMatcher::FuzzyMatcher(std::string_view pattern, int sensitivity) {
}
}
int FuzzyMatcher::Match(std::string_view text) {
int FuzzyMatcher::Match(std::string_view text, bool strict) {
if (pat.empty() != text.empty())
return kMinScore;
int n = int(text.size());
if (n > kMaxText)
return kMinScore + 1;
@ -103,6 +105,8 @@ int FuzzyMatcher::Match(std::string_view text) {
for (int i = 0; i < n; i++)
low_text[i] = (char)::tolower(text[i]);
CalculateRoles(text, text_role, &text_set);
if (strict && n && !!pat_role[0] != !!text_role[0])
return kMinScore;
dp[0][0][0] = dp[0][0][1] = 0;
for (int j = 0; j < n; j++) {
dp[0][j + 1][0] = dp[0][j][0] + MissScore(j, false);

View File

@ -17,7 +17,7 @@ public:
constexpr static int kMinScore = INT_MIN / 4;
FuzzyMatcher(std::string_view pattern, int case_sensitivity);
int Match(std::string_view text);
int Match(std::string_view text, bool strict);
private:
int case_sensitivity;

View File

@ -163,7 +163,7 @@ void FilterCandidates(CompletionList &result, const std::string &complete_text,
const std::string &filter =
item.filterText.size() ? item.filterText : item.label;
item.score_ = ReverseSubseqMatch(complete_text, filter, sensitive) >= 0
? fuzzy.Match(filter)
? fuzzy.Match(filter, true)
: FuzzyMatcher::kMinScore;
}
items.erase(std::remove_if(items.begin(), items.end(),

View File

@ -168,8 +168,8 @@ done_add:
longest, int(db->GetSymbolName(std::get<2>(cand), true).size()));
FuzzyMatcher fuzzy(query, g_config->workspaceSymbol.caseSensitivity);
for (auto &cand : cands)
std::get<1>(cand) =
fuzzy.Match(db->GetSymbolName(std::get<2>(cand), std::get<1>(cand)));
std::get<1>(cand) = fuzzy.Match(
db->GetSymbolName(std::get<2>(cand), std::get<1>(cand)), false);
std::sort(cands.begin(), cands.end(), [](const auto &l, const auto &r) {
return std::get<1>(l) > std::get<1>(r);
});