From 00d99bd1c4ad80dc909c74c39681978f843a44c6 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Thu, 11 Apr 2019 09:36:36 +0000 Subject: [clangd] Use identifiers in file as completion candidates when build is not ready. Summary: o Lex the code to get the identifiers and put them into a "symbol" index. o Adds a new completion mode without compilation/sema into code completion workflow. o Make IncludeInserter work even when no compile command is present, by avoiding inserting non-verbatim headers. Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, jdoerfert, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60126 llvm-svn: 358159 --- .../unittests/clangd/CodeCompleteTests.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp') diff --git a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp index f60ed1c8426..4118fea325c 100644 --- a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp +++ b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp @@ -20,6 +20,7 @@ #include "TestTU.h" #include "index/MemIndex.h" #include "clang/Sema/CodeCompleteConsumer.h" +#include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" @@ -138,6 +139,25 @@ CodeCompleteResult completions(llvm::StringRef Text, FilePath); } +// Builds a server and runs code completion. +// If IndexSymbols is non-empty, an index will be built and passed to opts. +CodeCompleteResult completionsNoCompile(llvm::StringRef Text, + std::vector IndexSymbols = {}, + clangd::CodeCompleteOptions Opts = {}, + PathRef FilePath = "foo.cpp") { + std::unique_ptr OverrideIndex; + if (!IndexSymbols.empty()) { + assert(!Opts.Index && "both Index and IndexSymbols given!"); + OverrideIndex = memIndex(std::move(IndexSymbols)); + Opts.Index = OverrideIndex.get(); + } + + MockFSProvider FS; + Annotations Test(Text); + return codeComplete(FilePath, tooling::CompileCommand(), /*Preamble=*/nullptr, + Test.code(), Test.point(), FS.getFileSystem(), Opts); +} + Symbol withReferences(int N, Symbol S) { S.References = N; return S; @@ -2401,6 +2421,33 @@ TEST(CompletionTest, NamespaceDoubleInsertion) { UnorderedElementsAre(AllOf(Qualifier(""), Named("ABCDE")))); } +TEST(NoCompileCompletionTest, Basic) { + auto Results = completionsNoCompile(R"cpp( + void func() { + int xyz; + int abc; + ^ + } + )cpp"); + EXPECT_THAT(Results.Completions, + UnorderedElementsAre(Named("void"), Named("func"), Named("int"), + Named("xyz"), Named("abc"))); +} + +TEST(NoCompileCompletionTest, WithFilter) { + auto Results = completionsNoCompile(R"cpp( + void func() { + int sym1; + int sym2; + int xyz1; + int xyz2; + sy^ + } + )cpp"); + EXPECT_THAT(Results.Completions, + UnorderedElementsAre(Named("sym1"), Named("sym2"))); +} + } // namespace } // namespace clangd } // namespace clang -- cgit v1.2.3