[clang-tools-extra] r334089 - [clangd] Boost fuzzy match score by 2x (so a maximum of 2) when the query is the full identifier name.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 6 05:38:38 PDT 2018


Author: sammccall
Date: Wed Jun  6 05:38:37 2018
New Revision: 334089

URL: http://llvm.org/viewvc/llvm-project?rev=334089&view=rev
Log:
[clangd] Boost fuzzy match score by 2x (so a maximum of 2) when the query is the full identifier name.

Summary: Fix a couple of bugs in tests an in Quality to keep tests passing.

Reviewers: ioeric

Subscribers: ilya-biryukov, MaskRay, jkorous, cfe-commits

Differential Revision: https://reviews.llvm.org/D47815

Modified:
    clang-tools-extra/trunk/clangd/FuzzyMatch.cpp
    clang-tools-extra/trunk/clangd/FuzzyMatch.h
    clang-tools-extra/trunk/clangd/Quality.cpp
    clang-tools-extra/trunk/clangd/Quality.h
    clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp
    clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp

Modified: clang-tools-extra/trunk/clangd/FuzzyMatch.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FuzzyMatch.cpp?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/FuzzyMatch.cpp (original)
+++ clang-tools-extra/trunk/clangd/FuzzyMatch.cpp Wed Jun  6 05:38:37 2018
@@ -101,7 +101,13 @@ Optional<float> FuzzyMatcher::match(Stri
                        Scores[PatN][WordN][Match].Score);
   if (isAwful(Best))
     return None;
-  return ScoreScale * std::min(PerfectBonus * PatN, std::max<int>(0, Best));
+  float Score =
+      ScoreScale * std::min(PerfectBonus * PatN, std::max<int>(0, Best));
+  // If the pattern is as long as the word, we have an exact string match,
+  // since every pattern character must match something.
+  if (WordN == PatN)
+    Score *= 2; // May not be perfect 2 if case differs in a significant way.
+  return Score;
 }
 
 // Segmentation of words and patterns.

Modified: clang-tools-extra/trunk/clangd/FuzzyMatch.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FuzzyMatch.h?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/FuzzyMatch.h (original)
+++ clang-tools-extra/trunk/clangd/FuzzyMatch.h Wed Jun  6 05:38:37 2018
@@ -31,7 +31,9 @@ public:
   // Characters beyond MaxPat are ignored.
   FuzzyMatcher(llvm::StringRef Pattern);
 
-  // If Word matches the pattern, return a score in [0,1] (higher is better).
+  // If Word matches the pattern, return a score indicating the quality match.
+  // Scores usually fall in a [0,1] range, with 1 being a very good score.
+  // "Super" scores in (1,2] are possible if the pattern is the full word.
   // Characters beyond MaxWord are ignored.
   llvm::Optional<float> match(llvm::StringRef Word);
 

Modified: clang-tools-extra/trunk/clangd/Quality.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Quality.cpp?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Quality.cpp (original)
+++ clang-tools-extra/trunk/clangd/Quality.cpp Wed Jun  6 05:38:37 2018
@@ -131,6 +131,8 @@ float SymbolQualitySignals::evaluate() c
       Score *= 1.1;
       break;
     case Namespace:
+      Score *= 0.8;
+      break;
     case Macro:
       Score *= 0.2;
       break;

Modified: clang-tools-extra/trunk/clangd/Quality.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Quality.h?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Quality.h (original)
+++ clang-tools-extra/trunk/clangd/Quality.h Wed Jun  6 05:38:37 2018
@@ -70,7 +70,7 @@ llvm::raw_ostream &operator<<(llvm::raw_
 
 /// Attributes of a symbol-query pair that affect how much we like it.
 struct SymbolRelevanceSignals {
-  /// 0-1 fuzzy-match score for unqualified name. Must be explicitly assigned.
+  /// 0-1+ fuzzy-match score for unqualified name. Must be explicitly assigned.
   float NameMatch = 1;
   bool Forbidden = false; // Unavailable (e.g const) or inaccessible (private).
   /// Proximity between best declaration and the query. [0-1], 1 is closest.

Modified: clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp Wed Jun  6 05:38:37 2018
@@ -273,10 +273,10 @@ TEST_F(WorkspaceSymbolsTest, WithLimit)
       #include "foo.h"
       )cpp");
   EXPECT_THAT(getSymbols("foo"),
-              ElementsAre(AllOf(Named("foo"), InContainer(""),
-                                WithKind(SymbolKind::Variable)),
-                          AllOf(Named("foo2"), InContainer(""),
-                                WithKind(SymbolKind::Variable))));
+              UnorderedElementsAre(AllOf(Named("foo"), InContainer(""),
+                                         WithKind(SymbolKind::Variable)),
+                                   AllOf(Named("foo2"), InContainer(""),
+                                         WithKind(SymbolKind::Variable))));
 
   Limit = 1;
   EXPECT_THAT(getSymbols("foo"),

Modified: clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp?rev=334089&r1=334088&r2=334089&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/FuzzyMatchTests.cpp Wed Jun  6 05:38:37 2018
@@ -46,10 +46,14 @@ private:
 
 struct MatchesMatcher : public testing::MatcherInterface<StringRef> {
   ExpectedMatch Candidate;
-  MatchesMatcher(ExpectedMatch Candidate) : Candidate(std::move(Candidate)) {}
+  Optional<float> Score;
+  MatchesMatcher(ExpectedMatch Candidate, Optional<float> Score)
+      : Candidate(std::move(Candidate)), Score(Score) {}
 
   void DescribeTo(::std::ostream *OS) const override {
     raw_os_ostream(*OS) << "Matches " << Candidate;
+    if (Score)
+      *OS << " with score " << *Score;
   }
 
   bool MatchAndExplain(StringRef Pattern,
@@ -60,14 +64,15 @@ struct MatchesMatcher : public testing::
     FuzzyMatcher Matcher(Pattern);
     auto Result = Matcher.match(Candidate.Word);
     auto AnnotatedMatch = Matcher.dumpLast(*OS << "\n");
-    return Result && Candidate.accepts(AnnotatedMatch);
+    return Result && Candidate.accepts(AnnotatedMatch) &&
+           (!Score || testing::Value(*Result, testing::FloatEq(*Score)));
   }
 };
 
-// Accepts patterns that match a given word.
+// Accepts patterns that match a given word, optionally requiring a score.
 // Dumps the debug tables on match failure.
-testing::Matcher<StringRef> matches(StringRef M) {
-  return testing::MakeMatcher<StringRef>(new MatchesMatcher(M));
+testing::Matcher<StringRef> matches(StringRef M, Optional<float> Score = {}) {
+  return testing::MakeMatcher<StringRef>(new MatchesMatcher(M, Score));
 }
 
 TEST(FuzzyMatch, Matches) {
@@ -239,7 +244,7 @@ TEST(FuzzyMatch, Ranking) {
               ranks("[onMess]age", "[onmess]age", "[on]This[M]ega[Es]cape[s]"));
   EXPECT_THAT("CC", ranks("[C]amel[C]ase", "[c]amel[C]ase"));
   EXPECT_THAT("cC", ranks("[c]amel[C]ase", "[C]amel[C]ase"));
-  EXPECT_THAT("p", ranks("[p]arse", "[p]osix", "[p]afdsa", "[p]ath", "[p]"));
+  EXPECT_THAT("p", ranks("[p]", "[p]arse", "[p]osix", "[p]afdsa", "[p]ath"));
   EXPECT_THAT("pa", ranks("[pa]rse", "[pa]th", "[pa]fdsa"));
   EXPECT_THAT("log", ranks("[log]", "Scroll[Log]icalPosition"));
   EXPECT_THAT("e", ranks("[e]lse", "Abstract[E]lement"));
@@ -262,6 +267,15 @@ TEST(FuzzyMatch, Ranking) {
                              "[c]ss.co[lo]rDecorator[s].[e]nable"));
 }
 
+// Verify some bounds so we know scores fall in the right range.
+// Testing exact scores is fragile, so we prefer Ranking tests.
+TEST(FuzzyMatch, Scoring) {
+  EXPECT_THAT("abs", matches("[a]x[b]y[s]z", 0.f));
+  EXPECT_THAT("abs", matches("[abs]l", 1.f));
+  EXPECT_THAT("abs", matches("[abs]", 2.f));
+  EXPECT_THAT("Abs", matches("[abs]", 2.f));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang




More information about the cfe-commits mailing list