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" |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Error.h" |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 23 | #include "llvm/Testing/Support/Error.h" |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 24 | #include "gmock/gmock.h" |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 25 | #include "gtest/gtest.h" |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace clangd { |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 29 | |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | using namespace llvm; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 32 | using ::testing::AllOf; |
| 33 | using ::testing::Contains; |
Ilya Biryukov | 5a5e1ca | 2017-12-29 14:59:22 +0000 | [diff] [blame] | 34 | using ::testing::Each; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 35 | using ::testing::ElementsAre; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 36 | using ::testing::Field; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 37 | using ::testing::HasSubstr; |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 38 | using ::testing::IsEmpty; |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 39 | using ::testing::Not; |
Sam McCall | 3d139c5 | 2018-01-12 18:30:08 +0000 | [diff] [blame] | 40 | using ::testing::UnorderedElementsAre; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 41 | |
| 42 | class IgnoreDiagnostics : public DiagnosticsConsumer { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 43 | void onDiagnosticsReady(PathRef File, |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 44 | std::vector<Diag> Diagnostics) override {} |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 47 | // GMock helpers for matching completion items. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 48 | MATCHER_P(Named, Name, "") { return arg.Name == Name; } |
| 49 | MATCHER_P(Scope, S, "") { return arg.Scope == S; } |
| 50 | MATCHER_P(Qualifier, Q, "") { return arg.RequiredQualifier == Q; } |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 51 | MATCHER_P(Labeled, Label, "") { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 52 | return arg.RequiredQualifier + arg.Name + arg.Signature == Label; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 53 | } |
| 54 | MATCHER_P(SigHelpLabeled, Label, "") { return arg.label == Label; } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 55 | MATCHER_P(Kind, K, "") { return arg.Kind == K; } |
| 56 | MATCHER_P(Doc, D, "") { return arg.Documentation == D; } |
| 57 | MATCHER_P(ReturnType, D, "") { return arg.ReturnType == D; } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 58 | MATCHER_P(InsertInclude, IncludeHeader, "") { |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 59 | return arg.Header == IncludeHeader && bool(arg.HeaderInsertion); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 60 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 61 | MATCHER(InsertInclude, "") { return bool(arg.HeaderInsertion); } |
| 62 | MATCHER_P(SnippetSuffix, Text, "") { return arg.SnippetSuffix == Text; } |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 63 | MATCHER_P(Origin, OriginSet, "") { return arg.Origin == OriginSet; } |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 64 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 65 | // Shorthand for Contains(Named(Name)). |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 66 | Matcher<const std::vector<CodeCompletion> &> Has(std::string Name) { |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 67 | return Contains(Named(std::move(Name))); |
| 68 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 69 | Matcher<const std::vector<CodeCompletion> &> Has(std::string Name, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 70 | CompletionItemKind K) { |
| 71 | return Contains(AllOf(Named(std::move(Name)), Kind(K))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 72 | } |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 73 | MATCHER(IsDocumented, "") { return !arg.Documentation.empty(); } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 74 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 75 | std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) { |
| 76 | SymbolSlab::Builder Slab; |
| 77 | for (const auto &Sym : Symbols) |
| 78 | Slab.insert(Sym); |
Haojian Wu | e8064b6 | 2018-08-31 19:53:37 +0000 | [diff] [blame] | 79 | return MemIndex::build(std::move(Slab).build(), |
| 80 | SymbolOccurrenceSlab::createEmpty()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 83 | CodeCompleteResult completions(ClangdServer &Server, StringRef TestCode, |
| 84 | Position point, |
| 85 | std::vector<Symbol> IndexSymbols = {}, |
| 86 | clangd::CodeCompleteOptions Opts = {}) { |
| 87 | std::unique_ptr<SymbolIndex> OverrideIndex; |
| 88 | if (!IndexSymbols.empty()) { |
| 89 | assert(!Opts.Index && "both Index and IndexSymbols given!"); |
| 90 | OverrideIndex = memIndex(std::move(IndexSymbols)); |
| 91 | Opts.Index = OverrideIndex.get(); |
| 92 | } |
| 93 | |
| 94 | auto File = testPath("foo.cpp"); |
| 95 | runAddDocument(Server, File, TestCode); |
| 96 | auto CompletionList = cantFail(runCodeComplete(Server, File, point, Opts)); |
| 97 | return CompletionList; |
| 98 | } |
| 99 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 100 | CodeCompleteResult completions(ClangdServer &Server, StringRef Text, |
| 101 | std::vector<Symbol> IndexSymbols = {}, |
| 102 | clangd::CodeCompleteOptions Opts = {}) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 103 | std::unique_ptr<SymbolIndex> OverrideIndex; |
| 104 | if (!IndexSymbols.empty()) { |
| 105 | assert(!Opts.Index && "both Index and IndexSymbols given!"); |
| 106 | OverrideIndex = memIndex(std::move(IndexSymbols)); |
| 107 | Opts.Index = OverrideIndex.get(); |
| 108 | } |
| 109 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 110 | auto File = testPath("foo.cpp"); |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 111 | Annotations Test(Text); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 112 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 113 | auto CompletionList = |
| 114 | cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Ilya Biryukov | 5a5e1ca | 2017-12-29 14:59:22 +0000 | [diff] [blame] | 115 | return CompletionList; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 118 | // Builds a server and runs code completion. |
| 119 | // 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] | 120 | CodeCompleteResult completions(StringRef Text, |
| 121 | std::vector<Symbol> IndexSymbols = {}, |
| 122 | clangd::CodeCompleteOptions Opts = {}) { |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 123 | MockFSProvider FS; |
| 124 | MockCompilationDatabase CDB; |
| 125 | IgnoreDiagnostics DiagConsumer; |
| 126 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 127 | return completions(Server, Text, std::move(IndexSymbols), std::move(Opts)); |
| 128 | } |
| 129 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 130 | std::string replace(StringRef Haystack, StringRef Needle, StringRef Repl) { |
| 131 | std::string Result; |
| 132 | raw_string_ostream OS(Result); |
| 133 | std::pair<StringRef, StringRef> Split; |
| 134 | for (Split = Haystack.split(Needle); !Split.second.empty(); |
| 135 | Split = Split.first.split(Needle)) |
| 136 | OS << Split.first << Repl; |
| 137 | Result += Split.first; |
| 138 | OS.flush(); |
| 139 | return Result; |
| 140 | } |
| 141 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 142 | // Helpers to produce fake index symbols for memIndex() or completions(). |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 143 | // USRFormat is a regex replacement string for the unqualified part of the USR. |
| 144 | Symbol sym(StringRef QName, index::SymbolKind Kind, StringRef USRFormat) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 145 | Symbol Sym; |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 146 | 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] | 147 | size_t Pos = QName.rfind("::"); |
| 148 | if (Pos == llvm::StringRef::npos) { |
| 149 | Sym.Name = QName; |
| 150 | Sym.Scope = ""; |
| 151 | } else { |
| 152 | Sym.Name = QName.substr(Pos + 2); |
Sam McCall | 8b2faee | 2018-01-19 22:18:21 +0000 | [diff] [blame] | 153 | Sym.Scope = QName.substr(0, Pos + 2); |
| 154 | USR += "@N@" + replace(QName.substr(0, Pos), "::", "@N@"); // ns:: -> @N@ns |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 155 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 156 | USR += Regex("^.*$").sub(USRFormat, Sym.Name); // e.g. func -> @F@func# |
| 157 | Sym.ID = SymbolID(USR); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 158 | Sym.SymInfo.Kind = Kind; |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 159 | Sym.IsIndexedForCodeCompletion = true; |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 160 | Sym.Origin = SymbolOrigin::Static; |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 161 | return Sym; |
| 162 | } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 163 | Symbol func(StringRef Name) { // Assumes the function has no args. |
| 164 | return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args |
| 165 | } |
| 166 | Symbol cls(StringRef Name) { |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 167 | return sym(Name, index::SymbolKind::Class, "@S@\\0"); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 168 | } |
| 169 | Symbol var(StringRef Name) { |
| 170 | return sym(Name, index::SymbolKind::Variable, "@\\0"); |
| 171 | } |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 172 | Symbol ns(StringRef Name) { |
| 173 | return sym(Name, index::SymbolKind::Namespace, "@N@\\0"); |
| 174 | } |
| 175 | Symbol withReferences(int N, Symbol S) { |
| 176 | S.References = N; |
| 177 | return S; |
| 178 | } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 179 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 180 | TEST(CompletionTest, Limit) { |
| 181 | clangd::CodeCompleteOptions Opts; |
| 182 | Opts.Limit = 2; |
| 183 | auto Results = completions(R"cpp( |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 184 | struct ClassWithMembers { |
| 185 | int AAA(); |
| 186 | int BBB(); |
| 187 | int CCC(); |
| 188 | } |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 189 | int main() { ClassWithMembers().^ } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 190 | )cpp", |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 191 | /*IndexSymbols=*/{}, Opts); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 192 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 193 | EXPECT_TRUE(Results.HasMore); |
| 194 | EXPECT_THAT(Results.Completions, ElementsAre(Named("AAA"), Named("BBB"))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 197 | TEST(CompletionTest, Filter) { |
| 198 | std::string Body = R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 199 | #define MotorCar |
| 200 | int Car; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 201 | struct S { |
| 202 | int FooBar; |
| 203 | int FooBaz; |
| 204 | int Qux; |
| 205 | }; |
| 206 | )cpp"; |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 207 | |
| 208 | // Only items matching the fuzzy query are returned. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 209 | EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions, |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 210 | AllOf(Has("FooBar"), Has("FooBaz"), Not(Has("Qux")))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 211 | |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 212 | // Macros require prefix match. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 213 | EXPECT_THAT(completions(Body + "int main() { C^ }").Completions, |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 214 | AllOf(Has("Car"), Not(Has("MotorCar")))); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 217 | void TestAfterDotCompletion(clangd::CodeCompleteOptions Opts) { |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 218 | auto Results = completions( |
| 219 | R"cpp( |
| 220 | #define MACRO X |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 221 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 222 | int global_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 | int global_func(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 225 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 226 | struct GlobalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 227 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 228 | struct ClassWithMembers { |
| 229 | /// Doc for method. |
| 230 | int method(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 231 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 232 | int field; |
| 233 | private: |
| 234 | int private_field; |
| 235 | }; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 236 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 237 | int test() { |
| 238 | struct LocalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 239 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 240 | /// Doc for local_var. |
| 241 | int local_var; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 242 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 243 | ClassWithMembers().^ |
| 244 | } |
| 245 | )cpp", |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 246 | {cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 247 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 248 | // Class members. The only items that must be present in after-dot |
| 249 | // completion. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 250 | EXPECT_THAT(Results.Completions, |
| 251 | AllOf(Has("method"), Has("field"), Not(Has("ClassWithMembers")), |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 252 | Not(Has("operator=")), Not(Has("~ClassWithMembers")))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 253 | EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 254 | Has("private_field")); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 255 | // Global items. |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 256 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 257 | Results.Completions, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 258 | Not(AnyOf(Has("global_var"), Has("index_var"), Has("global_func"), |
| 259 | Has("global_func()"), Has("index_func"), Has("GlobalClass"), |
| 260 | Has("IndexClass"), Has("MACRO"), Has("LocalClass")))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 261 | // There should be no code patterns (aka snippets) in after-dot |
| 262 | // completion. At least there aren't any we're aware of. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 263 | EXPECT_THAT(Results.Completions, |
| 264 | Not(Contains(Kind(CompletionItemKind::Snippet)))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 265 | // Check documentation. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 266 | EXPECT_IFF(Opts.IncludeComments, Results.Completions, |
| 267 | Contains(IsDocumented())); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 268 | } |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 269 | |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 270 | void TestGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) { |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 271 | auto Results = completions( |
| 272 | R"cpp( |
| 273 | #define MACRO X |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 274 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 275 | int global_var; |
| 276 | int global_func(); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 277 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 278 | struct GlobalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 279 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 280 | struct ClassWithMembers { |
| 281 | /// Doc for method. |
| 282 | int method(); |
| 283 | }; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 284 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 285 | int test() { |
| 286 | struct LocalClass {}; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 287 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 288 | /// Doc for local_var. |
| 289 | int local_var; |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 290 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 291 | ^ |
| 292 | } |
| 293 | )cpp", |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 294 | {cls("IndexClass"), var("index_var"), func("index_func")}, Opts); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 295 | |
| 296 | // Class members. Should never be present in global completions. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 297 | EXPECT_THAT(Results.Completions, |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 298 | Not(AnyOf(Has("method"), Has("method()"), Has("field")))); |
| 299 | // Global items. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 300 | EXPECT_THAT(Results.Completions, |
| 301 | AllOf(Has("global_var"), Has("index_var"), Has("global_func"), |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 302 | Has("index_func" /* our fake symbol doesn't include () */), |
| 303 | Has("GlobalClass"), Has("IndexClass"))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 304 | // A macro. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 305 | EXPECT_IFF(Opts.IncludeMacros, Results.Completions, Has("MACRO")); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 306 | // Local items. Must be present always. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 307 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | 9b5ffc2 | 2017-12-12 12:56:46 +0000 | [diff] [blame] | 308 | AllOf(Has("local_var"), Has("LocalClass"), |
| 309 | Contains(Kind(CompletionItemKind::Snippet)))); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 310 | // Check documentation. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 311 | EXPECT_IFF(Opts.IncludeComments, Results.Completions, |
| 312 | Contains(IsDocumented())); |
Sam McCall | f6ae323 | 2017-12-05 20:11:29 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST(CompletionTest, CompletionOptions) { |
Sam McCall | 2c3849a | 2018-01-16 12:21:24 +0000 | [diff] [blame] | 316 | auto Test = [&](const clangd::CodeCompleteOptions &Opts) { |
| 317 | TestAfterDotCompletion(Opts); |
| 318 | TestGlobalScopeCompletion(Opts); |
| 319 | }; |
| 320 | // We used to test every combination of options, but that got too slow (2^N). |
| 321 | auto Flags = { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 322 | &clangd::CodeCompleteOptions::IncludeMacros, |
Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 323 | &clangd::CodeCompleteOptions::IncludeComments, |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 324 | &clangd::CodeCompleteOptions::IncludeCodePatterns, |
| 325 | &clangd::CodeCompleteOptions::IncludeIneligibleResults, |
Sam McCall | 2c3849a | 2018-01-16 12:21:24 +0000 | [diff] [blame] | 326 | }; |
| 327 | // Test default options. |
| 328 | Test({}); |
| 329 | // Test with one flag flipped. |
| 330 | for (auto &F : Flags) { |
| 331 | clangd::CodeCompleteOptions O; |
| 332 | O.*F ^= true; |
| 333 | Test(O); |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 337 | TEST(CompletionTest, Priorities) { |
| 338 | auto Internal = completions(R"cpp( |
| 339 | class Foo { |
| 340 | public: void pub(); |
| 341 | protected: void prot(); |
| 342 | private: void priv(); |
| 343 | }; |
| 344 | void Foo::pub() { this->^ } |
| 345 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 346 | EXPECT_THAT(Internal.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 347 | HasSubsequence(Named("priv"), Named("prot"), Named("pub"))); |
| 348 | |
| 349 | auto External = completions(R"cpp( |
| 350 | class Foo { |
| 351 | public: void pub(); |
| 352 | protected: void prot(); |
| 353 | private: void priv(); |
| 354 | }; |
| 355 | void test() { |
| 356 | Foo F; |
| 357 | F.^ |
| 358 | } |
| 359 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 360 | EXPECT_THAT(External.Completions, |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 361 | AllOf(Has("pub"), Not(Has("prot")), Not(Has("priv")))); |
| 362 | } |
| 363 | |
| 364 | TEST(CompletionTest, Qualifiers) { |
| 365 | auto Results = completions(R"cpp( |
| 366 | class Foo { |
| 367 | public: int foo() const; |
| 368 | int bar() const; |
| 369 | }; |
| 370 | class Bar : public Foo { |
| 371 | int foo() const; |
| 372 | }; |
| 373 | void test() { Bar().^ } |
| 374 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 375 | EXPECT_THAT(Results.Completions, |
| 376 | HasSubsequence(AllOf(Qualifier(""), Named("bar")), |
| 377 | AllOf(Qualifier("Foo::"), Named("foo")))); |
| 378 | EXPECT_THAT(Results.Completions, |
| 379 | Not(Contains(AllOf(Qualifier(""), Named("foo"))))); // private |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 382 | TEST(CompletionTest, InjectedTypename) { |
| 383 | // These are suppressed when accessed as a member... |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 384 | EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 385 | Not(Has("X"))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 386 | EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 387 | Not(Has("X"))); |
| 388 | // ...but accessible in other, more useful cases. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 389 | EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions, |
| 390 | Has("X")); |
| 391 | EXPECT_THAT( |
| 392 | completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions, |
| 393 | Has("Y")); |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 394 | EXPECT_THAT( |
| 395 | completions( |
| 396 | "template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };") |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 397 | .Completions, |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 398 | Has("Y")); |
| 399 | // 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] | 400 | EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions, |
| 401 | Has("X")); |
Sam McCall | 4caa851 | 2018-06-07 12:49:17 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 404 | TEST(CompletionTest, Snippets) { |
| 405 | clangd::CodeCompleteOptions Opts; |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 406 | auto Results = completions( |
| 407 | R"cpp( |
| 408 | struct fake { |
| 409 | int a; |
| 410 | int f(int i, const float f) const; |
| 411 | }; |
| 412 | int main() { |
| 413 | fake f; |
| 414 | f.^ |
| 415 | } |
| 416 | )cpp", |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 417 | /*IndexSymbols=*/{}, Opts); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 418 | EXPECT_THAT( |
| 419 | Results.Completions, |
| 420 | HasSubsequence(Named("a"), |
| 421 | SnippetSuffix("(${1:int i}, ${2:const float f})"))); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST(CompletionTest, Kinds) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 425 | auto Results = completions( |
| 426 | R"cpp( |
| 427 | #define MACRO X |
| 428 | int variable; |
| 429 | struct Struct {}; |
| 430 | int function(); |
| 431 | int X = ^ |
| 432 | )cpp", |
| 433 | {func("indexFunction"), var("indexVariable"), cls("indexClass")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 434 | EXPECT_THAT(Results.Completions, |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 435 | AllOf(Has("function", CompletionItemKind::Function), |
| 436 | Has("variable", CompletionItemKind::Variable), |
| 437 | Has("int", CompletionItemKind::Keyword), |
| 438 | Has("Struct", CompletionItemKind::Class), |
| 439 | Has("MACRO", CompletionItemKind::Text), |
| 440 | Has("indexFunction", CompletionItemKind::Function), |
| 441 | Has("indexVariable", CompletionItemKind::Variable), |
| 442 | Has("indexClass", CompletionItemKind::Class))); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 443 | |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 444 | Results = completions("nam^"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 445 | EXPECT_THAT(Results.Completions, |
| 446 | Has("namespace", CompletionItemKind::Snippet)); |
Sam McCall | 44fdcec2 | 2017-12-08 15:00:59 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 449 | TEST(CompletionTest, NoDuplicates) { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 450 | auto Results = completions( |
| 451 | R"cpp( |
| 452 | class Adapter { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 453 | }; |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 454 | |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 455 | void f() { |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 456 | Adapter^ |
| 457 | } |
| 458 | )cpp", |
| 459 | {cls("Adapter")}); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 460 | |
| 461 | // Make sure there are no duplicate entries of 'Adapter'. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 462 | EXPECT_THAT(Results.Completions, ElementsAre(Named("Adapter"))); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 465 | TEST(CompletionTest, ScopedNoIndex) { |
| 466 | auto Results = completions( |
| 467 | R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 468 | namespace fake { int BigBang, Babble, Box; }; |
| 469 | int main() { fake::ba^ } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 470 | ")cpp"); |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 471 | // 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] | 472 | EXPECT_THAT(Results.Completions, |
| 473 | ElementsAre(Named("Babble"), Named("BigBang"))); |
Sam McCall | 84652cc | 2018-01-12 16:16:09 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 476 | TEST(CompletionTest, Scoped) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 477 | auto Results = completions( |
| 478 | R"cpp( |
Sam McCall | 8b2dcc1 | 2018-06-14 13:50:30 +0000 | [diff] [blame] | 479 | namespace fake { int Babble, Box; }; |
| 480 | int main() { fake::ba^ } |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 481 | ")cpp", |
| 482 | {var("fake::BigBang")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 483 | EXPECT_THAT(Results.Completions, |
| 484 | ElementsAre(Named("Babble"), Named("BigBang"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 487 | TEST(CompletionTest, ScopedWithFilter) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 488 | auto Results = completions( |
| 489 | R"cpp( |
| 490 | void f() { ns::x^ } |
| 491 | )cpp", |
| 492 | {cls("ns::XYZ"), func("ns::foo")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 493 | EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("XYZ"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 496 | TEST(CompletionTest, ReferencesAffectRanking) { |
Eric Liu | 84bd5db | 2018-07-25 11:26:35 +0000 | [diff] [blame] | 497 | auto Results = completions("int main() { abs^ }", {ns("absl"), func("absb")}); |
| 498 | EXPECT_THAT(Results.Completions, HasSubsequence(Named("absb"), Named("absl"))); |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 499 | Results = completions("int main() { abs^ }", |
Eric Liu | 84bd5db | 2018-07-25 11:26:35 +0000 | [diff] [blame] | 500 | {withReferences(10000, ns("absl")), func("absb")}); |
| 501 | EXPECT_THAT(Results.Completions, |
| 502 | HasSubsequence(Named("absl"), Named("absb"))); |
Sam McCall | dc8abc4 | 2018-05-03 14:53:02 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 505 | TEST(CompletionTest, GlobalQualified) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 506 | auto Results = completions( |
| 507 | R"cpp( |
| 508 | void f() { ::^ } |
| 509 | )cpp", |
| 510 | {cls("XYZ")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 511 | EXPECT_THAT(Results.Completions, |
| 512 | AllOf(Has("XYZ", CompletionItemKind::Class), |
| 513 | Has("f", CompletionItemKind::Function))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 516 | TEST(CompletionTest, FullyQualified) { |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 517 | auto Results = completions( |
| 518 | R"cpp( |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 519 | namespace ns { void bar(); } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 520 | void f() { ::ns::^ } |
| 521 | )cpp", |
| 522 | {cls("ns::XYZ")}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 523 | EXPECT_THAT(Results.Completions, |
| 524 | AllOf(Has("XYZ", CompletionItemKind::Class), |
| 525 | Has("bar", CompletionItemKind::Function))); |
Sam McCall | 545a20d | 2018-01-19 14:34:02 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | TEST(CompletionTest, SemaIndexMerge) { |
| 529 | auto Results = completions( |
| 530 | R"cpp( |
| 531 | namespace ns { int local; void both(); } |
| 532 | void f() { ::ns::^ } |
| 533 | )cpp", |
| 534 | {func("ns::both"), cls("ns::Index")}); |
| 535 | // We get results from both index and sema, with no duplicates. |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 536 | EXPECT_THAT(Results.Completions, |
| 537 | UnorderedElementsAre( |
| 538 | AllOf(Named("local"), Origin(SymbolOrigin::AST)), |
| 539 | AllOf(Named("Index"), Origin(SymbolOrigin::Static)), |
| 540 | AllOf(Named("both"), |
| 541 | Origin(SymbolOrigin::AST | SymbolOrigin::Static)))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 544 | TEST(CompletionTest, SemaIndexMergeWithLimit) { |
| 545 | clangd::CodeCompleteOptions Opts; |
| 546 | Opts.Limit = 1; |
| 547 | auto Results = completions( |
| 548 | R"cpp( |
| 549 | namespace ns { int local; void both(); } |
| 550 | void f() { ::ns::^ } |
| 551 | )cpp", |
| 552 | {func("ns::both"), cls("ns::Index")}, Opts); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 553 | EXPECT_EQ(Results.Completions.size(), Opts.Limit); |
| 554 | EXPECT_TRUE(Results.HasMore); |
Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 557 | TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) { |
| 558 | MockFSProvider FS; |
| 559 | MockCompilationDatabase CDB; |
| 560 | std::string Subdir = testPath("sub"); |
| 561 | std::string SearchDirArg = (llvm::Twine("-I") + Subdir).str(); |
| 562 | CDB.ExtraClangFlags = {SearchDirArg.c_str()}; |
| 563 | std::string BarHeader = testPath("sub/bar.h"); |
| 564 | FS.Files[BarHeader] = ""; |
| 565 | |
| 566 | IgnoreDiagnostics DiagConsumer; |
| 567 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 568 | auto BarURI = URI::createFile(BarHeader).toString(); |
| 569 | Symbol Sym = cls("ns::X"); |
| 570 | Sym.CanonicalDeclaration.FileURI = BarURI; |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 571 | Sym.IncludeHeader = BarURI; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 572 | // Shoten include path based on search dirctory and insert. |
| 573 | auto Results = completions(Server, |
| 574 | R"cpp( |
| 575 | int main() { ns::^ } |
| 576 | )cpp", |
| 577 | {Sym}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 578 | EXPECT_THAT(Results.Completions, |
| 579 | ElementsAre(AllOf(Named("X"), InsertInclude("\"bar.h\"")))); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 580 | // Duplicate based on inclusions in preamble. |
| 581 | Results = completions(Server, |
| 582 | R"cpp( |
| 583 | #include "sub/bar.h" // not shortest, so should only match resolved. |
| 584 | int main() { ns::^ } |
| 585 | )cpp", |
| 586 | {Sym}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 587 | EXPECT_THAT(Results.Completions, ElementsAre(AllOf(Named("X"), Labeled("X"), |
| 588 | Not(InsertInclude())))); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 591 | TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) { |
| 592 | MockFSProvider FS; |
| 593 | MockCompilationDatabase CDB; |
| 594 | |
| 595 | IgnoreDiagnostics DiagConsumer; |
| 596 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 597 | Symbol SymX = cls("ns::X"); |
| 598 | Symbol SymY = cls("ns::Y"); |
| 599 | std::string BarHeader = testPath("bar.h"); |
| 600 | auto BarURI = URI::createFile(BarHeader).toString(); |
| 601 | SymX.CanonicalDeclaration.FileURI = BarURI; |
| 602 | SymY.CanonicalDeclaration.FileURI = BarURI; |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 603 | SymX.IncludeHeader = "<bar>"; |
| 604 | SymY.IncludeHeader = "<bar>"; |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 605 | // Shoten include path based on search dirctory and insert. |
| 606 | auto Results = completions(Server, |
| 607 | R"cpp( |
| 608 | namespace ns { |
| 609 | class X; |
| 610 | class Y {} |
| 611 | } |
| 612 | int main() { ns::^ } |
| 613 | )cpp", |
| 614 | {SymX, SymY}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 615 | EXPECT_THAT(Results.Completions, |
| 616 | ElementsAre(AllOf(Named("X"), Not(InsertInclude())), |
| 617 | AllOf(Named("Y"), Not(InsertInclude())))); |
Eric Liu | 9b3cba7 | 2018-05-30 09:03:39 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 620 | TEST(CompletionTest, IndexSuppressesPreambleCompletions) { |
| 621 | MockFSProvider FS; |
| 622 | MockCompilationDatabase CDB; |
| 623 | IgnoreDiagnostics DiagConsumer; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 624 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 625 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 626 | FS.Files[testPath("bar.h")] = |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 627 | R"cpp(namespace ns { struct preamble { int member; }; })cpp"; |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 628 | auto File = testPath("foo.cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 629 | Annotations Test(R"cpp( |
| 630 | #include "bar.h" |
| 631 | namespace ns { int local; } |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 632 | void f() { ns::^; } |
| 633 | void f() { ns::preamble().$2^; } |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 634 | )cpp"); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 635 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 636 | clangd::CodeCompleteOptions Opts = {}; |
| 637 | |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 638 | auto I = memIndex({var("ns::index")}); |
| 639 | Opts.Index = I.get(); |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 640 | auto WithIndex = cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 641 | EXPECT_THAT(WithIndex.Completions, |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 642 | UnorderedElementsAre(Named("local"), Named("index"))); |
Sam McCall | d5ea3e3 | 2018-01-24 17:53:32 +0000 | [diff] [blame] | 643 | auto ClassFromPreamble = |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 644 | cantFail(runCodeComplete(Server, File, Test.point("2"), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 645 | EXPECT_THAT(ClassFromPreamble.Completions, Contains(Named("member"))); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 646 | |
| 647 | Opts.Index = nullptr; |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 648 | auto WithoutIndex = |
| 649 | cantFail(runCodeComplete(Server, File, Test.point(), Opts)); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 650 | EXPECT_THAT(WithoutIndex.Completions, |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 651 | UnorderedElementsAre(Named("local"), Named("preamble"))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | TEST(CompletionTest, DynamicIndexMultiFile) { |
| 655 | MockFSProvider FS; |
| 656 | MockCompilationDatabase CDB; |
| 657 | IgnoreDiagnostics DiagConsumer; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 658 | auto Opts = ClangdServer::optsForTest(); |
| 659 | Opts.BuildDynamicSymbolIndex = true; |
| 660 | ClangdServer Server(CDB, FS, DiagConsumer, Opts); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 661 | |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 662 | FS.Files[testPath("foo.h")] = R"cpp( |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 663 | namespace ns { class XYZ {}; void foo(int x) {} } |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 664 | )cpp"; |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 665 | runAddDocument(Server, testPath("foo.cpp"), R"cpp( |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 666 | #include "foo.h" |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 667 | )cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 668 | |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 669 | auto File = testPath("bar.cpp"); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 670 | Annotations Test(R"cpp( |
| 671 | namespace ns { |
| 672 | class XXX {}; |
| 673 | /// Doooc |
| 674 | void fooooo() {} |
| 675 | } |
| 676 | void f() { ns::^ } |
| 677 | )cpp"); |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 678 | runAddDocument(Server, File, Test.code()); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 679 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 680 | auto Results = cantFail(runCodeComplete(Server, File, Test.point(), {})); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 681 | // "XYZ" and "foo" are not included in the file being completed but are still |
| 682 | // visible through the index. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 683 | EXPECT_THAT(Results.Completions, Has("XYZ", CompletionItemKind::Class)); |
| 684 | EXPECT_THAT(Results.Completions, Has("foo", CompletionItemKind::Function)); |
| 685 | EXPECT_THAT(Results.Completions, Has("XXX", CompletionItemKind::Class)); |
| 686 | EXPECT_THAT(Results.Completions, |
| 687 | Contains((Named("fooooo"), Kind(CompletionItemKind::Function), |
| 688 | Doc("Doooc"), ReturnType("void")))); |
Sam McCall | a15c2d6 | 2018-01-18 09:27:56 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 691 | TEST(CompletionTest, Documentation) { |
| 692 | auto Results = completions( |
| 693 | R"cpp( |
| 694 | // Non-doxygen comment. |
| 695 | int foo(); |
| 696 | /// Doxygen comment. |
| 697 | /// \param int a |
| 698 | int bar(int a); |
| 699 | /* Multi-line |
| 700 | block comment |
| 701 | */ |
| 702 | int baz(); |
| 703 | |
| 704 | int x = ^ |
| 705 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 706 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 707 | Contains(AllOf(Named("foo"), Doc("Non-doxygen comment.")))); |
| 708 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 709 | Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 710 | Contains(AllOf(Named("bar"), Doc("Doxygen comment.\n\\param int a")))); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 711 | EXPECT_THAT(Results.Completions, |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 712 | Contains(AllOf(Named("baz"), Doc("Multi-line\nblock comment")))); |
| 713 | } |
| 714 | |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 715 | TEST(CompletionTest, GlobalCompletionFiltering) { |
| 716 | |
| 717 | Symbol Class = cls("XYZ"); |
| 718 | Class.IsIndexedForCodeCompletion = false; |
| 719 | Symbol Func = func("XYZ::foooo"); |
| 720 | Func.IsIndexedForCodeCompletion = false; |
| 721 | |
| 722 | auto Results = completions(R"(// void f() { |
| 723 | XYZ::foooo^ |
| 724 | })", |
| 725 | {Class, Func}); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 726 | EXPECT_THAT(Results.Completions, IsEmpty()); |
Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Haojian Wu | 58d208d | 2018-01-25 09:44:06 +0000 | [diff] [blame] | 729 | TEST(CodeCompleteTest, DisableTypoCorrection) { |
| 730 | auto Results = completions(R"cpp( |
| 731 | namespace clang { int v; } |
| 732 | void f() { clangd::^ |
| 733 | )cpp"); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 734 | EXPECT_TRUE(Results.Completions.empty()); |
Haojian Wu | 58d208d | 2018-01-25 09:44:06 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 737 | TEST(CodeCompleteTest, NoColonColonAtTheEnd) { |
| 738 | auto Results = completions(R"cpp( |
| 739 | namespace clang { } |
| 740 | void f() { |
| 741 | clan^ |
| 742 | } |
| 743 | )cpp"); |
| 744 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 745 | EXPECT_THAT(Results.Completions, Contains(Labeled("clang"))); |
| 746 | EXPECT_THAT(Results.Completions, Not(Contains(Labeled("clang::")))); |
Ilya Biryukov | 53d6d93 | 2018-03-06 16:45:21 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 749 | TEST(CompletionTest, BacktrackCrashes) { |
| 750 | // Sema calls code completion callbacks twice in these cases. |
| 751 | auto Results = completions(R"cpp( |
| 752 | namespace ns { |
| 753 | struct FooBarBaz {}; |
| 754 | } // namespace ns |
| 755 | |
| 756 | int foo(ns::FooBar^ |
| 757 | )cpp"); |
| 758 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 759 | EXPECT_THAT(Results.Completions, ElementsAre(Labeled("FooBarBaz"))); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 760 | |
| 761 | // Check we don't crash in that case too. |
| 762 | completions(R"cpp( |
| 763 | struct FooBarBaz {}; |
| 764 | void test() { |
| 765 | if (FooBarBaz * x^) {} |
| 766 | } |
| 767 | )cpp"); |
| 768 | } |
| 769 | |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 770 | TEST(CompletionTest, CompleteInMacroWithStringification) { |
| 771 | auto Results = completions(R"cpp( |
| 772 | void f(const char *, int x); |
| 773 | #define F(x) f(#x, x) |
| 774 | |
| 775 | namespace ns { |
| 776 | int X; |
| 777 | int Y; |
| 778 | } // namespace ns |
| 779 | |
| 780 | int f(int input_num) { |
| 781 | F(ns::^) |
| 782 | } |
| 783 | )cpp"); |
| 784 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 785 | EXPECT_THAT(Results.Completions, |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 786 | UnorderedElementsAre(Named("X"), Named("Y"))); |
| 787 | } |
| 788 | |
| 789 | TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) { |
| 790 | auto Results = completions(R"cpp( |
| 791 | void f(const char *, int x); |
| 792 | #define F(x) f(#x, x) |
| 793 | |
| 794 | namespace ns { |
| 795 | int X; |
| 796 | |
| 797 | int f(int input_num) { |
| 798 | F(^) |
| 799 | } |
| 800 | } // namespace ns |
| 801 | )cpp"); |
| 802 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 803 | EXPECT_THAT(Results.Completions, Contains(Named("X"))); |
Eric Liu | 42abe41 | 2018-05-24 11:20:19 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 806 | TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) { |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 807 | auto Results = completions(R"cpp( |
| 808 | int bar(int param_in_bar) { |
| 809 | } |
| 810 | |
| 811 | int foo(int param_in_foo) { |
| 812 | #if 0 |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 813 | // In recorvery mode, "param_in_foo" will also be suggested among many other |
| 814 | // unrelated symbols; however, this is really a special case where this works. |
| 815 | // If the #if block is outside of the function, "param_in_foo" is still |
| 816 | // suggested, but "bar" and "foo" are missing. So the recovery mode doesn't |
| 817 | // really provide useful results in excluded branches. |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 818 | par^ |
| 819 | #endif |
| 820 | } |
| 821 | )cpp"); |
| 822 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 823 | EXPECT_TRUE(Results.Completions.empty()); |
Ilya Biryukov | 94da7bd | 2018-03-16 15:23:44 +0000 | [diff] [blame] | 824 | } |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 825 | SignatureHelp signatures(StringRef Text, Position Point, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 826 | std::vector<Symbol> IndexSymbols = {}) { |
| 827 | std::unique_ptr<SymbolIndex> Index; |
| 828 | if (!IndexSymbols.empty()) |
| 829 | Index = memIndex(IndexSymbols); |
| 830 | |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 831 | MockFSProvider FS; |
| 832 | MockCompilationDatabase CDB; |
| 833 | IgnoreDiagnostics DiagConsumer; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 834 | ClangdServer::Options Opts = ClangdServer::optsForTest(); |
| 835 | Opts.StaticIndex = Index.get(); |
| 836 | |
| 837 | ClangdServer Server(CDB, FS, DiagConsumer, Opts); |
Sam McCall | c156806 | 2018-02-16 09:41:43 +0000 | [diff] [blame] | 838 | auto File = testPath("foo.cpp"); |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 839 | runAddDocument(Server, File, Text); |
| 840 | return cantFail(runSignatureHelp(Server, File, Point)); |
| 841 | } |
| 842 | |
| 843 | SignatureHelp signatures(StringRef Text, |
| 844 | std::vector<Symbol> IndexSymbols = {}) { |
Sam McCall | 328cbdb | 2017-12-20 16:06:05 +0000 | [diff] [blame] | 845 | Annotations Test(Text); |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 846 | return signatures(Test.code(), Test.point(), std::move(IndexSymbols)); |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | MATCHER_P(ParamsAre, P, "") { |
| 850 | if (P.size() != arg.parameters.size()) |
| 851 | return false; |
| 852 | for (unsigned I = 0; I < P.size(); ++I) |
| 853 | if (P[I] != arg.parameters[I].label) |
| 854 | return false; |
| 855 | return true; |
| 856 | } |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 857 | MATCHER_P(SigDoc, Doc, "") { return arg.documentation == Doc; } |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 858 | |
| 859 | Matcher<SignatureInformation> Sig(std::string Label, |
| 860 | std::vector<std::string> Params) { |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 861 | return AllOf(SigHelpLabeled(Label), ParamsAre(Params)); |
Sam McCall | 800d437 | 2017-12-19 10:29:27 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | TEST(SignatureHelpTest, Overloads) { |
| 865 | auto Results = signatures(R"cpp( |
| 866 | void foo(int x, int y); |
| 867 | void foo(int x, float y); |
| 868 | void foo(float x, int y); |
| 869 | void foo(float x, float y); |
| 870 | void bar(int x, int y = 0); |
| 871 | int main() { foo(^); } |
| 872 | )cpp"); |
| 873 | EXPECT_THAT(Results.signatures, |
| 874 | UnorderedElementsAre( |
| 875 | Sig("foo(float x, float y) -> void", {"float x", "float y"}), |
| 876 | Sig("foo(float x, int y) -> void", {"float x", "int y"}), |
| 877 | Sig("foo(int x, float y) -> void", {"int x", "float y"}), |
| 878 | Sig("foo(int x, int y) -> void", {"int x", "int y"}))); |
| 879 | // We always prefer the first signature. |
| 880 | EXPECT_EQ(0, Results.activeSignature); |
| 881 | EXPECT_EQ(0, Results.activeParameter); |
| 882 | } |
| 883 | |
| 884 | TEST(SignatureHelpTest, DefaultArgs) { |
| 885 | auto Results = signatures(R"cpp( |
| 886 | void bar(int x, int y = 0); |
| 887 | void bar(float x = 0, int y = 42); |
| 888 | int main() { bar(^ |
| 889 | )cpp"); |
| 890 | EXPECT_THAT(Results.signatures, |
| 891 | UnorderedElementsAre( |
| 892 | Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}), |
| 893 | Sig("bar(float x = 0, int y = 42) -> void", |
| 894 | {"float x = 0", "int y = 42"}))); |
| 895 | EXPECT_EQ(0, Results.activeSignature); |
| 896 | EXPECT_EQ(0, Results.activeParameter); |
| 897 | } |
| 898 | |
| 899 | TEST(SignatureHelpTest, ActiveArg) { |
| 900 | auto Results = signatures(R"cpp( |
| 901 | int baz(int a, int b, int c); |
| 902 | int main() { baz(baz(1,2,3), ^); } |
| 903 | )cpp"); |
| 904 | EXPECT_THAT(Results.signatures, |
| 905 | ElementsAre(Sig("baz(int a, int b, int c) -> int", |
| 906 | {"int a", "int b", "int c"}))); |
| 907 | EXPECT_EQ(0, Results.activeSignature); |
| 908 | EXPECT_EQ(1, Results.activeParameter); |
| 909 | } |
| 910 | |
Ilya Biryukov | 43c292c | 2018-08-30 13:14:31 +0000 | [diff] [blame] | 911 | TEST(SignatureHelpTest, OpeningParen) { |
| 912 | llvm::StringLiteral Tests[] = {// Recursive function call. |
| 913 | R"cpp( |
| 914 | int foo(int a, int b, int c); |
| 915 | int main() { |
| 916 | foo(foo $p^( foo(10, 10, 10), ^ ))); |
| 917 | })cpp", |
| 918 | // Functional type cast. |
| 919 | R"cpp( |
| 920 | struct Foo { |
| 921 | Foo(int a, int b, int c); |
| 922 | }; |
| 923 | int main() { |
| 924 | Foo $p^( 10, ^ ); |
| 925 | })cpp", |
| 926 | // New expression. |
| 927 | R"cpp( |
| 928 | struct Foo { |
| 929 | Foo(int a, int b, int c); |
| 930 | }; |
| 931 | int main() { |
| 932 | new Foo $p^( 10, ^ ); |
| 933 | })cpp", |
| 934 | // Macro expansion. |
| 935 | R"cpp( |
| 936 | int foo(int a, int b, int c); |
| 937 | #define FOO foo( |
| 938 | |
| 939 | int main() { |
| 940 | // Macro expansions. |
| 941 | $p^FOO 10, ^ ); |
| 942 | })cpp", |
| 943 | // Macro arguments. |
| 944 | R"cpp( |
| 945 | int foo(int a, int b, int c); |
| 946 | int main() { |
| 947 | #define ID(X) X |
| 948 | ID(foo $p^( foo(10), ^ )) |
| 949 | })cpp"}; |
| 950 | |
| 951 | for (auto Test : Tests) { |
| 952 | Annotations Code(Test); |
| 953 | EXPECT_EQ(signatures(Code.code(), Code.point()).argListStart, |
| 954 | Code.point("p")) |
| 955 | << "Test source:" << Test; |
| 956 | } |
| 957 | } |
| 958 | |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 959 | class IndexRequestCollector : public SymbolIndex { |
| 960 | public: |
| 961 | bool |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 962 | fuzzyFind(const FuzzyFindRequest &Req, |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 963 | llvm::function_ref<void(const Symbol &)> Callback) const override { |
| 964 | Requests.push_back(Req); |
Sam McCall | ab8e393 | 2018-02-19 13:04:41 +0000 | [diff] [blame] | 965 | return true; |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Eric Liu | 9ec459f | 2018-03-14 09:48:05 +0000 | [diff] [blame] | 968 | void lookup(const LookupRequest &, |
| 969 | llvm::function_ref<void(const Symbol &)>) const override {} |
| 970 | |
Haojian Wu | 65ac321 | 2018-08-06 13:14:32 +0000 | [diff] [blame] | 971 | void findOccurrences(const OccurrencesRequest &Req, |
| 972 | llvm::function_ref<void(const SymbolOccurrence &)> |
| 973 | Callback) const override {} |
| 974 | |
Kirill Bobyrev | fc89001 | 2018-08-24 09:12:54 +0000 | [diff] [blame] | 975 | // This is incorrect, but IndexRequestCollector is not an actual index and it |
| 976 | // isn't used in production code. |
| 977 | size_t estimateMemoryUsage() const override { return 0; } |
| 978 | |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 979 | const std::vector<FuzzyFindRequest> consumeRequests() const { |
| 980 | auto Reqs = std::move(Requests); |
| 981 | Requests = {}; |
| 982 | return Reqs; |
| 983 | } |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 984 | |
| 985 | private: |
| 986 | mutable std::vector<FuzzyFindRequest> Requests; |
| 987 | }; |
| 988 | |
| 989 | std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code) { |
| 990 | clangd::CodeCompleteOptions Opts; |
| 991 | IndexRequestCollector Requests; |
| 992 | Opts.Index = &Requests; |
| 993 | completions(Code, {}, Opts); |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 994 | return Requests.consumeRequests(); |
Haojian Wu | 061c73e | 2018-01-23 11:37:26 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | TEST(CompletionTest, UnqualifiedIdQuery) { |
| 998 | auto Requests = captureIndexRequests(R"cpp( |
| 999 | namespace std {} |
| 1000 | using namespace std; |
| 1001 | namespace ns { |
| 1002 | void f() { |
| 1003 | vec^ |
| 1004 | } |
| 1005 | } |
| 1006 | )cpp"); |
| 1007 | |
| 1008 | EXPECT_THAT(Requests, |
| 1009 | ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1010 | UnorderedElementsAre("", "ns::", "std::")))); |
| 1011 | } |
| 1012 | |
| 1013 | TEST(CompletionTest, ResolvedQualifiedIdQuery) { |
| 1014 | auto Requests = captureIndexRequests(R"cpp( |
| 1015 | namespace ns1 {} |
| 1016 | namespace ns2 {} // ignore |
| 1017 | namespace ns3 { namespace nns3 {} } |
| 1018 | namespace foo { |
| 1019 | using namespace ns1; |
| 1020 | using namespace ns3::nns3; |
| 1021 | } |
| 1022 | namespace ns { |
| 1023 | void f() { |
| 1024 | foo::^ |
| 1025 | } |
| 1026 | } |
| 1027 | )cpp"); |
| 1028 | |
| 1029 | EXPECT_THAT(Requests, |
| 1030 | ElementsAre(Field( |
| 1031 | &FuzzyFindRequest::Scopes, |
| 1032 | UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::")))); |
| 1033 | } |
| 1034 | |
| 1035 | TEST(CompletionTest, UnresolvedQualifierIdQuery) { |
| 1036 | auto Requests = captureIndexRequests(R"cpp( |
| 1037 | namespace a {} |
| 1038 | using namespace a; |
| 1039 | namespace ns { |
| 1040 | void f() { |
| 1041 | bar::^ |
| 1042 | } |
| 1043 | } // namespace ns |
| 1044 | )cpp"); |
| 1045 | |
| 1046 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1047 | UnorderedElementsAre("bar::")))); |
| 1048 | } |
| 1049 | |
| 1050 | TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) { |
| 1051 | auto Requests = captureIndexRequests(R"cpp( |
| 1052 | namespace a {} |
| 1053 | using namespace a; |
| 1054 | namespace ns { |
| 1055 | void f() { |
| 1056 | ::a::bar::^ |
| 1057 | } |
| 1058 | } // namespace ns |
| 1059 | )cpp"); |
| 1060 | |
| 1061 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1062 | UnorderedElementsAre("a::bar::")))); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(CompletionTest, EmptyQualifiedQuery) { |
| 1066 | auto Requests = captureIndexRequests(R"cpp( |
| 1067 | namespace ns { |
| 1068 | void f() { |
| 1069 | ^ |
| 1070 | } |
| 1071 | } // namespace ns |
| 1072 | )cpp"); |
| 1073 | |
| 1074 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1075 | UnorderedElementsAre("", "ns::")))); |
| 1076 | } |
| 1077 | |
| 1078 | TEST(CompletionTest, GlobalQualifiedQuery) { |
| 1079 | auto Requests = captureIndexRequests(R"cpp( |
| 1080 | namespace ns { |
| 1081 | void f() { |
| 1082 | ::^ |
| 1083 | } |
| 1084 | } // namespace ns |
| 1085 | )cpp"); |
| 1086 | |
| 1087 | EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes, |
| 1088 | UnorderedElementsAre("")))); |
| 1089 | } |
| 1090 | |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1091 | TEST(CompletionTest, NoIndexCompletionsInsideClasses) { |
| 1092 | auto Completions = completions( |
| 1093 | R"cpp( |
| 1094 | struct Foo { |
| 1095 | int SomeNameOfField; |
| 1096 | typedef int SomeNameOfTypedefField; |
| 1097 | }; |
| 1098 | |
| 1099 | Foo::^)cpp", |
| 1100 | {func("::SomeNameInTheIndex"), func("::Foo::SomeNameInTheIndex")}); |
| 1101 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1102 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1103 | AllOf(Contains(Labeled("SomeNameOfField")), |
| 1104 | Contains(Labeled("SomeNameOfTypedefField")), |
| 1105 | Not(Contains(Labeled("SomeNameInTheIndex"))))); |
| 1106 | } |
| 1107 | |
| 1108 | TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) { |
| 1109 | { |
| 1110 | auto Completions = completions( |
| 1111 | R"cpp( |
| 1112 | template <class T> |
| 1113 | void foo() { |
| 1114 | T::^ |
| 1115 | } |
| 1116 | )cpp", |
| 1117 | {func("::SomeNameInTheIndex")}); |
| 1118 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1119 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1120 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1121 | } |
| 1122 | |
| 1123 | { |
| 1124 | auto Completions = completions( |
| 1125 | R"cpp( |
| 1126 | template <class T> |
| 1127 | void foo() { |
| 1128 | T::template Y<int>::^ |
| 1129 | } |
| 1130 | )cpp", |
| 1131 | {func("::SomeNameInTheIndex")}); |
| 1132 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1133 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1134 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1135 | } |
| 1136 | |
| 1137 | { |
| 1138 | auto Completions = completions( |
| 1139 | R"cpp( |
| 1140 | template <class T> |
| 1141 | void foo() { |
| 1142 | T::foo::^ |
| 1143 | } |
| 1144 | )cpp", |
| 1145 | {func("::SomeNameInTheIndex")}); |
| 1146 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1147 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | a907ba4 | 2018-05-14 10:50:04 +0000 | [diff] [blame] | 1148 | Not(Contains(Labeled("SomeNameInTheIndex")))); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1152 | TEST(CompletionTest, OverloadBundling) { |
| 1153 | clangd::CodeCompleteOptions Opts; |
| 1154 | Opts.BundleOverloads = true; |
| 1155 | |
| 1156 | std::string Context = R"cpp( |
| 1157 | struct X { |
| 1158 | // Overload with int |
| 1159 | int a(int); |
| 1160 | // Overload with bool |
| 1161 | int a(bool); |
| 1162 | int b(float); |
| 1163 | }; |
| 1164 | int GFuncC(int); |
| 1165 | int GFuncD(int); |
| 1166 | )cpp"; |
| 1167 | |
| 1168 | // Member completions are bundled. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1169 | EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions, |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1170 | UnorderedElementsAre(Labeled("a(…)"), Labeled("b(float)"))); |
| 1171 | |
| 1172 | // Non-member completions are bundled, including index+sema. |
| 1173 | Symbol NoArgsGFunc = func("GFuncC"); |
| 1174 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1175 | completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1176 | UnorderedElementsAre(Labeled("GFuncC(…)"), Labeled("GFuncD(int)"))); |
| 1177 | |
| 1178 | // Differences in header-to-insert suppress bundling. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1179 | std::string DeclFile = URI::createFile(testPath("foo")).toString(); |
| 1180 | NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile; |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 1181 | NoArgsGFunc.IncludeHeader = "<foo>"; |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1182 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1183 | completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions, |
| 1184 | UnorderedElementsAre(AllOf(Named("GFuncC"), InsertInclude("<foo>")), |
| 1185 | Labeled("GFuncC(int)"), Labeled("GFuncD(int)"))); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1186 | |
| 1187 | // Examine a bundled completion in detail. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1188 | auto A = |
| 1189 | completions(Context + "int y = X().a^", {}, Opts).Completions.front(); |
| 1190 | EXPECT_EQ(A.Name, "a"); |
| 1191 | EXPECT_EQ(A.Signature, "(…)"); |
| 1192 | EXPECT_EQ(A.BundleSize, 2u); |
| 1193 | EXPECT_EQ(A.Kind, CompletionItemKind::Method); |
| 1194 | EXPECT_EQ(A.ReturnType, "int"); // All overloads return int. |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1195 | // For now we just return one of the doc strings arbitrarily. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1196 | EXPECT_THAT(A.Documentation, AnyOf(HasSubstr("Overload with int"), |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1197 | HasSubstr("Overload with bool"))); |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1198 | EXPECT_EQ(A.SnippetSuffix, "($0)"); |
Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Ilya Biryukov | 30b04b1 | 2018-05-28 09:54:51 +0000 | [diff] [blame] | 1201 | TEST(CompletionTest, DocumentationFromChangedFileCrash) { |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1202 | MockFSProvider FS; |
| 1203 | auto FooH = testPath("foo.h"); |
| 1204 | auto FooCpp = testPath("foo.cpp"); |
| 1205 | FS.Files[FooH] = R"cpp( |
| 1206 | // this is my documentation comment. |
| 1207 | int func(); |
| 1208 | )cpp"; |
| 1209 | FS.Files[FooCpp] = ""; |
| 1210 | |
| 1211 | MockCompilationDatabase CDB; |
| 1212 | IgnoreDiagnostics DiagConsumer; |
| 1213 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1214 | |
| 1215 | Annotations Source(R"cpp( |
| 1216 | #include "foo.h" |
| 1217 | int func() { |
| 1218 | // This makes sure we have func from header in the AST. |
| 1219 | } |
| 1220 | int a = fun^ |
| 1221 | )cpp"); |
| 1222 | Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes); |
| 1223 | // We need to wait for preamble to build. |
| 1224 | ASSERT_TRUE(Server.blockUntilIdleForTest()); |
| 1225 | |
| 1226 | // Change the header file. Completion will reuse the old preamble! |
| 1227 | FS.Files[FooH] = R"cpp( |
| 1228 | int func(); |
| 1229 | )cpp"; |
| 1230 | |
| 1231 | clangd::CodeCompleteOptions Opts; |
| 1232 | Opts.IncludeComments = true; |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1233 | CodeCompleteResult Completions = |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1234 | cantFail(runCodeComplete(Server, FooCpp, Source.point(), Opts)); |
| 1235 | // We shouldn't crash. Unfortunately, current workaround is to not produce |
| 1236 | // comments for symbols from headers. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1237 | EXPECT_THAT(Completions.Completions, |
Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 1238 | Contains(AllOf(Not(IsDocumented()), Named("func")))); |
| 1239 | } |
| 1240 | |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1241 | TEST(CompletionTest, NonDocComments) { |
| 1242 | MockFSProvider FS; |
| 1243 | auto FooCpp = testPath("foo.cpp"); |
| 1244 | FS.Files[FooCpp] = ""; |
| 1245 | |
| 1246 | MockCompilationDatabase CDB; |
| 1247 | IgnoreDiagnostics DiagConsumer; |
| 1248 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1249 | |
| 1250 | Annotations Source(R"cpp( |
Ilya Biryukov | da8dd8b | 2018-06-27 09:47:20 +0000 | [diff] [blame] | 1251 | // We ignore namespace comments, for rationale see CodeCompletionStrings.h. |
| 1252 | namespace comments_ns { |
| 1253 | } |
| 1254 | |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1255 | // ------------------ |
| 1256 | int comments_foo(); |
| 1257 | |
| 1258 | // A comment and a decl are separated by newlines. |
| 1259 | // Therefore, the comment shouldn't show up as doc comment. |
| 1260 | |
| 1261 | int comments_bar(); |
| 1262 | |
| 1263 | // this comment should be in the results. |
| 1264 | int comments_baz(); |
| 1265 | |
| 1266 | |
| 1267 | template <class T> |
| 1268 | struct Struct { |
| 1269 | int comments_qux(); |
| 1270 | int comments_quux(); |
| 1271 | }; |
| 1272 | |
| 1273 | |
| 1274 | // This comment should not be there. |
| 1275 | |
| 1276 | template <class T> |
| 1277 | int Struct<T>::comments_qux() { |
| 1278 | } |
| 1279 | |
| 1280 | // This comment **should** be in results. |
| 1281 | template <class T> |
| 1282 | int Struct<T>::comments_quux() { |
| 1283 | int a = comments^; |
| 1284 | } |
| 1285 | )cpp"); |
Reid Kleckner | 80274b1 | 2018-06-18 18:55:10 +0000 | [diff] [blame] | 1286 | // FIXME: Auto-completion in a template requires disabling delayed template |
| 1287 | // parsing. |
| 1288 | CDB.ExtraClangFlags.push_back("-fno-delayed-template-parsing"); |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1289 | Server.addDocument(FooCpp, Source.code(), WantDiagnostics::Yes); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1290 | CodeCompleteResult Completions = cantFail(runCodeComplete( |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1291 | Server, FooCpp, Source.point(), clangd::CodeCompleteOptions())); |
| 1292 | |
| 1293 | // We should not get any of those comments in completion. |
| 1294 | EXPECT_THAT( |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1295 | Completions.Completions, |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1296 | UnorderedElementsAre(AllOf(Not(IsDocumented()), Named("comments_foo")), |
| 1297 | AllOf(IsDocumented(), Named("comments_baz")), |
| 1298 | AllOf(IsDocumented(), Named("comments_quux")), |
Ilya Biryukov | da8dd8b | 2018-06-27 09:47:20 +0000 | [diff] [blame] | 1299 | AllOf(Not(IsDocumented()), Named("comments_ns")), |
Ilya Biryukov | 89fcf6b | 2018-06-15 08:31:17 +0000 | [diff] [blame] | 1300 | // FIXME(ibiryukov): the following items should have |
| 1301 | // empty documentation, since they are separated from |
| 1302 | // a comment with an empty line. Unfortunately, I |
| 1303 | // couldn't make Sema tests pass if we ignore those. |
| 1304 | AllOf(IsDocumented(), Named("comments_bar")), |
| 1305 | AllOf(IsDocumented(), Named("comments_qux")))); |
| 1306 | } |
| 1307 | |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1308 | TEST(CompletionTest, CompleteOnInvalidLine) { |
| 1309 | auto FooCpp = testPath("foo.cpp"); |
| 1310 | |
| 1311 | MockCompilationDatabase CDB; |
| 1312 | IgnoreDiagnostics DiagConsumer; |
| 1313 | MockFSProvider FS; |
| 1314 | FS.Files[FooCpp] = "// empty file"; |
| 1315 | |
| 1316 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1317 | // Run completion outside the file range. |
| 1318 | Position Pos; |
| 1319 | Pos.line = 100; |
| 1320 | Pos.character = 0; |
| 1321 | EXPECT_THAT_EXPECTED( |
| 1322 | runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()), |
| 1323 | Failed()); |
| 1324 | } |
| 1325 | |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 1326 | TEST(CompletionTest, QualifiedNames) { |
| 1327 | auto Results = completions( |
| 1328 | R"cpp( |
| 1329 | namespace ns { int local; void both(); } |
| 1330 | void f() { ::ns::^ } |
| 1331 | )cpp", |
| 1332 | {func("ns::both"), cls("ns::Index")}); |
| 1333 | // We get results from both index and sema, with no duplicates. |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1334 | EXPECT_THAT( |
| 1335 | Results.Completions, |
| 1336 | UnorderedElementsAre(Scope("ns::"), Scope("ns::"), Scope("ns::"))); |
| 1337 | } |
| 1338 | |
| 1339 | TEST(CompletionTest, Render) { |
| 1340 | CodeCompletion C; |
| 1341 | C.Name = "x"; |
| 1342 | C.Signature = "(bool) const"; |
| 1343 | C.SnippetSuffix = "(${0:bool})"; |
| 1344 | C.ReturnType = "int"; |
| 1345 | C.RequiredQualifier = "Foo::"; |
| 1346 | C.Scope = "ns::Foo::"; |
| 1347 | C.Documentation = "This is x()."; |
| 1348 | C.Header = "\"foo.h\""; |
| 1349 | C.Kind = CompletionItemKind::Method; |
| 1350 | C.Score.Total = 1.0; |
Sam McCall | 4e5742a | 2018-07-06 11:50:49 +0000 | [diff] [blame] | 1351 | C.Origin = SymbolOrigin::AST | SymbolOrigin::Static; |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1352 | |
| 1353 | CodeCompleteOptions Opts; |
| 1354 | Opts.IncludeIndicator.Insert = "^"; |
| 1355 | Opts.IncludeIndicator.NoInsert = ""; |
| 1356 | Opts.EnableSnippets = false; |
| 1357 | |
| 1358 | auto R = C.render(Opts); |
| 1359 | EXPECT_EQ(R.label, "Foo::x(bool) const"); |
| 1360 | EXPECT_EQ(R.insertText, "Foo::x"); |
| 1361 | EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText); |
| 1362 | EXPECT_EQ(R.filterText, "x"); |
| 1363 | EXPECT_EQ(R.detail, "int\n\"foo.h\""); |
| 1364 | EXPECT_EQ(R.documentation, "This is x()."); |
| 1365 | EXPECT_THAT(R.additionalTextEdits, IsEmpty()); |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1366 | EXPECT_EQ(R.sortText, sortText(1.0, "x")); |
| 1367 | |
| 1368 | Opts.EnableSnippets = true; |
| 1369 | R = C.render(Opts); |
| 1370 | EXPECT_EQ(R.insertText, "Foo::x(${0:bool})"); |
| 1371 | EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet); |
| 1372 | |
| 1373 | C.HeaderInsertion.emplace(); |
| 1374 | R = C.render(Opts); |
| 1375 | EXPECT_EQ(R.label, "^Foo::x(bool) const"); |
| 1376 | EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty())); |
| 1377 | |
Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 1378 | Opts.ShowOrigins = true; |
| 1379 | R = C.render(Opts); |
| 1380 | EXPECT_EQ(R.label, "^[AS]Foo::x(bool) const"); |
| 1381 | |
Sam McCall | e746a2b | 2018-07-02 11:13:16 +0000 | [diff] [blame] | 1382 | C.BundleSize = 2; |
| 1383 | R = C.render(Opts); |
| 1384 | EXPECT_EQ(R.detail, "[2 overloads]\n\"foo.h\""); |
Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 1385 | } |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 1386 | |
Eric Liu | 485074f | 2018-07-11 13:15:31 +0000 | [diff] [blame] | 1387 | TEST(CompletionTest, IgnoreRecoveryResults) { |
| 1388 | auto Results = completions( |
| 1389 | R"cpp( |
| 1390 | namespace ns { int NotRecovered() { return 0; } } |
| 1391 | void f() { |
| 1392 | // Sema enters recovery mode first and then normal mode. |
| 1393 | if (auto x = ns::NotRecover^) |
| 1394 | } |
| 1395 | )cpp"); |
| 1396 | EXPECT_THAT(Results.Completions, UnorderedElementsAre(Named("NotRecovered"))); |
| 1397 | } |
| 1398 | |
Eric Liu | f433c2d | 2018-07-18 15:31:14 +0000 | [diff] [blame] | 1399 | TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) { |
| 1400 | auto Results = completions( |
| 1401 | R"cpp( |
| 1402 | namespace ns { |
| 1403 | class X { public: X(); int x_; }; |
| 1404 | X::X() : x_^(0) {} |
| 1405 | } |
| 1406 | )cpp"); |
| 1407 | EXPECT_THAT(Results.Completions, |
| 1408 | UnorderedElementsAre(AllOf(Scope("ns::X::"), Named("x_")))); |
| 1409 | } |
| 1410 | |
Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 1411 | TEST(CompletionTest, CodeCompletionContext) { |
| 1412 | auto Results = completions( |
| 1413 | R"cpp( |
| 1414 | namespace ns { |
| 1415 | class X { public: X(); int x_; }; |
| 1416 | void f() { |
| 1417 | X x; |
| 1418 | x.^; |
| 1419 | } |
| 1420 | } |
| 1421 | )cpp"); |
| 1422 | |
| 1423 | EXPECT_THAT(Results.Context, CodeCompletionContext::CCC_DotMemberAccess); |
| 1424 | } |
| 1425 | |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 1426 | TEST(CompletionTest, FixItForArrowToDot) { |
| 1427 | MockFSProvider FS; |
| 1428 | MockCompilationDatabase CDB; |
| 1429 | IgnoreDiagnostics DiagConsumer; |
| 1430 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1431 | |
| 1432 | CodeCompleteOptions Opts; |
| 1433 | Opts.IncludeFixIts = true; |
| 1434 | Annotations TestCode( |
| 1435 | R"cpp( |
| 1436 | class Auxilary { |
| 1437 | public: |
| 1438 | void AuxFunction(); |
| 1439 | }; |
| 1440 | class ClassWithPtr { |
| 1441 | public: |
| 1442 | void MemberFunction(); |
| 1443 | Auxilary* operator->() const; |
| 1444 | Auxilary* Aux; |
| 1445 | }; |
| 1446 | void f() { |
| 1447 | ClassWithPtr x; |
| 1448 | x[[->]]^; |
| 1449 | } |
| 1450 | )cpp"); |
| 1451 | auto Results = |
| 1452 | completions(Server, TestCode.code(), TestCode.point(), {}, Opts); |
| 1453 | EXPECT_EQ(Results.Completions.size(), 3u); |
| 1454 | |
| 1455 | TextEdit ReplacementEdit; |
| 1456 | ReplacementEdit.range = TestCode.range(); |
| 1457 | ReplacementEdit.newText = "."; |
| 1458 | for (const auto &C : Results.Completions) { |
| 1459 | EXPECT_TRUE(C.FixIts.size() == 1u || C.Name == "AuxFunction"); |
Haojian Wu | 1793bc9 | 2018-08-10 08:34:16 +0000 | [diff] [blame] | 1460 | if (!C.FixIts.empty()) { |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 1461 | EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit)); |
Haojian Wu | 1793bc9 | 2018-08-10 08:34:16 +0000 | [diff] [blame] | 1462 | } |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | TEST(CompletionTest, FixItForDotToArrow) { |
| 1467 | MockFSProvider FS; |
| 1468 | MockCompilationDatabase CDB; |
| 1469 | IgnoreDiagnostics DiagConsumer; |
| 1470 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1471 | |
| 1472 | CodeCompleteOptions Opts; |
| 1473 | Opts.IncludeFixIts = true; |
| 1474 | Annotations TestCode( |
| 1475 | R"cpp( |
| 1476 | class Auxilary { |
| 1477 | public: |
| 1478 | void AuxFunction(); |
| 1479 | }; |
| 1480 | class ClassWithPtr { |
| 1481 | public: |
| 1482 | void MemberFunction(); |
| 1483 | Auxilary* operator->() const; |
| 1484 | Auxilary* Aux; |
| 1485 | }; |
| 1486 | void f() { |
| 1487 | ClassWithPtr x; |
| 1488 | x[[.]]^; |
| 1489 | } |
| 1490 | )cpp"); |
| 1491 | auto Results = |
| 1492 | completions(Server, TestCode.code(), TestCode.point(), {}, Opts); |
| 1493 | EXPECT_EQ(Results.Completions.size(), 3u); |
| 1494 | |
| 1495 | TextEdit ReplacementEdit; |
| 1496 | ReplacementEdit.range = TestCode.range(); |
| 1497 | ReplacementEdit.newText = "->"; |
| 1498 | for (const auto &C : Results.Completions) { |
| 1499 | EXPECT_TRUE(C.FixIts.empty() || C.Name == "AuxFunction"); |
| 1500 | if (!C.FixIts.empty()) { |
| 1501 | EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit)); |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 1506 | TEST(CompletionTest, RenderWithFixItMerged) { |
| 1507 | TextEdit FixIt; |
| 1508 | FixIt.range.end.character = 5; |
| 1509 | FixIt.newText = "->"; |
| 1510 | |
| 1511 | CodeCompletion C; |
| 1512 | C.Name = "x"; |
| 1513 | C.RequiredQualifier = "Foo::"; |
| 1514 | C.FixIts = {FixIt}; |
| 1515 | C.CompletionTokenRange.start.character = 5; |
| 1516 | |
| 1517 | CodeCompleteOptions Opts; |
| 1518 | Opts.IncludeFixIts = true; |
| 1519 | |
| 1520 | auto R = C.render(Opts); |
| 1521 | EXPECT_TRUE(R.textEdit); |
| 1522 | EXPECT_EQ(R.textEdit->newText, "->Foo::x"); |
| 1523 | EXPECT_TRUE(R.additionalTextEdits.empty()); |
| 1524 | } |
| 1525 | |
| 1526 | TEST(CompletionTest, RenderWithFixItNonMerged) { |
| 1527 | TextEdit FixIt; |
| 1528 | FixIt.range.end.character = 4; |
| 1529 | FixIt.newText = "->"; |
| 1530 | |
| 1531 | CodeCompletion C; |
| 1532 | C.Name = "x"; |
| 1533 | C.RequiredQualifier = "Foo::"; |
| 1534 | C.FixIts = {FixIt}; |
| 1535 | C.CompletionTokenRange.start.character = 5; |
| 1536 | |
| 1537 | CodeCompleteOptions Opts; |
| 1538 | Opts.IncludeFixIts = true; |
| 1539 | |
| 1540 | auto R = C.render(Opts); |
| 1541 | EXPECT_TRUE(R.textEdit); |
| 1542 | EXPECT_EQ(R.textEdit->newText, "Foo::x"); |
| 1543 | EXPECT_THAT(R.additionalTextEdits, UnorderedElementsAre(FixIt)); |
| 1544 | } |
| 1545 | |
| 1546 | TEST(CompletionTest, CompletionTokenRange) { |
| 1547 | MockFSProvider FS; |
| 1548 | MockCompilationDatabase CDB; |
| 1549 | IgnoreDiagnostics DiagConsumer; |
| 1550 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1551 | |
| 1552 | constexpr const char *TestCodes[] = { |
| 1553 | R"cpp( |
| 1554 | class Auxilary { |
| 1555 | public: |
| 1556 | void AuxFunction(); |
| 1557 | }; |
| 1558 | void f() { |
| 1559 | Auxilary x; |
| 1560 | x.[[Aux]]^; |
| 1561 | } |
| 1562 | )cpp", |
| 1563 | R"cpp( |
| 1564 | class Auxilary { |
| 1565 | public: |
| 1566 | void AuxFunction(); |
| 1567 | }; |
| 1568 | void f() { |
| 1569 | Auxilary x; |
| 1570 | x.[[]]^; |
| 1571 | } |
| 1572 | )cpp"}; |
| 1573 | for (const auto &Text : TestCodes) { |
| 1574 | Annotations TestCode(Text); |
| 1575 | auto Results = completions(Server, TestCode.code(), TestCode.point()); |
| 1576 | |
| 1577 | EXPECT_EQ(Results.Completions.size(), 1u); |
| 1578 | EXPECT_THAT(Results.Completions.front().CompletionTokenRange, TestCode.range()); |
| 1579 | } |
| 1580 | } |
| 1581 | |
Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 1582 | TEST(SignatureHelpTest, OverloadsOrdering) { |
| 1583 | const auto Results = signatures(R"cpp( |
| 1584 | void foo(int x); |
| 1585 | void foo(int x, float y); |
| 1586 | void foo(float x, int y); |
| 1587 | void foo(float x, float y); |
| 1588 | void foo(int x, int y = 0); |
| 1589 | int main() { foo(^); } |
| 1590 | )cpp"); |
| 1591 | EXPECT_THAT( |
| 1592 | Results.signatures, |
| 1593 | ElementsAre( |
| 1594 | Sig("foo(int x) -> void", {"int x"}), |
| 1595 | Sig("foo(int x, int y = 0) -> void", {"int x", "int y = 0"}), |
| 1596 | Sig("foo(float x, int y) -> void", {"float x", "int y"}), |
| 1597 | Sig("foo(int x, float y) -> void", {"int x", "float y"}), |
| 1598 | Sig("foo(float x, float y) -> void", {"float x", "float y"}))); |
| 1599 | // We always prefer the first signature. |
| 1600 | EXPECT_EQ(0, Results.activeSignature); |
| 1601 | EXPECT_EQ(0, Results.activeParameter); |
| 1602 | } |
| 1603 | |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1604 | TEST(SignatureHelpTest, InstantiatedSignatures) { |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1605 | StringRef Sig0 = R"cpp( |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1606 | template <class T> |
| 1607 | void foo(T, T, T); |
| 1608 | |
| 1609 | int main() { |
| 1610 | foo<int>(^); |
| 1611 | } |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1612 | )cpp"; |
| 1613 | |
| 1614 | EXPECT_THAT(signatures(Sig0).signatures, |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1615 | ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"}))); |
| 1616 | |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1617 | StringRef Sig1 = R"cpp( |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1618 | template <class T> |
| 1619 | void foo(T, T, T); |
| 1620 | |
| 1621 | int main() { |
| 1622 | foo(10, ^); |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1623 | })cpp"; |
| 1624 | |
| 1625 | EXPECT_THAT(signatures(Sig1).signatures, |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1626 | ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"}))); |
| 1627 | |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1628 | StringRef Sig2 = R"cpp( |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1629 | template <class ...T> |
| 1630 | void foo(T...); |
| 1631 | |
| 1632 | int main() { |
| 1633 | foo<int>(^); |
| 1634 | } |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1635 | )cpp"; |
| 1636 | |
| 1637 | EXPECT_THAT(signatures(Sig2).signatures, |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1638 | ElementsAre(Sig("foo(T...) -> void", {"T..."}))); |
| 1639 | |
| 1640 | // It is debatable whether we should substitute the outer template parameter |
| 1641 | // ('T') in that case. Currently we don't substitute it in signature help, but |
| 1642 | // do substitute in code complete. |
| 1643 | // FIXME: make code complete and signature help consistent, figure out which |
| 1644 | // way is better. |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1645 | StringRef Sig3 = R"cpp( |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1646 | template <class T> |
| 1647 | struct X { |
| 1648 | template <class U> |
| 1649 | void foo(T, U); |
| 1650 | }; |
| 1651 | |
| 1652 | int main() { |
| 1653 | X<int>().foo<double>(^) |
| 1654 | } |
Simon Pilgrim | 83552ee | 2018-08-16 11:41:19 +0000 | [diff] [blame] | 1655 | )cpp"; |
| 1656 | |
| 1657 | EXPECT_THAT(signatures(Sig3).signatures, |
Ilya Biryukov | 8fd44bb | 2018-08-14 09:36:32 +0000 | [diff] [blame] | 1658 | ElementsAre(Sig("foo(T, U) -> void", {"T", "U"}))); |
| 1659 | } |
| 1660 | |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1661 | TEST(SignatureHelpTest, IndexDocumentation) { |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1662 | Symbol Foo0 = sym("foo", index::SymbolKind::Function, "@F@\\0#"); |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 1663 | Foo0.Documentation = "Doc from the index"; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1664 | Symbol Foo1 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#"); |
Sam McCall | 2e5700f | 2018-08-31 13:55:01 +0000 | [diff] [blame] | 1665 | Foo1.Documentation = "Doc from the index"; |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1666 | Symbol Foo2 = sym("foo", index::SymbolKind::Function, "@F@\\0#I#I#"); |
| 1667 | |
Simon Pilgrim | 24d3492 | 2018-08-17 10:40:05 +0000 | [diff] [blame] | 1668 | StringRef Sig0 = R"cpp( |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1669 | int foo(); |
| 1670 | int foo(double); |
| 1671 | |
| 1672 | void test() { |
| 1673 | foo(^); |
| 1674 | } |
Simon Pilgrim | 24d3492 | 2018-08-17 10:40:05 +0000 | [diff] [blame] | 1675 | )cpp"; |
| 1676 | |
| 1677 | EXPECT_THAT( |
| 1678 | signatures(Sig0, {Foo0}).signatures, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1679 | ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")), |
| 1680 | AllOf(Sig("foo(double) -> int", {"double"}), SigDoc("")))); |
| 1681 | |
Simon Pilgrim | 24d3492 | 2018-08-17 10:40:05 +0000 | [diff] [blame] | 1682 | StringRef Sig1 = R"cpp( |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1683 | int foo(); |
| 1684 | // Overriden doc from sema |
| 1685 | int foo(int); |
| 1686 | // Doc from sema |
| 1687 | int foo(int, int); |
| 1688 | |
| 1689 | void test() { |
| 1690 | foo(^); |
| 1691 | } |
Simon Pilgrim | 24d3492 | 2018-08-17 10:40:05 +0000 | [diff] [blame] | 1692 | )cpp"; |
| 1693 | |
| 1694 | EXPECT_THAT( |
| 1695 | signatures(Sig1, {Foo0, Foo1, Foo2}).signatures, |
Ilya Biryukov | 8a0f76b | 2018-08-17 09:32:30 +0000 | [diff] [blame] | 1696 | ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")), |
| 1697 | AllOf(Sig("foo(int) -> int", {"int"}), |
| 1698 | SigDoc("Overriden doc from sema")), |
| 1699 | AllOf(Sig("foo(int, int) -> int", {"int", "int"}), |
| 1700 | SigDoc("Doc from sema")))); |
| 1701 | } |
| 1702 | |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1703 | TEST(CompletionTest, CompletionFunctionArgsDisabled) { |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1704 | CodeCompleteOptions Opts; |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1705 | Opts.EnableSnippets = true; |
| 1706 | Opts.EnableFunctionArgSnippets = false; |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1707 | const std::string Header = |
| 1708 | R"cpp( |
| 1709 | void xfoo(); |
| 1710 | void xfoo(int x, int y); |
| 1711 | void xbar(); |
| 1712 | void f() { |
| 1713 | )cpp"; |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1714 | { |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1715 | auto Results = completions(Header + "\nxfo^", {}, Opts); |
| 1716 | EXPECT_THAT( |
| 1717 | Results.Completions, |
| 1718 | UnorderedElementsAre(AllOf(Named("xfoo"), SnippetSuffix("()")), |
| 1719 | AllOf(Named("xfoo"), SnippetSuffix("($0)")))); |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1720 | } |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1721 | { |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1722 | auto Results = completions(Header + "\nxba^", {}, Opts); |
| 1723 | EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf( |
| 1724 | Named("xbar"), SnippetSuffix("()")))); |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1725 | } |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1726 | { |
Kadir Cetinkaya | 516fcda | 2018-08-23 12:19:39 +0000 | [diff] [blame] | 1727 | Opts.BundleOverloads = true; |
| 1728 | auto Results = completions(Header + "\nxfo^", {}, Opts); |
| 1729 | EXPECT_THAT( |
| 1730 | Results.Completions, |
| 1731 | UnorderedElementsAre(AllOf(Named("xfoo"), SnippetSuffix("($0)")))); |
Kadir Cetinkaya | 6c9f15c | 2018-08-17 15:42:54 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Kadir Cetinkaya | f8b85a3 | 2018-08-23 13:14:50 +0000 | [diff] [blame] | 1735 | TEST(CompletionTest, SuggestOverrides) { |
| 1736 | constexpr const char *const Text(R"cpp( |
| 1737 | class A { |
| 1738 | public: |
| 1739 | virtual void vfunc(bool param); |
| 1740 | virtual void vfunc(bool param, int p); |
| 1741 | void func(bool param); |
| 1742 | }; |
| 1743 | class B : public A { |
| 1744 | virtual void ttt(bool param) const; |
| 1745 | void vfunc(bool param, int p) override; |
| 1746 | }; |
| 1747 | class C : public B { |
| 1748 | public: |
| 1749 | void vfunc(bool param) override; |
| 1750 | ^ |
| 1751 | }; |
| 1752 | )cpp"); |
| 1753 | const auto Results = completions(Text); |
| 1754 | EXPECT_THAT(Results.Completions, |
| 1755 | AllOf(Contains(Labeled("void vfunc(bool param, int p) override")), |
| 1756 | Contains(Labeled("void ttt(bool param) const override")), |
| 1757 | Not(Contains(Labeled("void vfunc(bool param) override"))))); |
| 1758 | } |
| 1759 | |
Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 1760 | TEST(SpeculateCompletionFilter, Filters) { |
| 1761 | Annotations F(R"cpp($bof^ |
| 1762 | $bol^ |
| 1763 | ab$ab^ |
| 1764 | x.ab$dot^ |
| 1765 | x.$dotempty^ |
| 1766 | x::ab$scoped^ |
| 1767 | x::$scopedempty^ |
| 1768 | |
| 1769 | )cpp"); |
| 1770 | auto speculate = [&](StringRef PointName) { |
| 1771 | auto Filter = speculateCompletionFilter(F.code(), F.point(PointName)); |
| 1772 | assert(Filter); |
| 1773 | return *Filter; |
| 1774 | }; |
| 1775 | EXPECT_EQ(speculate("bof"), ""); |
| 1776 | EXPECT_EQ(speculate("bol"), ""); |
| 1777 | EXPECT_EQ(speculate("ab"), "ab"); |
| 1778 | EXPECT_EQ(speculate("dot"), "ab"); |
| 1779 | EXPECT_EQ(speculate("dotempty"), ""); |
| 1780 | EXPECT_EQ(speculate("scoped"), "ab"); |
| 1781 | EXPECT_EQ(speculate("scopedempty"), ""); |
| 1782 | } |
| 1783 | |
| 1784 | TEST(CompletionTest, EnableSpeculativeIndexRequest) { |
| 1785 | MockFSProvider FS; |
| 1786 | MockCompilationDatabase CDB; |
| 1787 | IgnoreDiagnostics DiagConsumer; |
| 1788 | ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); |
| 1789 | |
| 1790 | auto File = testPath("foo.cpp"); |
| 1791 | Annotations Test(R"cpp( |
| 1792 | namespace ns1 { int abc; } |
| 1793 | namespace ns2 { int abc; } |
| 1794 | void f() { ns1::ab$1^; ns1::ab$2^; } |
| 1795 | void f() { ns2::ab$3^; } |
| 1796 | )cpp"); |
| 1797 | runAddDocument(Server, File, Test.code()); |
| 1798 | clangd::CodeCompleteOptions Opts = {}; |
| 1799 | |
| 1800 | IndexRequestCollector Requests; |
| 1801 | Opts.Index = &Requests; |
| 1802 | Opts.SpeculativeIndexRequest = true; |
| 1803 | |
| 1804 | auto CompleteAtPoint = [&](StringRef P) { |
| 1805 | cantFail(runCodeComplete(Server, File, Test.point(P), Opts)); |
| 1806 | // Sleep for a while to make sure asynchronous call (if applicable) is also |
| 1807 | // triggered before callback is invoked. |
| 1808 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 1809 | }; |
| 1810 | |
| 1811 | CompleteAtPoint("1"); |
| 1812 | auto Reqs1 = Requests.consumeRequests(); |
| 1813 | ASSERT_EQ(Reqs1.size(), 1u); |
| 1814 | EXPECT_THAT(Reqs1[0].Scopes, UnorderedElementsAre("ns1::")); |
| 1815 | |
| 1816 | CompleteAtPoint("2"); |
| 1817 | auto Reqs2 = Requests.consumeRequests(); |
| 1818 | // Speculation succeeded. Used speculative index result. |
| 1819 | ASSERT_EQ(Reqs2.size(), 1u); |
| 1820 | EXPECT_EQ(Reqs2[0], Reqs1[0]); |
| 1821 | |
| 1822 | CompleteAtPoint("3"); |
| 1823 | // Speculation failed. Sent speculative index request and the new index |
| 1824 | // request after sema. |
| 1825 | auto Reqs3 = Requests.consumeRequests(); |
| 1826 | ASSERT_EQ(Reqs3.size(), 2u); |
| 1827 | } |
| 1828 | |
Sam McCall | 9aad25f | 2017-12-05 07:20:26 +0000 | [diff] [blame] | 1829 | } // namespace |
| 1830 | } // namespace clangd |
| 1831 | } // namespace clang |