2017-12-05 15:20:26 +08:00
|
|
|
//===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-20 00:50:37 +08:00
|
|
|
|
2017-12-21 00:06:05 +08:00
|
|
|
#include "Annotations.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "ClangdServer.h"
|
2017-12-21 00:06:05 +08:00
|
|
|
#include "CodeComplete.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "Compiler.h"
|
2017-12-13 20:51:22 +08:00
|
|
|
#include "Context.h"
|
2017-12-13 20:53:16 +08:00
|
|
|
#include "Matchers.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "Protocol.h"
|
2017-12-19 20:23:48 +08:00
|
|
|
#include "SourceCode.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "TestFS.h"
|
2017-12-20 00:50:37 +08:00
|
|
|
#include "index/MemIndex.h"
|
2017-12-06 04:11:29 +08:00
|
|
|
#include "gmock/gmock.h"
|
2017-12-05 15:20:26 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-12-19 18:29:27 +08:00
|
|
|
// Let GMock print completion items and signature help.
|
2017-12-06 04:11:29 +08:00
|
|
|
void PrintTo(const CompletionItem &I, std::ostream *O) {
|
|
|
|
llvm::raw_os_ostream OS(*O);
|
2017-12-08 23:00:59 +08:00
|
|
|
OS << I.label << " - " << toJSON(I);
|
|
|
|
}
|
|
|
|
void PrintTo(const std::vector<CompletionItem> &V, std::ostream *O) {
|
|
|
|
*O << "{\n";
|
|
|
|
for (const auto &I : V) {
|
|
|
|
*O << "\t";
|
|
|
|
PrintTo(I, O);
|
|
|
|
*O << "\n";
|
|
|
|
}
|
|
|
|
*O << "}";
|
2017-12-06 04:11:29 +08:00
|
|
|
}
|
2017-12-19 18:29:27 +08:00
|
|
|
void PrintTo(const SignatureInformation &I, std::ostream *O) {
|
|
|
|
llvm::raw_os_ostream OS(*O);
|
|
|
|
OS << I.label << " - " << toJSON(I);
|
|
|
|
}
|
|
|
|
void PrintTo(const std::vector<SignatureInformation> &V, std::ostream *O) {
|
|
|
|
*O << "{\n";
|
|
|
|
for (const auto &I : V) {
|
|
|
|
*O << "\t";
|
|
|
|
PrintTo(I, O);
|
|
|
|
*O << "\n";
|
|
|
|
}
|
|
|
|
*O << "}";
|
|
|
|
}
|
2017-12-06 04:11:29 +08:00
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
namespace {
|
|
|
|
using namespace llvm;
|
2017-12-06 04:11:29 +08:00
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::Contains;
|
2017-12-29 22:59:22 +08:00
|
|
|
using ::testing::Each;
|
2017-12-06 04:11:29 +08:00
|
|
|
using ::testing::ElementsAre;
|
|
|
|
using ::testing::Not;
|
2018-01-13 02:30:08 +08:00
|
|
|
using ::testing::UnorderedElementsAre;
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
class IgnoreDiagnostics : public DiagnosticsConsumer {
|
2018-01-11 01:59:27 +08:00
|
|
|
void
|
|
|
|
onDiagnosticsReady(const Context &Ctx, PathRef File,
|
|
|
|
Tagged<std::vector<DiagWithFixIts>> Diagnostics) override {
|
|
|
|
}
|
2017-12-05 15:20:26 +08:00
|
|
|
};
|
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
// GMock helpers for matching completion items.
|
|
|
|
MATCHER_P(Named, Name, "") { return arg.insertText == Name; }
|
2017-12-08 23:00:59 +08:00
|
|
|
MATCHER_P(Labeled, Label, "") { return arg.label == Label; }
|
|
|
|
MATCHER_P(Kind, K, "") { return arg.kind == K; }
|
2017-12-20 00:50:37 +08:00
|
|
|
MATCHER_P(Filter, F, "") { return arg.filterText == F; }
|
2018-01-10 01:32:00 +08:00
|
|
|
MATCHER_P(Doc, D, "") { return arg.documentation == D; }
|
|
|
|
MATCHER_P(Detail, D, "") { return arg.detail == D; }
|
2017-12-08 23:00:59 +08:00
|
|
|
MATCHER_P(PlainText, Text, "") {
|
|
|
|
return arg.insertTextFormat == clangd::InsertTextFormat::PlainText &&
|
|
|
|
arg.insertText == Text;
|
|
|
|
}
|
|
|
|
MATCHER_P(Snippet, Text, "") {
|
|
|
|
return arg.insertTextFormat == clangd::InsertTextFormat::Snippet &&
|
|
|
|
arg.insertText == Text;
|
|
|
|
}
|
2018-01-19 22:34:02 +08:00
|
|
|
MATCHER(NameContainsFilter, "") {
|
2017-12-29 22:59:22 +08:00
|
|
|
if (arg.filterText.empty())
|
|
|
|
return true;
|
|
|
|
return llvm::StringRef(arg.insertText).contains(arg.filterText);
|
|
|
|
}
|
2017-12-06 04:11:29 +08:00
|
|
|
// Shorthand for Contains(Named(Name)).
|
|
|
|
Matcher<const std::vector<CompletionItem> &> Has(std::string Name) {
|
|
|
|
return Contains(Named(std::move(Name)));
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
2017-12-08 23:00:59 +08:00
|
|
|
Matcher<const std::vector<CompletionItem> &> Has(std::string Name,
|
|
|
|
CompletionItemKind K) {
|
|
|
|
return Contains(AllOf(Named(std::move(Name)), Kind(K)));
|
2017-12-06 04:11:29 +08:00
|
|
|
}
|
2017-12-08 23:00:59 +08:00
|
|
|
MATCHER(IsDocumented, "") { return !arg.documentation.empty(); }
|
2017-12-06 04:11:29 +08:00
|
|
|
|
2018-01-18 17:27:56 +08:00
|
|
|
std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) {
|
|
|
|
SymbolSlab::Builder Slab;
|
|
|
|
for (const auto &Sym : Symbols)
|
|
|
|
Slab.insert(Sym);
|
|
|
|
return MemIndex::build(std::move(Slab).build());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds a server and runs code completion.
|
|
|
|
// If IndexSymbols is non-empty, an index will be built and passed to opts.
|
2017-12-06 04:11:29 +08:00
|
|
|
CompletionList completions(StringRef Text,
|
2018-01-18 17:27:56 +08:00
|
|
|
std::vector<Symbol> IndexSymbols = {},
|
2017-12-06 04:11:29 +08:00
|
|
|
clangd::CodeCompleteOptions Opts = {}) {
|
2018-01-18 17:27:56 +08:00
|
|
|
std::unique_ptr<SymbolIndex> OverrideIndex;
|
|
|
|
if (!IndexSymbols.empty()) {
|
|
|
|
assert(!Opts.Index && "both Index and IndexSymbols given!");
|
|
|
|
OverrideIndex = memIndex(std::move(IndexSymbols));
|
|
|
|
Opts.Index = OverrideIndex.get();
|
|
|
|
}
|
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
MockFSProvider FS;
|
2017-12-05 15:34:35 +08:00
|
|
|
MockCompilationDatabase CDB;
|
2017-12-05 15:20:26 +08:00
|
|
|
IgnoreDiagnostics DiagConsumer;
|
|
|
|
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
|
2017-12-13 20:51:22 +08:00
|
|
|
/*StorePreamblesInMemory=*/true);
|
2017-12-06 04:11:29 +08:00
|
|
|
auto File = getVirtualTestFilePath("foo.cpp");
|
2017-12-21 00:06:05 +08:00
|
|
|
Annotations Test(Text);
|
2018-01-13 02:30:08 +08:00
|
|
|
Server.addDocument(Context::empty(), File, Test.code()).wait();
|
2017-12-29 22:59:22 +08:00
|
|
|
auto CompletionList =
|
|
|
|
Server.codeComplete(Context::empty(), File, Test.point(), Opts)
|
|
|
|
.get()
|
|
|
|
.second.Value;
|
|
|
|
// Sanity-check that filterText is valid.
|
2018-01-19 22:34:02 +08:00
|
|
|
EXPECT_THAT(CompletionList.items, Each(NameContainsFilter()));
|
2017-12-29 22:59:22 +08:00
|
|
|
return CompletionList;
|
2017-12-06 04:11:29 +08:00
|
|
|
}
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
std::string replace(StringRef Haystack, StringRef Needle, StringRef Repl) {
|
|
|
|
std::string Result;
|
|
|
|
raw_string_ostream OS(Result);
|
|
|
|
std::pair<StringRef, StringRef> Split;
|
|
|
|
for (Split = Haystack.split(Needle); !Split.second.empty();
|
|
|
|
Split = Split.first.split(Needle))
|
|
|
|
OS << Split.first << Repl;
|
|
|
|
Result += Split.first;
|
|
|
|
OS.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-01-18 17:27:56 +08:00
|
|
|
// Helpers to produce fake index symbols for memIndex() or completions().
|
2018-01-19 22:34:02 +08:00
|
|
|
// USRFormat is a regex replacement string for the unqualified part of the USR.
|
|
|
|
Symbol sym(StringRef QName, index::SymbolKind Kind, StringRef USRFormat) {
|
2018-01-18 17:27:56 +08:00
|
|
|
Symbol Sym;
|
2018-01-19 22:34:02 +08:00
|
|
|
std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
|
2018-01-18 17:27:56 +08:00
|
|
|
size_t Pos = QName.rfind("::");
|
|
|
|
if (Pos == llvm::StringRef::npos) {
|
|
|
|
Sym.Name = QName;
|
|
|
|
Sym.Scope = "";
|
|
|
|
} else {
|
|
|
|
Sym.Name = QName.substr(Pos + 2);
|
2018-01-20 06:18:21 +08:00
|
|
|
Sym.Scope = QName.substr(0, Pos + 2);
|
|
|
|
USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns
|
2018-01-18 17:27:56 +08:00
|
|
|
}
|
2018-01-19 22:34:02 +08:00
|
|
|
USR += Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func#
|
|
|
|
Sym.ID = SymbolID(USR);
|
2018-01-18 17:27:56 +08:00
|
|
|
Sym.CompletionPlainInsertText = Sym.Name;
|
2018-01-19 22:34:02 +08:00
|
|
|
Sym.CompletionSnippetInsertText = Sym.Name;
|
2018-01-18 17:27:56 +08:00
|
|
|
Sym.CompletionLabel = Sym.Name;
|
|
|
|
Sym.SymInfo.Kind = Kind;
|
|
|
|
return Sym;
|
|
|
|
}
|
2018-01-19 22:34:02 +08:00
|
|
|
Symbol func(StringRef Name) { // Assumes the function has no args.
|
|
|
|
return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
|
|
|
|
}
|
|
|
|
Symbol cls(StringRef Name) {
|
|
|
|
return sym(Name, index::SymbolKind::Class, "@S@\\0@S@\\0");
|
|
|
|
}
|
|
|
|
Symbol var(StringRef Name) {
|
|
|
|
return sym(Name, index::SymbolKind::Variable, "@\\0");
|
|
|
|
}
|
2018-01-18 17:27:56 +08:00
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
TEST(CompletionTest, Limit) {
|
|
|
|
clangd::CodeCompleteOptions Opts;
|
|
|
|
Opts.Limit = 2;
|
|
|
|
auto Results = completions(R"cpp(
|
2017-12-05 15:20:26 +08:00
|
|
|
struct ClassWithMembers {
|
|
|
|
int AAA();
|
|
|
|
int BBB();
|
|
|
|
int CCC();
|
|
|
|
}
|
2017-12-06 04:11:29 +08:00
|
|
|
int main() { ClassWithMembers().^ }
|
2017-12-05 15:20:26 +08:00
|
|
|
)cpp",
|
2018-01-18 17:27:56 +08:00
|
|
|
/*IndexSymbols=*/{}, Opts);
|
2017-12-05 15:20:26 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(Results.isIncomplete);
|
2017-12-06 04:11:29 +08:00
|
|
|
EXPECT_THAT(Results.items, ElementsAre(Named("AAA"), Named("BBB")));
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
TEST(CompletionTest, Filter) {
|
|
|
|
std::string Body = R"cpp(
|
2017-12-05 15:20:26 +08:00
|
|
|
int Abracadabra;
|
|
|
|
int Alakazam;
|
|
|
|
struct S {
|
|
|
|
int FooBar;
|
|
|
|
int FooBaz;
|
|
|
|
int Qux;
|
|
|
|
};
|
|
|
|
)cpp";
|
2017-12-06 04:11:29 +08:00
|
|
|
EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").items,
|
|
|
|
AllOf(Has("FooBar"), Has("FooBaz"), Not(Has("Qux"))));
|
|
|
|
|
|
|
|
EXPECT_THAT(completions(Body + "int main() { S().FR^ }").items,
|
|
|
|
AllOf(Has("FooBar"), Not(Has("FooBaz")), Not(Has("Qux"))));
|
|
|
|
|
|
|
|
EXPECT_THAT(completions(Body + "int main() { S().opr^ }").items,
|
|
|
|
Has("operator="));
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
EXPECT_THAT(completions(Body + "int main() { aaa^ }").items,
|
|
|
|
AllOf(Has("Abracadabra"), Has("Alakazam")));
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
EXPECT_THAT(completions(Body + "int main() { _a^ }").items,
|
|
|
|
AllOf(Has("static_cast"), Not(Has("Abracadabra"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterDotCompletion(clangd::CodeCompleteOptions Opts) {
|
2017-12-08 23:00:59 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
#define MACRO X
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int global_var;
|
2017-12-06 04:11:29 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int global_func();
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
struct GlobalClass {};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
struct ClassWithMembers {
|
|
|
|
/// Doc for method.
|
|
|
|
int method();
|
2017-12-06 04:11:29 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int field;
|
|
|
|
private:
|
|
|
|
int private_field;
|
|
|
|
};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int test() {
|
|
|
|
struct LocalClass {};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
/// Doc for local_var.
|
|
|
|
int local_var;
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
ClassWithMembers().^
|
|
|
|
}
|
|
|
|
)cpp",
|
2018-01-19 22:34:02 +08:00
|
|
|
{cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
|
2017-12-06 04:11:29 +08:00
|
|
|
|
|
|
|
// Class members. The only items that must be present in after-dot
|
|
|
|
// completion.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
Results.items,
|
|
|
|
AllOf(Has(Opts.EnableSnippets ? "method()" : "method"), Has("field")));
|
|
|
|
EXPECT_IFF(Opts.IncludeIneligibleResults, Results.items,
|
|
|
|
Has("private_field"));
|
2017-12-06 04:11:29 +08:00
|
|
|
// Global items.
|
2018-01-19 22:34:02 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
Results.items,
|
|
|
|
Not(AnyOf(Has("global_var"), Has("index_var"), Has("global_func"),
|
|
|
|
Has("global_func()"), Has("index_func"), Has("GlobalClass"),
|
|
|
|
Has("IndexClass"), Has("MACRO"), Has("LocalClass"))));
|
2017-12-06 04:11:29 +08:00
|
|
|
// There should be no code patterns (aka snippets) in after-dot
|
|
|
|
// completion. At least there aren't any we're aware of.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_THAT(Results.items, Not(Contains(Kind(CompletionItemKind::Snippet))));
|
2017-12-06 04:11:29 +08:00
|
|
|
// Check documentation.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_IFF(Opts.IncludeBriefComments, Results.items,
|
|
|
|
Contains(IsDocumented()));
|
2017-12-06 04:11:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) {
|
2017-12-08 23:00:59 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
#define MACRO X
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int global_var;
|
|
|
|
int global_func();
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
struct GlobalClass {};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
struct ClassWithMembers {
|
|
|
|
/// Doc for method.
|
|
|
|
int method();
|
|
|
|
};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
int test() {
|
|
|
|
struct LocalClass {};
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
/// Doc for local_var.
|
|
|
|
int local_var;
|
2017-12-05 15:20:26 +08:00
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
^
|
|
|
|
}
|
|
|
|
)cpp",
|
2018-01-19 22:34:02 +08:00
|
|
|
{cls("IndexClass"), var("index_var"), func("index_func")}, Opts);
|
2017-12-06 04:11:29 +08:00
|
|
|
|
|
|
|
// Class members. Should never be present in global completions.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_THAT(Results.items,
|
2017-12-06 04:11:29 +08:00
|
|
|
Not(AnyOf(Has("method"), Has("method()"), Has("field"))));
|
|
|
|
// Global items.
|
2018-01-18 23:31:30 +08:00
|
|
|
EXPECT_THAT(Results.items,
|
2018-01-19 22:34:02 +08:00
|
|
|
AllOf(Has("global_var"), Has("index_var"),
|
2018-01-18 23:31:30 +08:00
|
|
|
Has(Opts.EnableSnippets ? "global_func()" : "global_func"),
|
2018-01-19 22:34:02 +08:00
|
|
|
Has("index_func" /* our fake symbol doesn't include () */),
|
|
|
|
Has("GlobalClass"), Has("IndexClass")));
|
2017-12-06 04:11:29 +08:00
|
|
|
// A macro.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_IFF(Opts.IncludeMacros, Results.items, Has("MACRO"));
|
2017-12-06 04:11:29 +08:00
|
|
|
// Local items. Must be present always.
|
2017-12-12 20:56:46 +08:00
|
|
|
EXPECT_THAT(Results.items,
|
|
|
|
AllOf(Has("local_var"), Has("LocalClass"),
|
|
|
|
Contains(Kind(CompletionItemKind::Snippet))));
|
2017-12-06 04:11:29 +08:00
|
|
|
// Check documentation.
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_IFF(Opts.IncludeBriefComments, Results.items,
|
|
|
|
Contains(IsDocumented()));
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
TEST(CompletionTest, CompletionOptions) {
|
2018-01-16 20:21:24 +08:00
|
|
|
auto Test = [&](const clangd::CodeCompleteOptions &Opts) {
|
|
|
|
TestAfterDotCompletion(Opts);
|
|
|
|
TestGlobalScopeCompletion(Opts);
|
|
|
|
};
|
|
|
|
// We used to test every combination of options, but that got too slow (2^N).
|
|
|
|
auto Flags = {
|
|
|
|
&clangd::CodeCompleteOptions::IncludeMacros,
|
|
|
|
&clangd::CodeCompleteOptions::IncludeBriefComments,
|
|
|
|
&clangd::CodeCompleteOptions::EnableSnippets,
|
|
|
|
&clangd::CodeCompleteOptions::IncludeCodePatterns,
|
|
|
|
&clangd::CodeCompleteOptions::IncludeIneligibleResults,
|
|
|
|
};
|
|
|
|
// Test default options.
|
|
|
|
Test({});
|
|
|
|
// Test with one flag flipped.
|
|
|
|
for (auto &F : Flags) {
|
|
|
|
clangd::CodeCompleteOptions O;
|
|
|
|
O.*F ^= true;
|
|
|
|
Test(O);
|
2017-12-05 15:20:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 04:11:29 +08:00
|
|
|
// Check code completion works when the file contents are overridden.
|
|
|
|
TEST(CompletionTest, CheckContentsOverride) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
IgnoreDiagnostics DiagConsumer;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
|
2017-12-13 20:51:22 +08:00
|
|
|
/*StorePreamblesInMemory=*/true);
|
2017-12-06 04:11:29 +08:00
|
|
|
auto File = getVirtualTestFilePath("foo.cpp");
|
2017-12-13 20:51:22 +08:00
|
|
|
Server.addDocument(Context::empty(), File, "ignored text!");
|
2017-12-06 04:11:29 +08:00
|
|
|
|
2017-12-21 00:06:05 +08:00
|
|
|
Annotations Example("int cbc; int b = ^;");
|
|
|
|
auto Results = Server
|
|
|
|
.codeComplete(Context::empty(), File, Example.point(),
|
|
|
|
clangd::CodeCompleteOptions(),
|
|
|
|
StringRef(Example.code()))
|
|
|
|
.get()
|
|
|
|
.second.Value;
|
2017-12-06 04:11:29 +08:00
|
|
|
EXPECT_THAT(Results.items, Contains(Named("cbc")));
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:00:59 +08:00
|
|
|
TEST(CompletionTest, Priorities) {
|
|
|
|
auto Internal = completions(R"cpp(
|
|
|
|
class Foo {
|
|
|
|
public: void pub();
|
|
|
|
protected: void prot();
|
|
|
|
private: void priv();
|
|
|
|
};
|
|
|
|
void Foo::pub() { this->^ }
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(Internal.items,
|
|
|
|
HasSubsequence(Named("priv"), Named("prot"), Named("pub")));
|
|
|
|
|
|
|
|
auto External = completions(R"cpp(
|
|
|
|
class Foo {
|
|
|
|
public: void pub();
|
|
|
|
protected: void prot();
|
|
|
|
private: void priv();
|
|
|
|
};
|
|
|
|
void test() {
|
|
|
|
Foo F;
|
|
|
|
F.^
|
|
|
|
}
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(External.items,
|
|
|
|
AllOf(Has("pub"), Not(Has("prot")), Not(Has("priv"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompletionTest, Qualifiers) {
|
|
|
|
auto Results = completions(R"cpp(
|
|
|
|
class Foo {
|
|
|
|
public: int foo() const;
|
|
|
|
int bar() const;
|
|
|
|
};
|
|
|
|
class Bar : public Foo {
|
|
|
|
int foo() const;
|
|
|
|
};
|
|
|
|
void test() { Bar().^ }
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(Results.items, HasSubsequence(Labeled("bar() const"),
|
|
|
|
Labeled("Foo::foo() const")));
|
|
|
|
EXPECT_THAT(Results.items, Not(Contains(Labeled("foo() const")))); // private
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompletionTest, Snippets) {
|
|
|
|
clangd::CodeCompleteOptions Opts;
|
|
|
|
Opts.EnableSnippets = true;
|
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
struct fake {
|
|
|
|
int a;
|
|
|
|
int f(int i, const float f) const;
|
|
|
|
};
|
|
|
|
int main() {
|
|
|
|
fake f;
|
|
|
|
f.^
|
|
|
|
}
|
|
|
|
)cpp",
|
2018-01-18 17:27:56 +08:00
|
|
|
/*IndexSymbols=*/{}, Opts);
|
2017-12-08 23:00:59 +08:00
|
|
|
EXPECT_THAT(Results.items,
|
2017-12-21 01:24:31 +08:00
|
|
|
HasSubsequence(Snippet("a"),
|
2017-12-08 23:00:59 +08:00
|
|
|
Snippet("f(${1:int i}, ${2:const float f})")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompletionTest, Kinds) {
|
2018-01-19 22:34:02 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
#define MACRO X
|
|
|
|
int variable;
|
|
|
|
struct Struct {};
|
|
|
|
int function();
|
|
|
|
int X = ^
|
|
|
|
)cpp",
|
|
|
|
{func("indexFunction"), var("indexVariable"), cls("indexClass")});
|
|
|
|
EXPECT_THAT(Results.items,
|
|
|
|
AllOf(Has("function", CompletionItemKind::Function),
|
|
|
|
Has("variable", CompletionItemKind::Variable),
|
|
|
|
Has("int", CompletionItemKind::Keyword),
|
|
|
|
Has("Struct", CompletionItemKind::Class),
|
|
|
|
Has("MACRO", CompletionItemKind::Text),
|
|
|
|
Has("indexFunction", CompletionItemKind::Function),
|
|
|
|
Has("indexVariable", CompletionItemKind::Variable),
|
|
|
|
Has("indexClass", CompletionItemKind::Class)));
|
2017-12-08 23:00:59 +08:00
|
|
|
|
|
|
|
Results = completions("nam^");
|
|
|
|
EXPECT_THAT(Results.items, Has("namespace", CompletionItemKind::Snippet));
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:16:09 +08:00
|
|
|
TEST(CompletionTest, NoDuplicates) {
|
2018-01-19 22:34:02 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
class Adapter {
|
|
|
|
void method();
|
|
|
|
};
|
2018-01-13 00:16:09 +08:00
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
void Adapter::method() {
|
|
|
|
Adapter^
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
{cls("Adapter")});
|
2018-01-13 00:16:09 +08:00
|
|
|
|
|
|
|
// Make sure there are no duplicate entries of 'Adapter'.
|
2018-01-23 05:05:00 +08:00
|
|
|
EXPECT_THAT(Results.items, ElementsAre(Named("Adapter")));
|
2018-01-13 00:16:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
TEST(CompletionTest, ScopedNoIndex) {
|
2018-01-18 17:27:56 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
2018-01-19 22:34:02 +08:00
|
|
|
namespace fake { int BigBang, Babble, Ball; };
|
|
|
|
int main() { fake::bb^ }
|
|
|
|
")cpp");
|
|
|
|
// BigBang is a better match than Babble. Ball doesn't match at all.
|
|
|
|
EXPECT_THAT(Results.items, ElementsAre(Named("BigBang"), Named("Babble")));
|
2018-01-10 22:44:34 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
TEST(CompletionTest, Scoped) {
|
2018-01-18 17:27:56 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
2018-01-19 22:34:02 +08:00
|
|
|
namespace fake { int Babble, Ball; };
|
|
|
|
int main() { fake::bb^ }
|
|
|
|
")cpp",
|
|
|
|
{var("fake::BigBang")});
|
|
|
|
EXPECT_THAT(Results.items, ElementsAre(Named("BigBang"), Named("Babble")));
|
2017-12-20 00:50:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
TEST(CompletionTest, ScopedWithFilter) {
|
2018-01-18 17:27:56 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
void f() { ns::x^ }
|
|
|
|
)cpp",
|
|
|
|
{cls("ns::XYZ"), func("ns::foo")});
|
|
|
|
EXPECT_THAT(Results.items,
|
|
|
|
UnorderedElementsAre(AllOf(Named("XYZ"), Filter("XYZ"))));
|
2017-12-20 00:50:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
TEST(CompletionTest, GlobalQualified) {
|
2018-01-18 17:27:56 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
void f() { ::^ }
|
|
|
|
)cpp",
|
|
|
|
{cls("XYZ")});
|
|
|
|
EXPECT_THAT(Results.items, AllOf(Has("XYZ", CompletionItemKind::Class),
|
|
|
|
Has("f", CompletionItemKind::Function)));
|
2017-12-20 00:50:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:34:02 +08:00
|
|
|
TEST(CompletionTest, FullyQualified) {
|
2018-01-18 17:27:56 +08:00
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
2018-01-19 22:34:02 +08:00
|
|
|
namespace ns { void bar(); }
|
2018-01-18 17:27:56 +08:00
|
|
|
void f() { ::ns::^ }
|
|
|
|
)cpp",
|
|
|
|
{cls("ns::XYZ")});
|
2018-01-19 22:34:02 +08:00
|
|
|
EXPECT_THAT(Results.items, AllOf(Has("XYZ", CompletionItemKind::Class),
|
|
|
|
Has("bar", CompletionItemKind::Function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompletionTest, SemaIndexMerge) {
|
|
|
|
auto Results = completions(
|
|
|
|
R"cpp(
|
|
|
|
namespace ns { int local; void both(); }
|
|
|
|
void f() { ::ns::^ }
|
|
|
|
)cpp",
|
|
|
|
{func("ns::both"), cls("ns::Index")});
|
|
|
|
// We get results from both index and sema, with no duplicates.
|
|
|
|
EXPECT_THAT(
|
|
|
|
Results.items,
|
|
|
|
UnorderedElementsAre(Named("local"), Named("Index"), Named("both")));
|
2017-12-20 00:50:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 02:30:08 +08:00
|
|
|
TEST(CompletionTest, IndexSuppressesPreambleCompletions) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
IgnoreDiagnostics DiagConsumer;
|
|
|
|
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
|
|
|
|
/*StorePreamblesInMemory=*/true);
|
|
|
|
|
|
|
|
FS.Files[getVirtualTestFilePath("bar.h")] =
|
|
|
|
R"cpp(namespace ns { int preamble; })cpp";
|
|
|
|
auto File = getVirtualTestFilePath("foo.cpp");
|
|
|
|
Annotations Test(R"cpp(
|
|
|
|
#include "bar.h"
|
|
|
|
namespace ns { int local; }
|
|
|
|
void f() { ns::^ }
|
|
|
|
)cpp");
|
|
|
|
Server.addDocument(Context::empty(), File, Test.code()).wait();
|
|
|
|
clangd::CodeCompleteOptions Opts = {};
|
|
|
|
|
|
|
|
auto WithoutIndex =
|
|
|
|
Server.codeComplete(Context::empty(), File, Test.point(), Opts)
|
|
|
|
.get()
|
|
|
|
.second.Value;
|
|
|
|
EXPECT_THAT(WithoutIndex.items,
|
|
|
|
UnorderedElementsAre(Named("local"), Named("preamble")));
|
|
|
|
|
2018-01-18 17:27:56 +08:00
|
|
|
auto I = memIndex({var("ns::index")});
|
2018-01-13 02:30:08 +08:00
|
|
|
Opts.Index = I.get();
|
|
|
|
auto WithIndex =
|
|
|
|
Server.codeComplete(Context::empty(), File, Test.point(), Opts)
|
|
|
|
.get()
|
|
|
|
.second.Value;
|
|
|
|
EXPECT_THAT(WithIndex.items,
|
|
|
|
UnorderedElementsAre(Named("local"), Named("index")));
|
|
|
|
}
|
|
|
|
|
2018-01-18 17:27:56 +08:00
|
|
|
TEST(CompletionTest, DynamicIndexMultiFile) {
|
2017-12-20 02:00:37 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
IgnoreDiagnostics DiagConsumer;
|
|
|
|
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
|
|
|
|
/*StorePreamblesInMemory=*/true,
|
|
|
|
/*BuildDynamicSymbolIndex=*/true);
|
|
|
|
|
|
|
|
Server
|
|
|
|
.addDocument(Context::empty(), getVirtualTestFilePath("foo.cpp"), R"cpp(
|
2018-01-10 01:32:00 +08:00
|
|
|
namespace ns { class XYZ {}; void foo(int x) {} }
|
2017-12-20 02:00:37 +08:00
|
|
|
)cpp")
|
|
|
|
.wait();
|
|
|
|
|
|
|
|
auto File = getVirtualTestFilePath("bar.cpp");
|
2017-12-21 00:06:05 +08:00
|
|
|
Annotations Test(R"cpp(
|
2018-01-10 01:32:00 +08:00
|
|
|
namespace ns {
|
|
|
|
class XXX {};
|
|
|
|
/// Doooc
|
|
|
|
void fooooo() {}
|
|
|
|
}
|
2017-12-20 02:00:37 +08:00
|
|
|
void f() { ns::^ }
|
|
|
|
)cpp");
|
2017-12-21 00:06:05 +08:00
|
|
|
Server.addDocument(Context::empty(), File, Test.code()).wait();
|
2017-12-20 02:00:37 +08:00
|
|
|
|
2017-12-21 00:06:05 +08:00
|
|
|
auto Results = Server.codeComplete(Context::empty(), File, Test.point(), {})
|
2017-12-20 02:00:37 +08:00
|
|
|
.get()
|
|
|
|
.second.Value;
|
|
|
|
// "XYZ" and "foo" are not included in the file being completed but are still
|
|
|
|
// visible through the index.
|
|
|
|
EXPECT_THAT(Results.items, Has("XYZ", CompletionItemKind::Class));
|
|
|
|
EXPECT_THAT(Results.items, Has("foo", CompletionItemKind::Function));
|
|
|
|
EXPECT_THAT(Results.items, Has("XXX", CompletionItemKind::Class));
|
2018-01-10 01:32:00 +08:00
|
|
|
EXPECT_THAT(Results.items, Contains(AllOf(Named("fooooo"), Filter("fooooo"),
|
|
|
|
Kind(CompletionItemKind::Function),
|
|
|
|
Doc("Doooc"), Detail("void"))));
|
2017-12-20 02:00:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:27:56 +08:00
|
|
|
SignatureHelp signatures(StringRef Text) {
|
|
|
|
MockFSProvider FS;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
IgnoreDiagnostics DiagConsumer;
|
|
|
|
ClangdServer Server(CDB, DiagConsumer, FS, getDefaultAsyncThreadsCount(),
|
|
|
|
/*StorePreamblesInMemory=*/true);
|
|
|
|
auto File = getVirtualTestFilePath("foo.cpp");
|
|
|
|
Annotations Test(Text);
|
|
|
|
Server.addDocument(Context::empty(), File, Test.code());
|
|
|
|
auto R = Server.signatureHelp(Context::empty(), File, Test.point());
|
|
|
|
assert(R);
|
|
|
|
return R.get().Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
MATCHER_P(ParamsAre, P, "") {
|
|
|
|
if (P.size() != arg.parameters.size())
|
|
|
|
return false;
|
|
|
|
for (unsigned I = 0; I < P.size(); ++I)
|
|
|
|
if (P[I] != arg.parameters[I].label)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matcher<SignatureInformation> Sig(std::string Label,
|
|
|
|
std::vector<std::string> Params) {
|
|
|
|
return AllOf(Labeled(Label), ParamsAre(Params));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SignatureHelpTest, Overloads) {
|
|
|
|
auto Results = signatures(R"cpp(
|
|
|
|
void foo(int x, int y);
|
|
|
|
void foo(int x, float y);
|
|
|
|
void foo(float x, int y);
|
|
|
|
void foo(float x, float y);
|
|
|
|
void bar(int x, int y = 0);
|
|
|
|
int main() { foo(^); }
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(Results.signatures,
|
|
|
|
UnorderedElementsAre(
|
|
|
|
Sig("foo(float x, float y) -> void", {"float x", "float y"}),
|
|
|
|
Sig("foo(float x, int y) -> void", {"float x", "int y"}),
|
|
|
|
Sig("foo(int x, float y) -> void", {"int x", "float y"}),
|
|
|
|
Sig("foo(int x, int y) -> void", {"int x", "int y"})));
|
|
|
|
// We always prefer the first signature.
|
|
|
|
EXPECT_EQ(0, Results.activeSignature);
|
|
|
|
EXPECT_EQ(0, Results.activeParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SignatureHelpTest, DefaultArgs) {
|
|
|
|
auto Results = signatures(R"cpp(
|
|
|
|
void bar(int x, int y = 0);
|
|
|
|
void bar(float x = 0, int y = 42);
|
|
|
|
int main() { bar(^
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(Results.signatures,
|
|
|
|
UnorderedElementsAre(
|
|
|
|
Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}),
|
|
|
|
Sig("bar(float x = 0, int y = 42) -> void",
|
|
|
|
{"float x = 0", "int y = 42"})));
|
|
|
|
EXPECT_EQ(0, Results.activeSignature);
|
|
|
|
EXPECT_EQ(0, Results.activeParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SignatureHelpTest, ActiveArg) {
|
|
|
|
auto Results = signatures(R"cpp(
|
|
|
|
int baz(int a, int b, int c);
|
|
|
|
int main() { baz(baz(1,2,3), ^); }
|
|
|
|
)cpp");
|
|
|
|
EXPECT_THAT(Results.signatures,
|
|
|
|
ElementsAre(Sig("baz(int a, int b, int c) -> int",
|
|
|
|
{"int a", "int b", "int c"})));
|
|
|
|
EXPECT_EQ(0, Results.activeSignature);
|
|
|
|
EXPECT_EQ(1, Results.activeParameter);
|
|
|
|
}
|
|
|
|
|
2017-12-05 15:20:26 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|