Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 1 | //===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 9 | |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 10 | #include "Annotations.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 11 | #include "ClangdServer.h" |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 12 | #include "CodeComplete.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 13 | #include "Compiler.h" |
Ilya Biryukov | 5a85b8e | 2017-12-13 12:53:16 +0000 | [diff] [blame] | 14 | #include "Matchers.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 15 | #include "Protocol.h" |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 16 | #include "Quality.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 17 | #include "SourceCode.h" |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 18 | #include "SyncAPI.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 19 | #include "TestFS.h" |
Eric Liu | 6f648df | 2017-12-19 16:50:37 +0000 | [diff] [blame] | 20 | #include "index/MemIndex.h" |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Error.h" |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 22 | #include "llvm/Testing/Support/Error.h" |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 23 | #include "gmock/gmock.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | namespace clang { |
| 27 | namespace clangd { |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 28 | |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | using namespace llvm; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 31 | using ::testing::AllOf; |
| 32 | using ::testing::Contains; |
Ilya Biryukov | 5a5e1ca | 2017-12-29 14:59:22 +0000 | [diff] [blame] | 33 | using ::testing::Each; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 34 | using ::testing::ElementsAre; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 35 | using ::testing::Field; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 36 | using ::testing::HasSubstr; |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 37 | using ::testing::IsEmpty; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 38 | using ::testing::Not; |
Sam McCall | 3d139c5 | 2018-01-12 18:30:08 +0000 | [diff] [blame] | 39 | using ::testing::UnorderedElementsAre; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 40 | |
| 41 | class IgnoreDiagnostics : public DiagnosticsConsumer { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 42 | void onDiagnosticsReady(PathRef File, |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 43 | std::vector<Diag> Diagnostics) override {} |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 46 | // GMock helpers for matching completion items. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 47 | MATCHER_P(Named, Name, "") { return arg.Name == Name; } |
| 48 | MATCHER_P(Scope, S, "") { return arg.Scope == S; } |
| 49 | MATCHER_P(Qualifier, Q, "") { return arg.RequiredQualifier == Q; } |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 50 | MATCHER_P(Labeled, Label, "") { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 51 | return arg.RequiredQualifier + arg.Name + arg.Signature == Label; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 52 | } |
| 53 | MATCHER_P(SigHelpLabeled, Label, "") { return arg.label == Label; } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 54 | MATCHER_P(Kind, K, "") { return arg.Kind == K; } |
| 55 | MATCHER_P(Doc, D, "") { return arg.Documentation == D; } |
| 56 | MATCHER_P(ReturnType, D, "") { return arg.ReturnType == D; } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 57 | MATCHER_P(InsertInclude, IncludeHeader, "") { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 58 | return arg.Header == IncludeHeader && bool(arg.HeaderInsertion); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 59 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 60 | MATCHER(InsertInclude, "") { return bool(arg.HeaderInsertion); } |
| 61 | MATCHER_P(SnippetSuffix, Text, "") { return arg.SnippetSuffix == Text; } |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 62 | MATCHER_P(Origin, OriginSet, "") { return arg.Origin == OriginSet; } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 63 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 64 | // Shorthand for Contains(Named(Name)). |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 65 | Matcher<const std::vector<CodeCompletion> &> Has(std::string Name) { |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 66 | return Contains(Named(std::move(Name))); |
| 67 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 68 | Matcher<const std::vector<CodeCompletion> &> Has(std::string Name, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 69 | CompletionItemKind K) { |
| 70 | return Contains(AllOf(Named(std::move(Name)), Kind(K))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 71 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 72 | MATCHER(IsDocumented, "") { return !arg.Documentation.empty(); } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 73 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 74 | std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) { |
| 75 | SymbolSlab::Builder Slab; |
| 76 | for (const auto &Sym : Symbols) |
| 77 | Slab.insert(Sym); |
| 78 | return MemIndex::build(std::move(Slab).build()); |
| 79 | } |
| 80 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 81 | CodeCompleteResult completions(ClangdServer &Server, StringRef Text, |
| 82 | std::vector<Symbol> IndexSymbols = {}, |
| 83 | clangd::CodeCompleteOptions Opts = {}) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 84 | std::unique_ptr<SymbolIndex> OverrideIndex; |
| 85 | if (!IndexSymbols.empty()) { |
| 86 | assert(!Opts.Index && "both Index and IndexSymbols given!"); |
| 87 | OverrideIndex = memIndex(std::move(IndexSymbols)); |
| 88 | Opts.Index = OverrideIndex.get(); |
| 89 | } |
| 90 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 91 | auto File = testPath("foo.cpp"); |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 92 | Annotations Test(Text); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 93 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 94 | auto CompletionList = |
| 95 | cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Ilya Biryukov | 5a5e1ca | 2017-12-29 14:59:22 +0000 | [diff] [blame] | 96 | return CompletionList; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 99 | // Builds a server and runs code completion. |
| 100 | // If IndexSymbols is non-empty, an index will be built and passed to opts. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 101 | CodeCompleteResult completions(StringRef Text, |
| 102 | std::vector<Symbol> IndexSymbols = {}, |
| 103 | clangd::CodeCompleteOptions Opts = {}) { |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 104 | MockFSProvider FS; |
| 105 | MockCompilationDatabase CDB; |
| 106 | IgnoreDiagnostics DiagConsumer; |
| 107 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 108 | return completions(Server, Text, std::move(IndexSymbols), std::move(Opts)); |
| 109 | } |
| 110 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 111 | std::string replace(StringRef Haystack, StringRef Needle, StringRef Repl) { |
| 112 | std::string Result; |
| 113 | raw_string_ostream OS(Result); |
| 114 | std::pair<StringRef, StringRef> Split; |
| 115 | for (Split = Haystack.split(Needle); !Split.second.empty(); |
| 116 | Split = Split.first.split(Needle)) |
| 117 | OS << Split.first << Repl; |
| 118 | Result += Split.first; |
| 119 | OS.flush(); |
| 120 | return Result; |
| 121 | } |
| 122 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 123 | // Helpers to produce fake index symbols for memIndex() or completions(). |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 124 | // USRFormat is a regex replacement string for the unqualified part of the USR. |
| 125 | Symbol sym(StringRef QName, index::SymbolKind Kind, StringRef USRFormat) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 126 | Symbol Sym; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 127 | std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand! |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 128 | size_t Pos = QName.rfind("::"); |
| 129 | if (Pos == llvm::StringRef::npos) { |
| 130 | Sym.Name = QName; |
| 131 | Sym.Scope = ""; |
| 132 | } else { |
| 133 | Sym.Name = QName.substr(Pos + 2); |
Sam McCall | 8b2faee | 2018-01-19 22:18:21 +0000 | [diff] [blame] | 134 | Sym.Scope = QName.substr(0, Pos + 2); |
| 135 | USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 136 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 137 | USR += Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func# |
| 138 | Sym.ID = SymbolID(USR); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 139 | Sym.SymInfo.Kind = Kind; |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 140 | Sym.IsIndexedForCodeCompletion = true; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 141 | Sym.Origin = SymbolOrigin::Static; |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 142 | return Sym; |
| 143 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 144 | Symbol func(StringRef Name) { // Assumes the function has no args. |
| 145 | return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args |
| 146 | } |
| 147 | Symbol cls(StringRef Name) { |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 148 | return sym(Name, index::SymbolKind::Class, "@S@\\0"); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 149 | } |
| 150 | Symbol var(StringRef Name) { |
| 151 | return sym(Name, index::SymbolKind::Variable, "@\\0"); |
| 152 | } |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 153 | Symbol ns(StringRef Name) { |
| 154 | return sym(Name, index::SymbolKind::Namespace, "@N@\\0"); |
| 155 | } |
| 156 | Symbol withReferences(int N, Symbol S) { |
| 157 | S.References = N; |
| 158 | return S; |
| 159 | } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 160 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 161 | TEST(CompletionTest, Limit) { |
| 162 | clangd::CodeCompleteOptions Opts; |
| 163 | Opts.Limit = 2; |
| 164 | auto Results = completions(R"cpp( |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 165 | struct ClassWithMembers { |
| 166 | int AAA(); |
| 167 | int BBB(); |
| 168 | int CCC(); |
| 169 | } |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 170 | int main() { ClassWithMembers().^ } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 171 | )cpp", |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 172 | /*IndexSymbols=*/{}, Opts); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 173 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 174 | EXPECT_TRUE(Results.HasMore); |
| 175 | EXPECT_THAT(Results.Completions, ElementsAre(Named("AAA"), Named("BBB"))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 178 | TEST(CompletionTest, Filter) { |
| 179 | std::string Body = R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 180 | #define MotorCar |
| 181 | int Car; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 182 | struct S { |
| 183 | int FooBar; |
| 184 | int FooBaz; |
| 185 | int Qux; |
| 186 | }; |
| 187 | )cpp"; |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 188 | |
| 189 | // Only items matching the fuzzy query are returned. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 190 | EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions, |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 191 | AllOf(Has("FooBar"), Has("FooBaz"), Not(Has("Qux")))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 192 | |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 193 | // Macros require prefix match. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 194 | EXPECT_THAT(completions(Body + "int main() { C^ }").Completions, |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 195 | AllOf(Has("Car"), Not(Has("MotorCar")))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 198 | void TestAfterDotCompletion(clangd::CodeCompleteOptions Opts) { |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 199 | auto Results = completions( |
| 200 | R"cpp( |
| 201 | #define MACRO X |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 202 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 203 | int global_var; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 204 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 205 | int global_func(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 206 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 207 | struct GlobalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 208 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 209 | struct ClassWithMembers { |
| 210 | /// Doc for method. |
| 211 | int method(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 212 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 213 | int field; |
| 214 | private: |
| 215 | int private_field; |
| 216 | }; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 217 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 218 | int test() { |
| 219 | struct LocalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 220 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 221 | /// Doc for local_var. |
| 222 | int local_var; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 223 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 224 | ClassWithMembers().^ |
| 225 | } |
| 226 | )cpp", |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 227 | {cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 228 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 229 | // Class members. The only items that must be present in after-dot |
| 230 | // completion. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 231 | EXPECT_THAT(Results.Completions, |
| 232 | AllOf(Has("method"), Has("field"), Not(Has("ClassWithMembers")), |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 233 | Not(Has("operator=")), Not(Has("~ClassWithMembers")))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 234 | EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 235 | Has("private_field")); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 236 | // Global items. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 237 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 238 | Results.Completions, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 239 | Not(AnyOf(Has("global_var"), Has("index_var"), Has("global_func"), |
| 240 | Has("global_func()"), Has("index_func"), Has("GlobalClass"), |
| 241 | Has("IndexClass"), Has("MACRO"), Has("LocalClass")))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 242 | // There should be no code patterns (aka snippets) in after-dot |
| 243 | // completion. At least there aren't any we're aware of. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 244 | EXPECT_THAT(Results.Completions, |
| 245 | Not(Contains(Kind(CompletionItemKind::Snippet)))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 246 | // Check documentation. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 247 | EXPECT_IFF(Opts.IncludeComments, Results.Completions, |
| 248 | Contains(IsDocumented())); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 249 | } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 250 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 251 | void TestGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) { |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 252 | auto Results = completions( |
| 253 | R"cpp( |
| 254 | #define MACRO X |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 255 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 256 | int global_var; |
| 257 | int global_func(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 258 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 259 | struct GlobalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 260 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 261 | struct ClassWithMembers { |
| 262 | /// Doc for method. |
| 263 | int method(); |
| 264 | }; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 265 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 266 | int test() { |
| 267 | struct LocalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 268 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 269 | /// Doc for local_var. |
| 270 | int local_var; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 271 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 272 | ^ |
| 273 | } |
| 274 | )cpp", |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 275 | {cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 276 | |
| 277 | // Class members. Should never be present in global completions. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 278 | EXPECT_THAT(Results.Completions, |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 279 | Not(AnyOf(Has("method"), Has("method()"), Has("field")))); |
| 280 | // Global items. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 281 | EXPECT_THAT(Results.Completions, |
| 282 | AllOf(Has("global_var"), Has("index_var"), Has("global_func"), |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 283 | Has("index_func" /* our fake symbol doesn't include () */), |
| 284 | Has("GlobalClass"), Has("IndexClass"))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 285 | // A macro. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 286 | EXPECT_IFF(Opts.IncludeMacros, Results.Completions, Has("MACRO")); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 287 | // Local items. Must be present always. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 288 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | 9b5ffc2 | 2017-12-12 12:56:46 +0000 | [diff] [blame] | 289 | AllOf(Has("local_var"), Has("LocalClass"), |
| 290 | Contains(Kind(CompletionItemKind::Snippet)))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 291 | // Check documentation. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 292 | EXPECT_IFF(Opts.IncludeComments, Results.Completions, |
| 293 | Contains(IsDocumented())); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST(CompletionTest, CompletionOptions) { |
Sam McCall | 2c3849a | 2018-01-16 12:21:24 +0000 | [diff] [blame] | 297 | auto Test = [&](const clangd::CodeCompleteOptions &Opts) { |
| 298 | TestAfterDotCompletion(Opts); |
| 299 | TestGlobalScopeCompletion(Opts); |
| 300 | }; |
| 301 | // We used to test every combination of options, but that got too slow (2^N). |
| 302 | auto Flags = { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 303 | &clangd::CodeCompleteOptions::IncludeMacros, |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 304 | &clangd::CodeCompleteOptions::IncludeComments, |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 305 | &clangd::CodeCompleteOptions::IncludeCodePatterns, |
| 306 | &clangd::CodeCompleteOptions::IncludeIneligibleResults, |
Sam McCall | 2c3849a | 2018-01-16 12:21:24 +0000 | [diff] [blame] | 307 | }; |
| 308 | // Test default options. |
| 309 | Test({}); |
| 310 | // Test with one flag flipped. |
| 311 | for (auto &F : Flags) { |
| 312 | clangd::CodeCompleteOptions O; |
| 313 | O.*F ^= true; |
| 314 | Test(O); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 318 | TEST(CompletionTest, Priorities) { |
| 319 | auto Internal = completions(R"cpp( |
| 320 | class Foo { |
| 321 | public: void pub(); |
| 322 | protected: void prot(); |
| 323 | private: void priv(); |
| 324 | }; |
| 325 | void Foo::pub() { this->^ } |
| 326 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 327 | EXPECT_THAT(Internal.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 328 | HasSubsequence(Named("priv"), Named("prot"), Named("pub"))); |
| 329 | |
| 330 | auto External = completions(R"cpp( |
| 331 | class Foo { |
| 332 | public: void pub(); |
| 333 | protected: void prot(); |
| 334 | private: void priv(); |
| 335 | }; |
| 336 | void test() { |
| 337 | Foo F; |
| 338 | F.^ |
| 339 | } |
| 340 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 341 | EXPECT_THAT(External.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 342 | AllOf(Has("pub"), Not(Has("prot")), Not(Has("priv")))); |
| 343 | } |
| 344 | |
| 345 | TEST(CompletionTest, Qualifiers) { |
| 346 | auto Results = completions(R"cpp( |
| 347 | class Foo { |
| 348 | public: int foo() const; |
| 349 | int bar() const; |
| 350 | }; |
| 351 | class Bar : public Foo { |
| 352 | int foo() const; |
| 353 | }; |
| 354 | void test() { Bar().^ } |
| 355 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 356 | EXPECT_THAT(Results.Completions, |
| 357 | HasSubsequence(AllOf(Qualifier(""), Named("bar")), |
| 358 | AllOf(Qualifier("Foo::"), Named("foo")))); |
| 359 | EXPECT_THAT(Results.Completions, |
| 360 | Not(Contains(AllOf(Qualifier(""), Named("foo"))))); // private |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 363 | TEST(CompletionTest, InjectedTypename) { |
| 364 | // These are suppressed when accessed as a member... |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 365 | EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 366 | Not(Has("X"))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 367 | EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 368 | Not(Has("X"))); |
| 369 | // ...but accessible in other, more useful cases. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 370 | EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions, |
| 371 | Has("X")); |
| 372 | EXPECT_THAT( |
| 373 | completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions, |
| 374 | Has("Y")); |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 375 | EXPECT_THAT( |
| 376 | completions( |
| 377 | "template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };") |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 378 | .Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 379 | Has("Y")); |
| 380 | // This case is marginal (`using X::X` is useful), we allow it for now. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 381 | EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions, |
| 382 | Has("X")); |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 385 | TEST(CompletionTest, Snippets) { |
| 386 | clangd::CodeCompleteOptions Opts; |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 387 | auto Results = completions( |
| 388 | R"cpp( |
| 389 | struct fake { |
| 390 | int a; |
| 391 | int f(int i, const float f) const; |
| 392 | }; |
| 393 | int main() { |
| 394 | fake f; |
| 395 | f.^ |
| 396 | } |
| 397 | )cpp", |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 398 | /*IndexSymbols=*/{}, Opts); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 399 | EXPECT_THAT( |
| 400 | Results.Completions, |
| 401 | HasSubsequence(Named("a"), |
| 402 | SnippetSuffix("(${1:int i}, ${2:const float f})"))); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | TEST(CompletionTest, Kinds) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 406 | auto Results = completions( |
| 407 | R"cpp( |
| 408 | #define MACRO X |
| 409 | int variable; |
| 410 | struct Struct {}; |
| 411 | int function(); |
| 412 | int X = ^ |
| 413 | )cpp", |
| 414 | {func("indexFunction"), var("indexVariable"), cls("indexClass")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 415 | EXPECT_THAT(Results.Completions, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 416 | AllOf(Has("function", CompletionItemKind::Function), |
| 417 | Has("variable", CompletionItemKind::Variable), |
| 418 | Has("int", CompletionItemKind::Keyword), |
| 419 | Has("Struct", CompletionItemKind::Class), |
| 420 | Has("MACRO", CompletionItemKind::Text), |
| 421 | Has("indexFunction", CompletionItemKind::Function), |
| 422 | Has("indexVariable", CompletionItemKind::Variable), |
| 423 | Has("indexClass", CompletionItemKind::Class))); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 424 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 425 | Results = completions("nam^"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 426 | EXPECT_THAT(Results.Completions, |
| 427 | Has("namespace", CompletionItemKind::Snippet)); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 430 | TEST(CompletionTest, NoDuplicates) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 431 | auto Results = completions( |
| 432 | R"cpp( |
| 433 | class Adapter { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 434 | }; |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 435 | |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 436 | void f() { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 437 | Adapter^ |
| 438 | } |
| 439 | )cpp", |
| 440 | {cls("Adapter")}); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 441 | |
| 442 | // Make sure there are no duplicate entries of 'Adapter'. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 443 | EXPECT_THAT(Results.Completions, ElementsAre(Named("Adapter"))); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 446 | TEST(CompletionTest, ScopedNoIndex) { |
| 447 | auto Results = completions( |
| 448 | R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 449 | namespace fake { int BigBang, Babble, Box; }; |
| 450 | int main() { fake::ba^ } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 451 | ")cpp"); |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 452 | // Babble is a better match than BigBang. Box doesn't match at all. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 453 | EXPECT_THAT(Results.Completions, |
| 454 | ElementsAre(Named("Babble"), Named("BigBang"))); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 457 | TEST(CompletionTest, Scoped) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 458 | auto Results = completions( |
| 459 | R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 460 | namespace fake { int Babble, Box; }; |
| 461 | int main() { fake::ba^ } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 462 | ")cpp", |
| 463 | {var("fake::BigBang")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 464 | EXPECT_THAT(Results.Completions, |
| 465 | ElementsAre(Named("Babble"), Named("BigBang"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 468 | TEST(CompletionTest, ScopedWithFilter) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 469 | auto Results = completions( |
| 470 | R"cpp( |
| 471 | void f() { ns::x^ } |
| 472 | )cpp", |
| 473 | {cls("ns::XYZ"), func("ns::foo")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 474 | EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("XYZ"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 477 | TEST(CompletionTest, ReferencesAffectRanking) { |
| 478 | auto Results = completions("int main() { abs^ }", {ns("absl"), func("abs")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 479 | EXPECT_THAT(Results.Completions, HasSubsequence(Named("abs"), Named("absl"))); |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 480 | Results = completions("int main() { abs^ }", |
| 481 | {withReferences(10000, ns("absl")), func("abs")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 482 | EXPECT_THAT(Results.Completions, HasSubsequence(Named("absl"), Named("abs"))); |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 485 | TEST(CompletionTest, GlobalQualified) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 486 | auto Results = completions( |
| 487 | R"cpp( |
| 488 | void f() { ::^ } |
| 489 | )cpp", |
| 490 | {cls("XYZ")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 491 | EXPECT_THAT(Results.Completions, |
| 492 | AllOf(Has("XYZ", CompletionItemKind::Class), |
| 493 | Has("f", CompletionItemKind::Function))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 496 | TEST(CompletionTest, FullyQualified) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 497 | auto Results = completions( |
| 498 | R"cpp( |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 499 | namespace ns { void bar(); } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 500 | void f() { ::ns::^ } |
| 501 | )cpp", |
| 502 | {cls("ns::XYZ")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 503 | EXPECT_THAT(Results.Completions, |
| 504 | AllOf(Has("XYZ", CompletionItemKind::Class), |
| 505 | Has("bar", CompletionItemKind::Function))); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | TEST(CompletionTest, SemaIndexMerge) { |
| 509 | auto Results = completions( |
| 510 | R"cpp( |
| 511 | namespace ns { int local; void both(); } |
| 512 | void f() { ::ns::^ } |
| 513 | )cpp", |
| 514 | {func("ns::both"), cls("ns::Index")}); |
| 515 | // We get results from both index and sema, with no duplicates. |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 516 | EXPECT_THAT(Results.Completions, |
| 517 | UnorderedElementsAre( |
| 518 | AllOf(Named("local"), Origin(SymbolOrigin::AST)), |
| 519 | AllOf(Named("Index"), Origin(SymbolOrigin::Static)), |
| 520 | AllOf(Named("both"), |
| 521 | Origin(SymbolOrigin::AST | SymbolOrigin::Static)))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 524 | TEST(CompletionTest, SemaIndexMergeWithLimit) { |
| 525 | clangd::CodeCompleteOptions Opts; |
| 526 | Opts.Limit = 1; |
| 527 | auto Results = completions( |
| 528 | R"cpp( |
| 529 | namespace ns { int local; void both(); } |
| 530 | void f() { ::ns::^ } |
| 531 | )cpp", |
| 532 | {func("ns::both"), cls("ns::Index")}, Opts); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 533 | EXPECT_EQ(Results.Completions.size(), Opts.Limit); |
| 534 | EXPECT_TRUE(Results.HasMore); |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 537 | TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) { |
| 538 | MockFSProvider FS; |
| 539 | MockCompilationDatabase CDB; |
| 540 | std::string Subdir = testPath("sub"); |
| 541 | std::string SearchDirArg = (llvm::Twine("-I") + Subdir).str(); |
| 542 | CDB.ExtraClangFlags = {SearchDirArg.c_str()}; |
| 543 | std::string BarHeader = testPath("sub/bar.h"); |
| 544 | FS.Files[BarHeader] = ""; |
| 545 | |
| 546 | IgnoreDiagnostics DiagConsumer; |
| 547 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 548 | Symbol::Details Scratch; |
| 549 | auto BarURI = URI::createFile(BarHeader).toString(); |
| 550 | Symbol Sym = cls("ns::X"); |
| 551 | Sym.CanonicalDeclaration.FileURI = BarURI; |
| 552 | Scratch.IncludeHeader = BarURI; |
| 553 | Sym.Detail = &Scratch; |
| 554 | // Shoten include path based on search dirctory and insert. |
| 555 | auto Results = completions(Server, |
| 556 | R"cpp( |
| 557 | int main() { ns::^ } |
| 558 | )cpp", |
| 559 | {Sym}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 560 | EXPECT_THAT(Results.Completions, |
| 561 | ElementsAre(AllOf(Named("X"), InsertInclude("\"bar.h\"")))); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 562 | // Duplicate based on inclusions in preamble. |
| 563 | Results = completions(Server, |
| 564 | R"cpp( |
| 565 | #include "sub/bar.h" // not shortest, so should only match resolved. |
| 566 | int main() { ns::^ } |
| 567 | )cpp", |
| 568 | {Sym}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 569 | EXPECT_THAT(Results.Completions, ElementsAre(AllOf(Named("X"), Labeled("X"), |
| 570 | Not(InsertInclude())))); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 573 | TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) { |
| 574 | MockFSProvider FS; |
| 575 | MockCompilationDatabase CDB; |
| 576 | |
| 577 | IgnoreDiagnostics DiagConsumer; |
| 578 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 579 | Symbol::Details Scratch; |
| 580 | Symbol SymX = cls("ns::X"); |
| 581 | Symbol SymY = cls("ns::Y"); |
| 582 | std::string BarHeader = testPath("bar.h"); |
| 583 | auto BarURI = URI::createFile(BarHeader).toString(); |
| 584 | SymX.CanonicalDeclaration.FileURI = BarURI; |
| 585 | SymY.CanonicalDeclaration.FileURI = BarURI; |
| 586 | Scratch.IncludeHeader = "<bar>"; |
| 587 | SymX.Detail = &Scratch; |
| 588 | SymY.Detail = &Scratch; |
| 589 | // Shoten include path based on search dirctory and insert. |
| 590 | auto Results = completions(Server, |
| 591 | R"cpp( |
| 592 | namespace ns { |
| 593 | class X; |
| 594 | class Y {} |
| 595 | } |
| 596 | int main() { ns::^ } |
| 597 | )cpp", |
| 598 | {SymX, SymY}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 599 | EXPECT_THAT(Results.Completions, |
| 600 | ElementsAre(AllOf(Named("X"), Not(InsertInclude())), |
| 601 | AllOf(Named("Y"), Not(InsertInclude())))); |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 604 | TEST(CompletionTest, IndexSuppressesPreambleCompletions) { |
| 605 | MockFSProvider FS; |
| 606 | MockCompilationDatabase CDB; |
| 607 | IgnoreDiagnostics DiagConsumer; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 608 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 609 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 610 | FS.Files[testPath("bar.h")] = |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 611 | R"cpp(namespace ns { struct preamble { int member; }; })cpp"; |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 612 | auto File = testPath("foo.cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 613 | Annotations Test(R"cpp( |
| 614 | #include "bar.h" |
| 615 | namespace ns { int local; } |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 616 | void f() { ns::^; } |
| 617 | void f() { ns::preamble().$2^; } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 618 | )cpp"); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 619 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 620 | clangd::CodeCompleteOptions Opts = {}; |
| 621 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 622 | auto I = memIndex({var("ns::index")}); |
| 623 | Opts.Index = I.get(); |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 624 | auto WithIndex = cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 625 | EXPECT_THAT(WithIndex.Completions, |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 626 | UnorderedElementsAre(Named("local"), Named("index"))); |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 627 | auto ClassFromPreamble = |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 628 | cantFail(runCodeComplete(Server, File, Test.point("2"), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 629 | EXPECT_THAT(ClassFromPreamble.Completions, Contains(Named("member"))); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 630 | |
| 631 | Opts.Index = nullptr; |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 632 | auto WithoutIndex = |
| 633 | cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 634 | EXPECT_THAT(WithoutIndex.Completions, |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 635 | UnorderedElementsAre(Named("local"), Named("preamble"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | TEST(CompletionTest, DynamicIndexMultiFile) { |
| 639 | MockFSProvider FS; |
| 640 | MockCompilationDatabase CDB; |
| 641 | IgnoreDiagnostics DiagConsumer; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 642 | auto Opts = ClangdServer::optsForTest(); |
| 643 | Opts.BuildDynamicSymbolIndex = true; |
| 644 | ClangdServer Server(CDB, FS, DiagConsumer, Opts); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 645 | |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 646 | FS.Files[testPath("foo.h")] = R"cpp( |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 647 | namespace ns { class XYZ {}; void foo(int x) {} } |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 648 | )cpp"; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 649 | runAddDocument(Server, testPath("foo.cpp"), R"cpp( |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 650 | #include "foo.h" |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 651 | )cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 652 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 653 | auto File = testPath("bar.cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 654 | Annotations Test(R"cpp( |
| 655 | namespace ns { |
| 656 | class XXX {}; |
| 657 | /// Doooc |
| 658 | void fooooo() {} |
| 659 | } |
| 660 | void f() { ns::^ } |
| 661 | )cpp"); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 662 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 663 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 664 | auto Results = cantFail(runCodeComplete(Server, File, Test.point(), {})); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 665 | // "XYZ" and "foo" are not included in the file being completed but are still |
| 666 | // visible through the index. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 667 | EXPECT_THAT(Results.Completions, Has("XYZ", CompletionItemKind::Class)); |
| 668 | EXPECT_THAT(Results.Completions, Has("foo", CompletionItemKind::Function)); |
| 669 | EXPECT_THAT(Results.Completions, Has("XXX", CompletionItemKind::Class)); |
| 670 | EXPECT_THAT(Results.Completions, |
| 671 | Contains((Named("fooooo"), Kind(CompletionItemKind::Function), |
| 672 | Doc("Doooc"), ReturnType("void")))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 675 | TEST(CompletionTest, Documentation) { |
| 676 | auto Results = completions( |
| 677 | R"cpp( |
| 678 | // Non-doxygen comment. |
| 679 | int foo(); |
| 680 | /// Doxygen comment. |
| 681 | /// \param int a |
| 682 | int bar(int a); |
| 683 | /* Multi-line |
| 684 | block comment |
| 685 | */ |
| 686 | int baz(); |
| 687 | |
| 688 | int x = ^ |
| 689 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 690 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 691 | Contains(AllOf(Named("foo"), Doc("Non-doxygen comment.")))); |
| 692 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 693 | Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 694 | Contains(AllOf(Named("bar"), Doc("Doxygen comment.\n\\param int a")))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 695 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 696 | Contains(AllOf(Named("baz"), Doc("Multi-line\nblock comment")))); |
| 697 | } |
| 698 | |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 699 | TEST(CompletionTest, GlobalCompletionFiltering) { |
| 700 | |
| 701 | Symbol Class = cls("XYZ"); |
| 702 | Class.IsIndexedForCodeCompletion = false; |
| 703 | Symbol Func = func("XYZ::foooo"); |
| 704 | Func.IsIndexedForCodeCompletion = false; |
| 705 | |
| 706 | auto Results = completions(R"(// void f() { |
| 707 | XYZ::foooo^ |
| 708 | })", |
| 709 | {Class, Func}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 710 | EXPECT_THAT(Results.Completions, IsEmpty()); |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Haojian Wu | 58d208d | 2018-01-25 09:44:06 +0000 | [diff] [blame] | 713 | TEST(CodeCompleteTest, DisableTypoCorrection) { |
| 714 | auto Results = completions(R"cpp( |
| 715 | namespace clang { int v; } |
| 716 | void f() { clangd::^ |
| 717 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 718 | EXPECT_TRUE(Results.Completions.empty()); |
Haojian Wu | 58d208d | 2018-01-25 09:44:06 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 721 | TEST(CodeCompleteTest, NoColonColonAtTheEnd) { |
| 722 | auto Results = completions(R"cpp( |
| 723 | namespace clang { } |
| 724 | void f() { |
| 725 | clan^ |
| 726 | } |
| 727 | )cpp"); |
| 728 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 729 | EXPECT_THAT(Results.Completions, Contains(Labeled("clang"))); |
| 730 | EXPECT_THAT(Results.Completions, Not(Contains(Labeled("clang::")))); |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 733 | TEST(CompletionTest, BacktrackCrashes) { |
| 734 | // Sema calls code completion callbacks twice in these cases. |
| 735 | auto Results = completions(R"cpp( |
| 736 | namespace ns { |
| 737 | struct FooBarBaz {}; |
| 738 | } // namespace ns |
| 739 | |
| 740 | int foo(ns::FooBar^ |
| 741 | )cpp"); |
| 742 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 743 | EXPECT_THAT(Results.Completions, ElementsAre(Labeled("FooBarBaz"))); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 744 | |
| 745 | // Check we don't crash in that case too. |
| 746 | completions(R"cpp( |
| 747 | struct FooBarBaz {}; |
| 748 | void test() { |
| 749 | if (FooBarBaz * x^) {} |
| 750 | } |
| 751 | )cpp"); |
| 752 | } |
| 753 | |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 754 | TEST(CompletionTest, CompleteInMacroWithStringification) { |
| 755 | auto Results = completions(R"cpp( |
| 756 | void f(const char *, int x); |
| 757 | #define F(x) f(#x, x) |
| 758 | |
| 759 | namespace ns { |
| 760 | int X; |
| 761 | int Y; |
| 762 | } // namespace ns |
| 763 | |
| 764 | int f(int input_num) { |
| 765 | F(ns::^) |
| 766 | } |
| 767 | )cpp"); |
| 768 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 769 | EXPECT_THAT(Results.Completions, |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 770 | UnorderedElementsAre(Named("X"), Named("Y"))); |
| 771 | } |
| 772 | |
| 773 | TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) { |
| 774 | auto Results = completions(R"cpp( |
| 775 | void f(const char *, int x); |
| 776 | #define F(x) f(#x, x) |
| 777 | |
| 778 | namespace ns { |
| 779 | int X; |
| 780 | |
| 781 | int f(int input_num) { |
| 782 | F(^) |
| 783 | } |
| 784 | } // namespace ns |
| 785 | )cpp"); |
| 786 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 787 | EXPECT_THAT(Results.Completions, Contains(Named("X"))); |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 790 | TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) { |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 791 | auto Results = completions(R"cpp( |
| 792 | int bar(int param_in_bar) { |
| 793 | } |
| 794 | |
| 795 | int foo(int param_in_foo) { |
| 796 | #if 0 |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 797 | // In recorvery mode, "param_in_foo" will also be suggested among many other |
| 798 | // unrelated symbols; however, this is really a special case where this works. |
| 799 | // If the #if block is outside of the function, "param_in_foo" is still |
| 800 | // suggested, but "bar" and "foo" are missing. So the recovery mode doesn't |
| 801 | // really provide useful results in excluded branches. |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 802 | par^ |
| 803 | #endif |
| 804 | } |
| 805 | )cpp"); |
| 806 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 807 | EXPECT_TRUE(Results.Completions.empty()); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 810 | SignatureHelp signatures(StringRef Text) { |
| 811 | MockFSProvider FS; |
| 812 | MockCompilationDatabase CDB; |
| 813 | IgnoreDiagnostics DiagConsumer; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 814 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 815 | auto File = testPath("foo.cpp"); |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 816 | Annotations Test(Text); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 817 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 818 | return cantFail(runSignatureHelp(Server, File, Test.point())); |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | MATCHER_P(ParamsAre, P, "") { |
| 822 | if (P.size() != arg.parameters.size()) |
| 823 | return false; |
| 824 | for (unsigned I = 0; I < P.size(); ++I) |
| 825 | if (P[I] != arg.parameters[I].label) |
| 826 | return false; |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | Matcher<SignatureInformation> Sig(std::string Label, |
| 831 | std::vector<std::string> Params) { |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 832 | return AllOf(SigHelpLabeled(Label), ParamsAre(Params)); |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | TEST(SignatureHelpTest, Overloads) { |
| 836 | auto Results = signatures(R"cpp( |
| 837 | void foo(int x, int y); |
| 838 | void foo(int x, float y); |
| 839 | void foo(float x, int y); |
| 840 | void foo(float x, float y); |
| 841 | void bar(int x, int y = 0); |
| 842 | int main() { foo(^); } |
| 843 | )cpp"); |
| 844 | EXPECT_THAT(Results.signatures, |
| 845 | UnorderedElementsAre( |
| 846 | Sig("foo(float x, float y) -> void", {"float x", "float y"}), |
| 847 | Sig("foo(float x, int y) -> void", {"float x", "int y"}), |
| 848 | Sig("foo(int x, float y) -> void", {"int x", "float y"}), |
| 849 | Sig("foo(int x, int y) -> void", {"int x", "int y"}))); |
| 850 | // We always prefer the first signature. |
| 851 | EXPECT_EQ(0, Results.activeSignature); |
| 852 | EXPECT_EQ(0, Results.activeParameter); |
| 853 | } |
| 854 | |
| 855 | TEST(SignatureHelpTest, DefaultArgs) { |
| 856 | auto Results = signatures(R"cpp( |
| 857 | void bar(int x, int y = 0); |
| 858 | void bar(float x = 0, int y = 42); |
| 859 | int main() { bar(^ |
| 860 | )cpp"); |
| 861 | EXPECT_THAT(Results.signatures, |
| 862 | UnorderedElementsAre( |
| 863 | Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}), |
| 864 | Sig("bar(float x = 0, int y = 42) -> void", |
| 865 | {"float x = 0", "int y = 42"}))); |
| 866 | EXPECT_EQ(0, Results.activeSignature); |
| 867 | EXPECT_EQ(0, Results.activeParameter); |
| 868 | } |
| 869 | |
| 870 | TEST(SignatureHelpTest, ActiveArg) { |
| 871 | auto Results = signatures(R"cpp( |
| 872 | int baz(int a, int b, int c); |
| 873 | int main() { baz(baz(1,2,3), ^); } |
| 874 | )cpp"); |
| 875 | EXPECT_THAT(Results.signatures, |
| 876 | ElementsAre(Sig("baz(int a, int b, int c) -> int", |
| 877 | {"int a", "int b", "int c"}))); |
| 878 | EXPECT_EQ(0, Results.activeSignature); |
| 879 | EXPECT_EQ(1, Results.activeParameter); |
| 880 | } |
| 881 | |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 882 | class IndexRequestCollector : public SymbolIndex { |
| 883 | public: |
| 884 | bool |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 885 | fuzzyFind(const FuzzyFindRequest &Req, |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 886 | llvm::function_ref<void(const Symbol &)> Callback) const override { |
| 887 | Requests.push_back(Req); |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 888 | return true; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Eric Liu | 9ec459f | 2018-03-14 09:48:05 +0000 | [diff] [blame] | 891 | void lookup(const LookupRequest &, |
| 892 | llvm::function_ref<void(const Symbol &)>) const override {} |
| 893 | |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 894 | const std::vector<FuzzyFindRequest> allRequests() const { return Requests; } |
| 895 | |
| 896 | private: |
| 897 | mutable std::vector<FuzzyFindRequest> Requests; |
| 898 | }; |
| 899 | |
| 900 | std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code) { |
| 901 | clangd::CodeCompleteOptions Opts; |
| 902 | IndexRequestCollector Requests; |
| 903 | Opts.Index = &Requests; |
| 904 | completions(Code, {}, Opts); |
| 905 | return Requests.allRequests(); |
| 906 | } |
| 907 | |
| 908 | TEST(CompletionTest, UnqualifiedIdQuery) { |
| 909 | auto Requests = captureIndexRequests(R"cpp( |
| 910 | namespace std {} |
| 911 | using namespace std; |
| 912 | namespace ns { |
| 913 | void f() { |
| 914 | vec^ |
| 915 | } |
| 916 | } |
| 917 | )cpp"); |
| 918 | |
| 919 | EXPECT_THAT(Requests, |
| 920 | ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 921 | UnorderedElementsAre("", "ns::", "std::")))); |
| 922 | } |
| 923 | |
| 924 | TEST(CompletionTest, ResolvedQualifiedIdQuery) { |
| 925 | auto Requests = captureIndexRequests(R"cpp( |
| 926 | namespace ns1 {} |
| 927 | namespace ns2 {} // ignore |
| 928 | namespace ns3 { namespace nns3 {} } |
| 929 | namespace foo { |
| 930 | using namespace ns1; |
| 931 | using namespace ns3::nns3; |
| 932 | } |
| 933 | namespace ns { |
| 934 | void f() { |
| 935 | foo::^ |
| 936 | } |
| 937 | } |
| 938 | )cpp"); |
| 939 | |
| 940 | EXPECT_THAT(Requests, |
| 941 | ElementsAre(Field( |
| 942 | &FuzzyFindRequest::Scopes, |
| 943 | UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::")))); |
| 944 | } |
| 945 | |
| 946 | TEST(CompletionTest, UnresolvedQualifierIdQuery) { |
| 947 | auto Requests = captureIndexRequests(R"cpp( |
| 948 | namespace a {} |
| 949 | using namespace a; |
| 950 | namespace ns { |
| 951 | void f() { |
| 952 | bar::^ |
| 953 | } |
| 954 | } // namespace ns |
| 955 | )cpp"); |
| 956 | |
| 957 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 958 | UnorderedElementsAre("bar::")))); |
| 959 | } |
| 960 | |
| 961 | TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) { |
| 962 | auto Requests = captureIndexRequests(R"cpp( |
| 963 | namespace a {} |
| 964 | using namespace a; |
| 965 | namespace ns { |
| 966 | void f() { |
| 967 | ::a::bar::^ |
| 968 | } |
| 969 | } // namespace ns |
| 970 | )cpp"); |
| 971 | |
| 972 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 973 | UnorderedElementsAre("a::bar::")))); |
| 974 | } |
| 975 | |
| 976 | TEST(CompletionTest, EmptyQualifiedQuery) { |
| 977 | auto Requests = captureIndexRequests(R"cpp( |
| 978 | namespace ns { |
| 979 | void f() { |
| 980 | ^ |
| 981 | } |
| 982 | } // namespace ns |
| 983 | )cpp"); |
| 984 | |
| 985 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 986 | UnorderedElementsAre("", "ns::")))); |
| 987 | } |
| 988 | |
| 989 | TEST(CompletionTest, GlobalQualifiedQuery) { |
| 990 | auto Requests = captureIndexRequests(R"cpp( |
| 991 | namespace ns { |
| 992 | void f() { |
| 993 | ::^ |
| 994 | } |
| 995 | } // namespace ns |
| 996 | )cpp"); |
| 997 | |
| 998 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 999 | UnorderedElementsAre("")))); |
| 1000 | } |
| 1001 | |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1002 | TEST(CompletionTest, NoIndexCompletionsInsideClasses) { |
| 1003 | auto Completions = completions( |
| 1004 | R"cpp( |
| 1005 | struct Foo { |
| 1006 | int SomeNameOfField; |
| 1007 | typedef int SomeNameOfTypedefField; |
| 1008 | }; |
| 1009 | |
| 1010 | Foo::^)cpp", |
| 1011 | {func("::SomeNameInTheIndex"), func("::Foo::SomeNameInTheIndex")}); |
| 1012 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1013 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1014 | AllOf(Contains(Labeled("SomeNameOfField")), |
| 1015 | Contains(Labeled("SomeNameOfTypedefField")), |
| 1016 | Not(Contains(Labeled("SomeNameInTheIndex"))))); |
| 1017 | } |
| 1018 | |
| 1019 | TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) { |
| 1020 | { |
| 1021 | auto Completions = completions( |
| 1022 | R"cpp( |
| 1023 | template <class T> |
| 1024 | void foo() { |
| 1025 | T::^ |
| 1026 | } |
| 1027 | )cpp", |
| 1028 | {func("::SomeNameInTheIndex")}); |
| 1029 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1030 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1031 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1032 | } |
| 1033 | |
| 1034 | { |
| 1035 | auto Completions = completions( |
| 1036 | R"cpp( |
| 1037 | template <class T> |
| 1038 | void foo() { |
| 1039 | T::template Y<int>::^ |
| 1040 | } |
| 1041 | )cpp", |
| 1042 | {func("::SomeNameInTheIndex")}); |
| 1043 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1044 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1045 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1046 | } |
| 1047 | |
| 1048 | { |
| 1049 | auto Completions = completions( |
| 1050 | R"cpp( |
| 1051 | template <class T> |
| 1052 | void foo() { |
| 1053 | T::foo::^ |
| 1054 | } |
| 1055 | )cpp", |
| 1056 | {func("::SomeNameInTheIndex")}); |
| 1057 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1058 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1059 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1060 | } |
| 1061 | } |
| 1062 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1063 | TEST(CompletionTest, OverloadBundling) { |
| 1064 | clangd::CodeCompleteOptions Opts; |
| 1065 | Opts.BundleOverloads = true; |
| 1066 | |
| 1067 | std::string Context = R"cpp( |
| 1068 | struct X { |
| 1069 | // Overload with int |
| 1070 | int a(int); |
| 1071 | // Overload with bool |
| 1072 | int a(bool); |
| 1073 | int b(float); |
| 1074 | }; |
| 1075 | int GFuncC(int); |
| 1076 | int GFuncD(int); |
| 1077 | )cpp"; |
| 1078 | |
| 1079 | // Member completions are bundled. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1080 | EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions, |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1081 | UnorderedElementsAre(Labeled("a(…)"), Labeled("b(float)"))); |
| 1082 | |
| 1083 | // Non-member completions are bundled, including index+sema. |
| 1084 | Symbol NoArgsGFunc = func("GFuncC"); |
| 1085 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1086 | completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1087 | UnorderedElementsAre(Labeled("GFuncC(…)"), Labeled("GFuncD(int)"))); |
| 1088 | |
| 1089 | // Differences in header-to-insert suppress bundling. |
| 1090 | Symbol::Details Detail; |
| 1091 | std::string DeclFile = URI::createFile(testPath("foo")).toString(); |
| 1092 | NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile; |
| 1093 | Detail.IncludeHeader = "<foo>"; |
| 1094 | NoArgsGFunc.Detail = &Detail; |
| 1095 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1096 | completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
| 1097 | UnorderedElementsAre(AllOf(Named("GFuncC"), InsertInclude("<foo>")), |
| 1098 | Labeled("GFuncC(int)"), Labeled("GFuncD(int)"))); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Examine a bundled completion in detail. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1101 | auto A = |
| 1102 | completions(Context + "int y = X().a^", {}, Opts).Completions.front(); |
| 1103 | EXPECT_EQ(A.Name, "a"); |
| 1104 | EXPECT_EQ(A.Signature, "(…)"); |
| 1105 | EXPECT_EQ(A.BundleSize, 2u); |
| 1106 | EXPECT_EQ(A.Kind, CompletionItemKind::Method); |
| 1107 | EXPECT_EQ(A.ReturnType, "int"); // All overloads return int. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1108 | // For now we just return one of the doc strings arbitrarily. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1109 | EXPECT_THAT(A.Documentation, AnyOf(HasSubstr("Overload with int"), |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1110 | HasSubstr("Overload with bool"))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1111 | EXPECT_EQ(A.SnippetSuffix, "(${0})"); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Ilya Biryukov | 30b04b1 | 2018-05-28 09:54:51 +0000 | [diff] [blame] | 1114 | TEST(CompletionTest, DocumentationFromChangedFileCrash) { |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1115 | MockFSProvider FS; |
| 1116 | auto FooH = testPath("foo.h"); |
| 1117 | auto FooCpp = testPath("foo.cpp"); |
| 1118 | FS.Files[FooH] = R"cpp( |
| 1119 | // this is my documentation comment. |
| 1120 | int func(); |
| 1121 | )cpp"; |
| 1122 | FS.Files[FooCpp] = ""; |
| 1123 | |
| 1124 | MockCompilationDatabase CDB; |
| 1125 | IgnoreDiagnostics DiagConsumer; |
| 1126 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1127 | |
| 1128 | Annotations Source(R"cpp( |
| 1129 | #include "foo.h" |
| 1130 | int func() { |
| 1131 | // This makes sure we have func from header in the AST. |
| 1132 | } |
| 1133 | int a = fun^ |
| 1134 | )cpp"); |
| 1135 | Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes); |
| 1136 | // We need to wait for preamble to build. |
| 1137 | ASSERT_TRUE(Server.blockUntilIdleForTest()); |
| 1138 | |
| 1139 | // Change the header file. Completion will reuse the old preamble! |
| 1140 | FS.Files[FooH] = R"cpp( |
| 1141 | int func(); |
| 1142 | )cpp"; |
| 1143 | |
| 1144 | clangd::CodeCompleteOptions Opts; |
| 1145 | Opts.IncludeComments = true; |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1146 | CodeCompleteResult Completions = |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1147 | cantFail(runCodeComplete(Server, FooCpp, Source.point(), Opts)); |
| 1148 | // We shouldn't crash. Unfortunately, current workaround is to not produce |
| 1149 | // comments for symbols from headers. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1150 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1151 | Contains(AllOf(Not(IsDocumented()), Named("func")))); |
| 1152 | } |
| 1153 | |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1154 | TEST(CompletionTest, NonDocComments) { |
| 1155 | MockFSProvider FS; |
| 1156 | auto FooCpp = testPath("foo.cpp"); |
| 1157 | FS.Files[FooCpp] = ""; |
| 1158 | |
| 1159 | MockCompilationDatabase CDB; |
| 1160 | IgnoreDiagnostics DiagConsumer; |
| 1161 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1162 | |
| 1163 | Annotations Source(R"cpp( |
Ilya Biryukov | da8dd8b | 2018-06-27 09:47:20 +0000 | [diff] [blame] | 1164 | // We ignore namespace comments, for rationale see CodeCompletionStrings.h. |
| 1165 | namespace comments_ns { |
| 1166 | } |
| 1167 | |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1168 | // ------------------ |
| 1169 | int comments_foo(); |
| 1170 | |
| 1171 | // A comment and a decl are separated by newlines. |
| 1172 | // Therefore, the comment shouldn't show up as doc comment. |
| 1173 | |
| 1174 | int comments_bar(); |
| 1175 | |
| 1176 | // this comment should be in the results. |
| 1177 | int comments_baz(); |
| 1178 | |
| 1179 | |
| 1180 | template <class T> |
| 1181 | struct Struct { |
| 1182 | int comments_qux(); |
| 1183 | int comments_quux(); |
| 1184 | }; |
| 1185 | |
| 1186 | |
| 1187 | // This comment should not be there. |
| 1188 | |
| 1189 | template <class T> |
| 1190 | int Struct<T>::comments_qux() { |
| 1191 | } |
| 1192 | |
| 1193 | // This comment **should** be in results. |
| 1194 | template <class T> |
| 1195 | int Struct<T>::comments_quux() { |
| 1196 | int a = comments^; |
| 1197 | } |
| 1198 | )cpp"); |
Reid Kleckner | 80274b1 | 2018-06-18 18:55:10 +0000 | [diff] [blame] | 1199 | // FIXME: Auto-completion in a template requires disabling delayed template |
| 1200 | // parsing. |
| 1201 | CDB.ExtraClangFlags.push_back("-fno-delayed-template-parsing"); |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1202 | Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1203 | CodeCompleteResult Completions = cantFail(runCodeComplete( |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1204 | Server, FooCpp, Source.point(), clangd::CodeCompleteOptions())); |
| 1205 | |
| 1206 | // We should not get any of those comments in completion. |
| 1207 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1208 | Completions.Completions, |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1209 | UnorderedElementsAre(AllOf(Not(IsDocumented()), Named("comments_foo")), |
| 1210 | AllOf(IsDocumented(), Named("comments_baz")), |
| 1211 | AllOf(IsDocumented(), Named("comments_quux")), |
Ilya Biryukov | da8dd8b | 2018-06-27 09:47:20 +0000 | [diff] [blame] | 1212 | AllOf(Not(IsDocumented()), Named("comments_ns")), |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1213 | // FIXME(ibiryukov): the following items should have |
| 1214 | // empty documentation, since they are separated from |
| 1215 | // a comment with an empty line. Unfortunately, I |
| 1216 | // couldn't make Sema tests pass if we ignore those. |
| 1217 | AllOf(IsDocumented(), Named("comments_bar")), |
| 1218 | AllOf(IsDocumented(), Named("comments_qux")))); |
| 1219 | } |
| 1220 | |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1221 | TEST(CompletionTest, CompleteOnInvalidLine) { |
| 1222 | auto FooCpp = testPath("foo.cpp"); |
| 1223 | |
| 1224 | MockCompilationDatabase CDB; |
| 1225 | IgnoreDiagnostics DiagConsumer; |
| 1226 | MockFSProvider FS; |
| 1227 | FS.Files[FooCpp] = "// empty file"; |
| 1228 | |
| 1229 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1230 | // Run completion outside the file range. |
| 1231 | Position Pos; |
| 1232 | Pos.line = 100; |
| 1233 | Pos.character = 0; |
| 1234 | EXPECT_THAT_EXPECTED( |
| 1235 | runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()), |
| 1236 | Failed()); |
| 1237 | } |
| 1238 | |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 1239 | TEST(CompletionTest, QualifiedNames) { |
| 1240 | auto Results = completions( |
| 1241 | R"cpp( |
| 1242 | namespace ns { int local; void both(); } |
| 1243 | void f() { ::ns::^ } |
| 1244 | )cpp", |
| 1245 | {func("ns::both"), cls("ns::Index")}); |
| 1246 | // We get results from both index and sema, with no duplicates. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1247 | EXPECT_THAT( |
| 1248 | Results.Completions, |
| 1249 | UnorderedElementsAre(Scope("ns::"), Scope("ns::"), Scope("ns::"))); |
| 1250 | } |
| 1251 | |
| 1252 | TEST(CompletionTest, Render) { |
| 1253 | CodeCompletion C; |
| 1254 | C.Name = "x"; |
| 1255 | C.Signature = "(bool) const"; |
| 1256 | C.SnippetSuffix = "(${0:bool})"; |
| 1257 | C.ReturnType = "int"; |
| 1258 | C.RequiredQualifier = "Foo::"; |
| 1259 | C.Scope = "ns::Foo::"; |
| 1260 | C.Documentation = "This is x()."; |
| 1261 | C.Header = "\"foo.h\""; |
| 1262 | C.Kind = CompletionItemKind::Method; |
| 1263 | C.Score.Total = 1.0; |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1264 | C.Origin = SymbolOrigin::AST | SymbolOrigin::Static; |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1265 | |
| 1266 | CodeCompleteOptions Opts; |
| 1267 | Opts.IncludeIndicator.Insert = "^"; |
| 1268 | Opts.IncludeIndicator.NoInsert = ""; |
| 1269 | Opts.EnableSnippets = false; |
| 1270 | |
| 1271 | auto R = C.render(Opts); |
| 1272 | EXPECT_EQ(R.label, "Foo::x(bool) const"); |
| 1273 | EXPECT_EQ(R.insertText, "Foo::x"); |
| 1274 | EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText); |
| 1275 | EXPECT_EQ(R.filterText, "x"); |
| 1276 | EXPECT_EQ(R.detail, "int\n\"foo.h\""); |
| 1277 | EXPECT_EQ(R.documentation, "This is x()."); |
| 1278 | EXPECT_THAT(R.additionalTextEdits, IsEmpty()); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1279 | EXPECT_EQ(R.sortText, sortText(1.0, "x")); |
| 1280 | |
| 1281 | Opts.EnableSnippets = true; |
| 1282 | R = C.render(Opts); |
| 1283 | EXPECT_EQ(R.insertText, "Foo::x(${0:bool})"); |
| 1284 | EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet); |
| 1285 | |
| 1286 | C.HeaderInsertion.emplace(); |
| 1287 | R = C.render(Opts); |
| 1288 | EXPECT_EQ(R.label, "^Foo::x(bool) const"); |
| 1289 | EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty())); |
| 1290 | |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1291 | Opts.ShowOrigins = true; |
| 1292 | R = C.render(Opts); |
| 1293 | EXPECT_EQ(R.label, "^[AS]Foo::x(bool) const"); |
| 1294 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1295 | C.BundleSize = 2; |
| 1296 | R = C.render(Opts); |
| 1297 | EXPECT_EQ(R.detail, "[2 overloads]\n\"foo.h\""); |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 1298 | } |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1299 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 1300 | TEST(CompletionTest, IgnoreRecoveryResults) { |
| 1301 | auto Results = completions( |
| 1302 | R"cpp( |
| 1303 | namespace ns { int NotRecovered() { return 0; } } |
| 1304 | void f() { |
| 1305 | // Sema enters recovery mode first and then normal mode. |
| 1306 | if (auto x = ns::NotRecover^) |
| 1307 | } |
| 1308 | )cpp"); |
| 1309 | EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("NotRecovered"))); |
| 1310 | } |
| 1311 | |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame^] | 1312 | TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) { |
| 1313 | auto Results = completions( |
| 1314 | R"cpp( |
| 1315 | namespace ns { |
| 1316 | class X { public: X(); int x_; }; |
| 1317 | X::X() : x_^(0) {} |
| 1318 | } |
| 1319 | )cpp"); |
| 1320 | EXPECT_THAT(Results.Completions, |
| 1321 | UnorderedElementsAre(AllOf(Scope("ns::X::"), Named("x_")))); |
| 1322 | } |
| 1323 | |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 1324 | } // namespace |
| 1325 | } // namespace clangd |
| 1326 | } // namespace clang |