[PATCH] D52276: [clangd] Add type boosting in code completion

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 19 12:19:09 PDT 2018


ilya-biryukov created this revision.
ilya-biryukov added reviewers: sammccall, ioeric.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay.
ilya-biryukov added a dependency: D52274: [clangd] Collect and store expected types in the index.
ilya-biryukov planned changes to this revision.
ilya-biryukov added a comment.

Ranking-related code should be moved to `Quality.cpp`


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52276

Files:
  clangd/CodeComplete.cpp
  clangd/Quality.cpp
  clangd/Quality.h


Index: clangd/Quality.h
===================================================================
--- clangd/Quality.h
+++ clangd/Quality.h
@@ -27,7 +27,7 @@
 
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H
-
+#include "ExpectedTypes.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
@@ -90,6 +90,8 @@
   /// This is used to calculate proximity between the index symbol and the
   /// query.
   llvm::StringRef SymbolURI;
+  /// A multiplier to be applied due to matching type.
+  float TypeMatchMultiplier = 1.0;
   /// Proximity between best declaration and the query. [0-1], 1 is closest.
   /// FIXME: unify with index proximity score - signals should be
   /// source-independent.
Index: clangd/Quality.cpp
===================================================================
--- clangd/Quality.cpp
+++ clangd/Quality.cpp
@@ -348,6 +348,8 @@
   if (NeedsFixIts)
     Score *= 0.5;
 
+  Score *= TypeMatchMultiplier;
+
   return Score;
 }
 
Index: clangd/CodeComplete.cpp
===================================================================
--- clangd/CodeComplete.cpp
+++ clangd/CodeComplete.cpp
@@ -23,6 +23,7 @@
 #include "CodeCompletionStrings.h"
 #include "Compiler.h"
 #include "Diagnostics.h"
+#include "ExpectedTypes.h"
 #include "FileDistance.h"
 #include "FuzzyMatch.h"
 #include "Headers.h"
@@ -1224,6 +1225,7 @@
   bool Incomplete = false; // Would more be available with a higher limit?
   llvm::Optional<FuzzyMatcher> Filter;       // Initialized once Sema runs.
   std::vector<std::string> QueryScopes;      // Initialized once Sema runs.
+  llvm::DenseMap<SType, float> ExpectedTypes; // Initialized once Sema runs.
   // Include-insertion and proximity scoring rely on the include structure.
   // This is available after Sema has run.
   llvm::Optional<IncludeInserter> Inserter;  // Available during runWithSema.
@@ -1349,6 +1351,10 @@
         Recorder->CCSema->getPreprocessor().getCodeCompletionFilter());
     QueryScopes = getQueryScopes(Recorder->CCContext,
                                  Recorder->CCSema->getSourceManager());
+    ExpectedTypes =
+        SType::forCopyInitOf(Recorder->CCSema->getASTContext(),
+                             Recorder->CCContext.getPreferredType());
+    vlog("Added {0} expected types", ExpectedTypes.size());
     // Sema provides the needed context to query the index.
     // FIXME: in addition to querying for extra/overlapping symbols, we should
     //        explicitly request symbols corresponding to Sema results.
@@ -1511,10 +1517,32 @@
         Relevance.merge(*Candidate.IndexResult);
         Origin |= Candidate.IndexResult->Origin;
         FromIndex = true;
+        if (!ExpectedTypes.empty()) {
+          // FIXME(ibiryukov): this should live in Quality.cpp
+          auto TypeMatchMult =
+              typesMatch(ExpectedTypes, Candidate.IndexResult->Types);
+          if (TypeMatchMult) {
+            vlog("Types matched in sema for {0}", Candidate.Name);
+            Relevance.TypeMatchMultiplier =
+                std::max(*TypeMatchMult, Relevance.TypeMatchMultiplier);
+          }
+        }
       }
       if (Candidate.SemaResult) {
         Quality.merge(*Candidate.SemaResult);
         Relevance.merge(*Candidate.SemaResult);
+        if (!ExpectedTypes.empty()) {
+          // FIXME(ibiryukov): this should live in Quality.cpp
+          auto TypeMatchMult = typesMatch(
+              ExpectedTypes,
+              SType::fromCompletionResult(Recorder->CCSema->getASTContext(),
+                                          *Candidate.SemaResult));
+          if (TypeMatchMult) {
+            vlog("Types matched in sema for {0}", Candidate.Name);
+            Relevance.TypeMatchMultiplier =
+                std::max(*TypeMatchMult, Relevance.TypeMatchMultiplier);
+          }
+        }
         Origin |= SymbolOrigin::AST;
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52276.166168.patch
Type: text/x-patch
Size: 3960 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180919/12db6d82/attachment.bin>


More information about the cfe-commits mailing list