Index: clangd/CodeComplete.cpp =================================================================== --- clangd/CodeComplete.cpp +++ clangd/CodeComplete.cpp @@ -864,6 +864,8 @@ SymbolSlab::Builder ResultsBuilder; // Build the query. FuzzyFindRequest Req; + if (Opts.Limit) + Req.MaxCandidateCount = Opts.Limit; Req.Query = Filter->pattern(); Req.Scopes = getQueryScopes(Recorder.CCContext, Recorder.CCSema->getSourceManager()); Index: clangd/index/Merge.cpp =================================================================== --- clangd/index/Merge.cpp +++ clangd/index/Merge.cpp @@ -50,7 +50,7 @@ for (const Symbol &S : Dyn) if (!SeenDynamicSymbols.count(S.ID)) Callback(S); - return More; + return !More; // returning true indicates the result is complete. } private: Index: clangd/tool/ClangdMain.cpp =================================================================== --- clangd/tool/ClangdMain.cpp +++ clangd/tool/ClangdMain.cpp @@ -124,6 +124,12 @@ "eventually. Don't rely on it."), llvm::cl::init(""), llvm::cl::Hidden); +static llvm::cl::opt LimitCompletionResult( + "limit-completion-results", + llvm::cl::desc("Limit the number of completion results returned by clangd. " + "0 means no limit."), + llvm::cl::init(100), llvm::cl::Hidden); + int main(int argc, char *argv[]) { llvm::cl::ParseCommandLineOptions(argc, argv, "clangd"); @@ -215,6 +221,7 @@ clangd::CodeCompleteOptions CCOpts; CCOpts.EnableSnippets = EnableSnippets; CCOpts.IncludeIneligibleResults = IncludeIneligibleResults; + CCOpts.Limit = LimitCompletionResult; // Initialize and run ClangdLSPServer. ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory, CCOpts, ResourceDirRef, CompileCommandsDirPath, Index: unittests/clangd/CodeCompleteTests.cpp =================================================================== --- unittests/clangd/CodeCompleteTests.cpp +++ unittests/clangd/CodeCompleteTests.cpp @@ -529,6 +529,18 @@ UnorderedElementsAre(Named("local"), Named("Index"), Named("both"))); } +TEST(CompletionTest, SemaIndexMergeWithLimit) { + clangd::CodeCompleteOptions Opts; + Opts.Limit = 1; + auto Results = completions( + R"cpp( + namespace ns { int local; void both(); } + void f() { ::ns::^ } + )cpp", + {func("ns::both"), cls("ns::Index")}, Opts); + EXPECT_EQ(Results.items.size(), Opts.Limit); +} + TEST(CompletionTest, IndexSuppressesPreambleCompletions) { MockFSProvider FS; MockCompilationDatabase CDB;