[clang-tools-extra] [clangd] Augment code completion results with documentation from the index. (PR #120099)
Bevin Hansson via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 16 07:45:43 PST 2024
https://github.com/bevin-hansson created https://github.com/llvm/llvm-project/pull/120099
When looking up code completions from Sema, there is no associated
documentation. This is due to crash issues with stale preambles.
However, this also means that code completion results from other
than the main file do not have documentation in certain cases,
which is a bad user experience.
This patch performs a lookup into the index using the code
completion result declarations to find documentation, and
attaches it to the results.
This fixes clangd/clangd#2252 and clangd/clangd#564.
>From 16bd4a00feec97ae65a14600caf6921045d54833 Mon Sep 17 00:00:00 2001
From: Bevin Hansson <bevin.hansson at ericsson.com>
Date: Mon, 16 Dec 2024 16:40:06 +0100
Subject: [PATCH] [clangd] Augment code completion results with documentation
from the index.
When looking up code completions from Sema, there is no associated
documentation. This is due to crash issues with stale preambles.
However, this also means that code completion results from other
than the main file do not have documentation in certain cases,
which is a bad user experience.
This patch performs a lookup into the index using the code
completion result declarations to find documentation, and
attaches it to the results.
This fixes clangd/clangd#2252 and clangd/clangd#564.
---
clang-tools-extra/clangd/CodeComplete.cpp | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 2c2d5f0b5ac924..6916a32b7c2bac 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -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())
+ return;
+ C.Documentation.emplace();
+ parseDocumentation(S.Documentation, *C.Documentation);
+ });
+ }
+
return Output;
}
More information about the cfe-commits
mailing list