[clang-tools-extra] [clangd] Augment code completion results with documentation from the index. (PR #120099)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 22 14:19:31 PST 2024


================
@@ -1863,14 +1863,41 @@ class CodeCompleteFlow {
     CodeCompleteResult Output;
 
     // Convert the results to final form, assembling the expensive strings.
+    // If necessary, search the index for documentation comments.
+    LookupRequest Req;
+    llvm::DenseMap<SymbolID, uint32_t> SymbolToCompletion;
     for (auto &C : Scored) {
       Output.Completions.push_back(toCodeCompletion(C.first));
       Output.Completions.back().Score = C.second;
       Output.Completions.back().CompletionTokenRange = ReplacedRange;
+      if (Opts.Index && !Output.Completions.back().Documentation) {
+        for (auto &Cand : C.first) {
+          if (Cand.SemaResult &&
+              Cand.SemaResult->Kind == CodeCompletionResult::RK_Declaration) {
+            auto ID = clangd::getSymbolID(Cand.SemaResult->getDeclaration());
+            if (!ID)
+              continue;
+            Req.IDs.insert(ID);
+            SymbolToCompletion[ID] = Output.Completions.size() - 1;
+          }
+        }
+      }
     }
     Output.HasMore = Incomplete;
     Output.Context = CCContextKind;
     Output.CompletionRange = ReplacedRange;
+
+    // Look up documentation from the index.
+    if (Opts.Index) {
+      Opts.Index->lookup(Req, [&](const Symbol &S) {
+        auto &C = Output.Completions[SymbolToCompletion.at(S.ID)];
+        if (S.Documentation.empty())
----------------
HighCommander4 wrote:

nit: this early-return can be moved to before the lookup in `SymbolToCompletion`

https://github.com/llvm/llvm-project/pull/120099


More information about the cfe-commits mailing list