[PATCH] D42484: [clangd] Limit completion results.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 24 08:05:49 PST 2018


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: ioeric, jkorous-apple, ilya-biryukov, klimek.

- truncate symbols from static/dynamic index to the limited number

(which would save lots of cost in constructing the merged symbols).

- add an CLI option allowing to limit the number of returned completion results.

(default to 100)


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D42484

Files:
  clangd/CodeComplete.cpp
  clangd/index/Merge.cpp
  clangd/tool/ClangdMain.cpp
  unittests/clangd/CodeCompleteTests.cpp


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;
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<int> 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: 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/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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42484.131271.patch
Type: text/x-patch
Size: 2648 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180124/ef1532e8/attachment-0001.bin>


More information about the cfe-commits mailing list