From 18e4be616c8edefd975af524a72923c7183a0197 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 20 Dec 2018 00:00:42 -0800 Subject: [PATCH] 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. --- src/fuzzy_match.cc | 6 +++++- src/fuzzy_match.hh | 2 +- src/messages/textDocument_completion.cc | 2 +- src/messages/workspace.cc | 4 ++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/fuzzy_match.cc b/src/fuzzy_match.cc index 680aa4ac..7f70b168 100644 --- a/src/fuzzy_match.cc +++ b/src/fuzzy_match.cc @@ -107,7 +107,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; @@ -115,6 +117,8 @@ int FuzzyMatcher::Match(std::string_view text) { for (int i = 0; i < n; i++) low_text[i] = ::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); diff --git a/src/fuzzy_match.hh b/src/fuzzy_match.hh index 9153c6c0..a7a98a9e 100644 --- a/src/fuzzy_match.hh +++ b/src/fuzzy_match.hh @@ -29,7 +29,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; diff --git a/src/messages/textDocument_completion.cc b/src/messages/textDocument_completion.cc index 3eb76b3a..be84bad7 100644 --- a/src/messages/textDocument_completion.cc +++ b/src/messages/textDocument_completion.cc @@ -175,7 +175,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(), diff --git a/src/messages/workspace.cc b/src/messages/workspace.cc index 5661a5d6..5b049958 100644 --- a/src/messages/workspace.cc +++ b/src/messages/workspace.cc @@ -180,8 +180,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); });