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