https://github.com/bevin-hansson updated https://github.com/llvm/llvm-project/pull/120099
>From 16bd4a00feec97ae65a14600caf6921045d54833 Mon Sep 17 00:00:00 2001 From: Bevin Hansson <bevin.hans...@ericsson.com> Date: Mon, 16 Dec 2024 16:40:06 +0100 Subject: [PATCH 1/2] [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; } >From ace8121506029e9016df9ec3bffa9d465334e97d Mon Sep 17 00:00:00 2001 From: Bevin Hansson <bevin.hans...@ericsson.com> Date: Tue, 17 Dec 2024 08:57:52 +0100 Subject: [PATCH 2/2] Add test. --- .../clangd/unittests/CodeCompleteTests.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp index 3acacf496e77f9..827b64c882d583 100644 --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -1101,6 +1101,43 @@ int x = foo^ Contains(AllOf(named("foo"), doc("This comment should be retained!")))); } +TEST(CompletionTest, CommentsOnMembers) { + MockFS FS; + MockCompilationDatabase CDB; + + auto Opts = ClangdServer::optsForTest(); + Opts.BuildDynamicSymbolIndex = true; + + ClangdServer Server(CDB, FS, Opts); + + FS.Files[testPath("foo.h")] = R"cpp( + struct alpha { + /// This is a member field. + int gamma; + + /// This is a member function. + int delta(); + }; + )cpp"; + + auto File = testPath("foo.cpp"); + Annotations Test(R"cpp( +#include "foo.h" +alpha a; +int x = a.^ + )cpp"); + runAddDocument(Server, File, Test.code()); + auto CompletionList = + llvm::cantFail(runCodeComplete(Server, File, Test.point(), {})); + + EXPECT_THAT( + CompletionList.Completions, + Contains(AllOf(named("gamma"), doc("This is a member field.")))); + EXPECT_THAT( + CompletionList.Completions, + Contains(AllOf(named("delta"), doc("This is a member function.")))); +} + TEST(CompletionTest, GlobalCompletionFiltering) { Symbol Class = cls("XYZ"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits