Index: clang-tools-extra/trunk/clangd/CodeComplete.cpp =================================================================== --- clang-tools-extra/trunk/clangd/CodeComplete.cpp +++ clang-tools-extra/trunk/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: clang-tools-extra/trunk/clangd/index/Merge.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/Merge.cpp +++ clang-tools-extra/trunk/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: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp =================================================================== --- clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp +++ clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp @@ -86,6 +86,12 @@ clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")), llvm::cl::init(PCHStorageFlag::Disk)); +static llvm::cl::opt LimitCompletionResult( + "limit-completion", + llvm::cl::desc("Limit the number of completion results returned by clangd. " + "0 means no limit."), + llvm::cl::init(0)); + static llvm::cl::opt RunSynchronously( "run-synchronously", llvm::cl::desc("Parse on main thread. If set, -j is ignored"), @@ -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: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp @@ -529,6 +529,19 @@ 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); + EXPECT_TRUE(Results.isIncomplete); +} + TEST(CompletionTest, IndexSuppressesPreambleCompletions) { MockFSProvider FS; MockCompilationDatabase CDB;