diff --git a/clang-tools-extra/clangd/Quality.cpp b/clang-tools-extra/clangd/Quality.cpp index 0d2175eb00cd..6d0c37625af6 100644 --- a/clang-tools-extra/clangd/Quality.cpp +++ b/clang-tools-extra/clangd/Quality.cpp @@ -299,6 +299,7 @@ void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) { : 0.6; SemaFileProximityScore = std::max(DeclProximity, SemaFileProximityScore); IsInstanceMember |= isInstanceMember(SemaCCResult.Declaration); + InBaseClass |= SemaCCResult.InBaseClass; } // Declarations are scoped, others (like macros) are assumed global. @@ -372,9 +373,12 @@ float SymbolRelevanceSignals::evaluate() const { if (!IsInstanceMember && (Context == CodeCompletionContext::CCC_DotMemberAccess || Context == CodeCompletionContext::CCC_ArrowMemberAccess)) { - Score *= 0.5; + Score *= 0.2; } + if (InBaseClass) + Score *= 0.5; + // Penalize for FixIts. if (NeedsFixIts) Score *= 0.5; diff --git a/clang-tools-extra/clangd/Quality.h b/clang-tools-extra/clangd/Quality.h index 36d87d82d44a..d71855640777 100644 --- a/clang-tools-extra/clangd/Quality.h +++ b/clang-tools-extra/clangd/Quality.h @@ -87,6 +87,7 @@ struct SymbolRelevanceSignals { bool Forbidden = false; // Unavailable (e.g const) or inaccessible (private). /// Whether fixits needs to be applied for that completion or not. bool NeedsFixIts = false; + bool InBaseClass = false; // A member from base class of the accessed class. URIDistance *FileProximityMatch = nullptr; /// These are used to calculate proximity between the index symbol and the diff --git a/clang-tools-extra/unittests/clangd/QualityTests.cpp b/clang-tools-extra/unittests/clangd/QualityTests.cpp index 7c6e63d909b4..9df895cd7399 100644 --- a/clang-tools-extra/unittests/clangd/QualityTests.cpp +++ b/clang-tools-extra/unittests/clangd/QualityTests.cpp @@ -185,6 +185,13 @@ TEST(QualityTests, SymbolRelevanceSignalExtraction) { Relevance = {}; Relevance.merge(CodeCompletionResult(&findDecl(AST, "S::S"), 42)); EXPECT_EQ(Relevance.Scope, SymbolRelevanceSignals::GlobalScope); + + Relevance = {}; + EXPECT_FALSE(Relevance.InBaseClass); + auto BaseMember = CodeCompletionResult(&findAnyDecl(AST, "y"), 42); + BaseMember.InBaseClass = true; + Relevance.merge(BaseMember); + EXPECT_TRUE(Relevance.InBaseClass); } // Do the signals move the scores in the direction we expect? @@ -276,6 +283,10 @@ TEST(QualityTests, SymbolRelevanceSignalsSanity) { EXPECT_LT(Instance.evaluate(), Default.evaluate()); Instance.IsInstanceMember = true; EXPECT_EQ(Instance.evaluate(), Default.evaluate()); + + SymbolRelevanceSignals InBaseClass; + InBaseClass.InBaseClass = true; + EXPECT_LT(InBaseClass.evaluate(), Default.evaluate()); } TEST(QualityTests, ScopeProximity) {