[clang-tools-extra] r321554 - [clangd] Properly set filterText for index-based completion items

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 29 06:59:22 PST 2017


Author: ibiryukov
Date: Fri Dec 29 06:59:22 2017
New Revision: 321554

URL: http://llvm.org/viewvc/llvm-project?rev=321554&view=rev
Log:
[clangd] Properly set filterText for index-based completion items

It was previously set to an identifier that the user typed, leading to
surprising behavior in VSCode (probably in other editors too).

Modified:
    clang-tools-extra/trunk/clangd/CodeComplete.cpp
    clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp

Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=321554&r1=321553&r2=321554&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Fri Dec 29 06:59:22 2017
@@ -558,7 +558,7 @@ CompletionItem indexCompletionItem(const
   Item.insertText = Sym.Name;
   // FIXME(ioeric): support snippets.
   Item.insertTextFormat = InsertTextFormat::PlainText;
-  Item.filterText = Filter;
+  Item.filterText = Sym.Name;
 
   // FIXME(ioeric): sort symbols appropriately.
   Item.sortText = "";

Modified: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp?rev=321554&r1=321553&r2=321554&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp Fri Dec 29 06:59:22 2017
@@ -54,6 +54,7 @@ namespace {
 using namespace llvm;
 using ::testing::AllOf;
 using ::testing::Contains;
+using ::testing::Each;
 using ::testing::ElementsAre;
 using ::testing::Not;
 
@@ -75,6 +76,11 @@ MATCHER_P(Snippet, Text, "") {
   return arg.insertTextFormat == clangd::InsertTextFormat::Snippet &&
          arg.insertText == Text;
 }
+MATCHER(FilterContainsName, "") {
+  if (arg.filterText.empty())
+    return true;
+  return llvm::StringRef(arg.insertText).contains(arg.filterText);
+}
 // Shorthand for Contains(Named(Name)).
 Matcher<const std::vector<CompletionItem> &> Has(std::string Name) {
   return Contains(Named(std::move(Name)));
@@ -95,9 +101,13 @@ CompletionList completions(StringRef Tex
   auto File = getVirtualTestFilePath("foo.cpp");
   Annotations Test(Text);
   Server.addDocument(Context::empty(), File, Test.code());
-  return Server.codeComplete(Context::empty(), File, Test.point(), Opts)
-      .get()
-      .second.Value;
+  auto CompletionList =
+      Server.codeComplete(Context::empty(), File, Test.point(), Opts)
+          .get()
+          .second.Value;
+  // Sanity-check that filterText is valid.
+  EXPECT_THAT(CompletionList.items, Each(FilterContainsName()));
+  return CompletionList;
 }
 
 TEST(CompletionTest, Limit) {
@@ -513,7 +523,7 @@ TEST(CompletionTest, IndexBasedWithFilte
       void f() { ns::x^ }
   )cpp",
                              Opts);
-  EXPECT_THAT(Results.items, Contains(AllOf(Named("XYZ"), Filter("x"))));
+  EXPECT_THAT(Results.items, Contains(AllOf(Named("XYZ"), Filter("XYZ"))));
   EXPECT_THAT(Results.items, Not(Has("foo")));
 }
 




More information about the cfe-commits mailing list