[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).
llvm-svn: 321554
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 87f73b2..28ea4db 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -558,7 +558,7 @@
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 = "";
diff --git a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
index e3042c3..5f852d0 100644
--- a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
+++ b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
@@ -54,6 +54,7 @@
using namespace llvm;
using ::testing::AllOf;
using ::testing::Contains;
+using ::testing::Each;
using ::testing::ElementsAre;
using ::testing::Not;
@@ -75,6 +76,11 @@
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 @@
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 @@
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")));
}