Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdServer.cpp - Main clangd server code --------------*- 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 | //===-------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangdServer.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 11 | #include "SourceCode.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 12 | #include "clang/Format/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 9e11c4c | 2017-11-15 18:04:56 +0000 | [diff] [blame] | 16 | #include "clang/Tooling/Refactoring/RefactoringResultConsumer.h" |
| 17 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Errc.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormatProviders.h" |
| 22 | #include "llvm/Support/FormatVariadic.h" |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <future> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 26 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 27 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 28 | using namespace clang::clangd; |
| 29 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 32 | std::string getStandardResourceDir() { |
| 33 | static int Dummy; // Just an address in this process. |
| 34 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 35 | } |
| 36 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 37 | class RefactoringResultCollector final |
| 38 | : public tooling::RefactoringResultConsumer { |
| 39 | public: |
| 40 | void handleError(llvm::Error Err) override { |
| 41 | assert(!Result.hasValue()); |
| 42 | // FIXME: figure out a way to return better message for DiagnosticError. |
| 43 | // clangd uses llvm::toString to convert the Err to string, however, for |
| 44 | // DiagnosticError, only "clang diagnostic" will be generated. |
| 45 | Result = std::move(Err); |
| 46 | } |
| 47 | |
| 48 | // Using the handle(SymbolOccurrences) from parent class. |
| 49 | using tooling::RefactoringResultConsumer::handle; |
| 50 | |
| 51 | void handle(tooling::AtomicChanges SourceReplacements) override { |
| 52 | assert(!Result.hasValue()); |
| 53 | Result = std::move(SourceReplacements); |
| 54 | } |
| 55 | |
| 56 | Optional<Expected<tooling::AtomicChanges>> Result; |
| 57 | }; |
| 58 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 59 | } // namespace |
| 60 | |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 61 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 62 | RealFileSystemProvider::getTaggedFileSystem(PathRef File) { |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 63 | return make_tagged(vfs::getRealFileSystem(), VFSTag()); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 66 | unsigned clangd::getDefaultAsyncThreadsCount() { |
| 67 | unsigned HardwareConcurrency = std::thread::hardware_concurrency(); |
| 68 | // C++ standard says that hardware_concurrency() |
| 69 | // may return 0, fallback to 1 worker thread in |
| 70 | // that case. |
| 71 | if (HardwareConcurrency == 0) |
| 72 | return 1; |
| 73 | return HardwareConcurrency; |
| 74 | } |
| 75 | |
| 76 | ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount) |
| 77 | : RunSynchronously(AsyncThreadsCount == 0) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 78 | if (RunSynchronously) { |
| 79 | // Don't start the worker thread if we're running synchronously |
| 80 | return; |
| 81 | } |
| 82 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 83 | Workers.reserve(AsyncThreadsCount); |
| 84 | for (unsigned I = 0; I < AsyncThreadsCount; ++I) { |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 85 | Workers.push_back(std::thread([this, I]() { |
| 86 | llvm::set_thread_name(llvm::formatv("scheduler/{0}", I)); |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 87 | while (true) { |
Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 88 | UniqueFunction<void()> Request; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 89 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 90 | // Pick request from the queue |
| 91 | { |
| 92 | std::unique_lock<std::mutex> Lock(Mutex); |
| 93 | // Wait for more requests. |
| 94 | RequestCV.wait(Lock, |
| 95 | [this] { return !RequestQueue.empty() || Done; }); |
| 96 | if (Done) |
| 97 | return; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 98 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 99 | assert(!RequestQueue.empty() && "RequestQueue was empty"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 100 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 101 | // We process requests starting from the front of the queue. Users of |
| 102 | // ClangdScheduler have a way to prioritise their requests by putting |
| 103 | // them to the either side of the queue (using either addToEnd or |
| 104 | // addToFront). |
| 105 | Request = std::move(RequestQueue.front()); |
| 106 | RequestQueue.pop_front(); |
| 107 | } // unlock Mutex |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 108 | |
Ilya Biryukov | 08e6ccb | 2017-10-09 16:26:26 +0000 | [diff] [blame] | 109 | Request(); |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 110 | } |
| 111 | })); |
| 112 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | ClangdScheduler::~ClangdScheduler() { |
| 116 | if (RunSynchronously) |
| 117 | return; // no worker thread is running in that case |
| 118 | |
| 119 | { |
| 120 | std::lock_guard<std::mutex> Lock(Mutex); |
| 121 | // Wake up the worker thread |
| 122 | Done = true; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 123 | } // unlock Mutex |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 124 | RequestCV.notify_all(); |
| 125 | |
| 126 | for (auto &Worker : Workers) |
| 127 | Worker.join(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 130 | ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB, |
| 131 | DiagnosticsConsumer &DiagConsumer, |
| 132 | FileSystemProvider &FSProvider, |
| 133 | unsigned AsyncThreadsCount, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 134 | bool StorePreamblesInMemory, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 135 | llvm::Optional<StringRef> ResourceDir) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 136 | : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider), |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 137 | ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()), |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 138 | PCHs(std::make_shared<PCHContainerOperations>()), |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 139 | StorePreamblesInMemory(StorePreamblesInMemory), |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 140 | WorkScheduler(AsyncThreadsCount) {} |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 141 | |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 142 | void ClangdServer::setRootPath(PathRef RootPath) { |
| 143 | std::string NewRootPath = llvm::sys::path::convert_to_slash( |
| 144 | RootPath, llvm::sys::path::Style::posix); |
| 145 | if (llvm::sys::fs::is_directory(NewRootPath)) |
| 146 | this->RootPath = NewRootPath; |
| 147 | } |
| 148 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 149 | std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File, |
| 150 | StringRef Contents) { |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 151 | DocVersion Version = DraftMgr.updateDraft(File, Contents); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 152 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 153 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 154 | std::shared_ptr<CppFile> Resources = Units.getOrCreateFile( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 155 | File, ResourceDir, CDB, StorePreamblesInMemory, PCHs); |
| 156 | return scheduleReparseAndDiags(std::move(Ctx), File, |
| 157 | VersionedDraft{Version, Contents.str()}, |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 158 | std::move(Resources), std::move(TaggedFS)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 161 | std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 162 | DraftMgr.removeDraft(File); |
| 163 | std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 164 | return scheduleCancelRebuild(std::move(Ctx), std::move(Resources)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 167 | std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) { |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 168 | auto FileContents = DraftMgr.getDraft(File); |
| 169 | assert(FileContents.Draft && |
| 170 | "forceReparse() was called for non-added document"); |
| 171 | |
| 172 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 173 | auto Recreated = Units.recreateFileIfCompileCommandChanged( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 174 | File, ResourceDir, CDB, StorePreamblesInMemory, PCHs); |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 175 | |
| 176 | // Note that std::future from this cleanup action is ignored. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 177 | scheduleCancelRebuild(Ctx.clone(), std::move(Recreated.RemovedFile)); |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 178 | // Schedule a reparse. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 179 | return scheduleReparseAndDiags(std::move(Ctx), File, std::move(FileContents), |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 180 | std::move(Recreated.FileInCollection), |
| 181 | std::move(TaggedFS)); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 184 | std::future<std::pair<Context, Tagged<CompletionList>>> |
| 185 | ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 186 | const clangd::CodeCompleteOptions &Opts, |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 187 | llvm::Optional<StringRef> OverridenContents, |
| 188 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 189 | using ResultType = std::pair<Context, Tagged<CompletionList>>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 190 | |
| 191 | std::promise<ResultType> ResultPromise; |
| 192 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 193 | auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx, |
| 194 | Tagged<CompletionList> Result) -> void { |
| 195 | ResultPromise.set_value({std::move(Ctx), std::move(Result)}); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | std::future<ResultType> ResultFuture = ResultPromise.get_future(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 199 | codeComplete(std::move(Ctx), File, Pos, Opts, |
| 200 | BindWithForward(Callback, std::move(ResultPromise)), |
| 201 | OverridenContents, UsedFS); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 202 | return ResultFuture; |
| 203 | } |
| 204 | |
| 205 | void ClangdServer::codeComplete( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 206 | Context Ctx, PathRef File, Position Pos, |
| 207 | const clangd::CodeCompleteOptions &Opts, |
| 208 | UniqueFunction<void(Context, Tagged<CompletionList>)> Callback, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 209 | llvm::Optional<StringRef> OverridenContents, |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 210 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 211 | using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 212 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 213 | std::string Contents; |
| 214 | if (OverridenContents) { |
| 215 | Contents = *OverridenContents; |
| 216 | } else { |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 217 | auto FileContents = DraftMgr.getDraft(File); |
| 218 | assert(FileContents.Draft && |
| 219 | "codeComplete is called for non-added document"); |
| 220 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 221 | Contents = std::move(*FileContents.Draft); |
Ilya Biryukov | 0e27ce4 | 2017-06-13 14:15:56 +0000 | [diff] [blame] | 222 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 223 | |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 224 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 225 | if (UsedFS) |
| 226 | *UsedFS = TaggedFS.Value; |
| 227 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 228 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 229 | assert(Resources && "Calling completion on non-added file"); |
| 230 | |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 231 | // Remember the current Preamble and use it when async task starts executing. |
| 232 | // At the point when async task starts executing, we may have a different |
| 233 | // Preamble in Resources. However, we assume the Preamble that we obtain here |
| 234 | // is reusable in completion more often. |
| 235 | std::shared_ptr<const PreambleData> Preamble = |
| 236 | Resources->getPossiblyStalePreamble(); |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 237 | // Copy completion options for passing them to async task handler. |
| 238 | auto CodeCompleteOpts = Opts; |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 239 | // A task that will be run asynchronously. |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 240 | auto Task = |
| 241 | // 'mutable' to reassign Preamble variable. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 242 | [=](Context Ctx, CallbackType Callback) mutable { |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 243 | if (!Preamble) { |
| 244 | // Maybe we built some preamble before processing this request. |
| 245 | Preamble = Resources->getPossiblyStalePreamble(); |
| 246 | } |
| 247 | // FIXME(ibiryukov): even if Preamble is non-null, we may want to check |
| 248 | // both the old and the new version in case only one of them matches. |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 249 | |
Sam McCall | a40371b | 2017-11-15 09:16:29 +0000 | [diff] [blame] | 250 | CompletionList Result = clangd::codeComplete( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 251 | Ctx, File, Resources->getCompileCommand(), |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 252 | Preamble ? &Preamble->Preamble : nullptr, Contents, Pos, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 253 | TaggedFS.Value, PCHs, CodeCompleteOpts); |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 254 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 255 | Callback(std::move(Ctx), |
| 256 | make_tagged(std::move(Result), std::move(TaggedFS.Tag))); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 259 | WorkScheduler.addToFront(std::move(Task), std::move(Ctx), |
| 260 | std::move(Callback)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 261 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 262 | |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 263 | llvm::Expected<Tagged<SignatureHelp>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 264 | ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos, |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 265 | llvm::Optional<StringRef> OverridenContents, |
| 266 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
| 267 | std::string DraftStorage; |
| 268 | if (!OverridenContents) { |
| 269 | auto FileContents = DraftMgr.getDraft(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 270 | if (!FileContents.Draft) |
| 271 | return llvm::make_error<llvm::StringError>( |
| 272 | "signatureHelp is called for non-added document", |
| 273 | llvm::errc::invalid_argument); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 274 | |
| 275 | DraftStorage = std::move(*FileContents.Draft); |
| 276 | OverridenContents = DraftStorage; |
| 277 | } |
| 278 | |
| 279 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 280 | if (UsedFS) |
| 281 | *UsedFS = TaggedFS.Value; |
| 282 | |
| 283 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 284 | if (!Resources) |
| 285 | return llvm::make_error<llvm::StringError>( |
| 286 | "signatureHelp is called for non-added document", |
| 287 | llvm::errc::invalid_argument); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 288 | |
| 289 | auto Preamble = Resources->getPossiblyStalePreamble(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 290 | auto Result = |
| 291 | clangd::signatureHelp(Ctx, File, Resources->getCompileCommand(), |
| 292 | Preamble ? &Preamble->Preamble : nullptr, |
| 293 | *OverridenContents, Pos, TaggedFS.Value, PCHs); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 294 | return make_tagged(std::move(Result), TaggedFS.Tag); |
| 295 | } |
| 296 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 297 | llvm::Expected<tooling::Replacements> |
| 298 | ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 299 | size_t Begin = positionToOffset(Code, Rng.start); |
| 300 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 301 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 302 | } |
| 303 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 304 | llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code, |
| 305 | PathRef File) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 306 | // Format everything. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 307 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 308 | } |
| 309 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 310 | llvm::Expected<tooling::Replacements> |
| 311 | ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 312 | // Look for the previous opening brace from the character position and |
| 313 | // format starting from there. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 314 | size_t CursorPos = positionToOffset(Code, Pos); |
| 315 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 316 | if (PreviousLBracePos == StringRef::npos) |
| 317 | PreviousLBracePos = CursorPos; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 318 | size_t Len = CursorPos - PreviousLBracePos; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 319 | |
| 320 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 321 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 322 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 323 | Expected<std::vector<tooling::Replacement>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 324 | ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos, |
| 325 | llvm::StringRef NewName) { |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 326 | std::string Code = getDocument(File); |
| 327 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 328 | RefactoringResultCollector ResultCollector; |
| 329 | Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) { |
| 330 | const SourceManager &SourceMgr = AST->getASTContext().getSourceManager(); |
| 331 | const FileEntry *FE = |
| 332 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 333 | if (!FE) |
| 334 | return; |
| 335 | SourceLocation SourceLocationBeg = |
| 336 | clangd::getBeginningOfIdentifier(*AST, Pos, FE); |
| 337 | tooling::RefactoringRuleContext Context( |
| 338 | AST->getASTContext().getSourceManager()); |
| 339 | Context.setASTContext(AST->getASTContext()); |
| 340 | auto Rename = clang::tooling::RenameOccurrences::initiate( |
| 341 | Context, SourceRange(SourceLocationBeg), NewName.str()); |
| 342 | if (!Rename) { |
| 343 | ResultCollector.Result = Rename.takeError(); |
| 344 | return; |
| 345 | } |
| 346 | Rename->invoke(ResultCollector, Context); |
| 347 | }); |
| 348 | assert(ResultCollector.Result.hasValue()); |
| 349 | if (!ResultCollector.Result.getValue()) |
| 350 | return ResultCollector.Result->takeError(); |
| 351 | |
| 352 | std::vector<tooling::Replacement> Replacements; |
| 353 | for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { |
| 354 | tooling::Replacements ChangeReps = Change.getReplacements(); |
| 355 | for (const auto &Rep : ChangeReps) { |
| 356 | // FIXME: Right now we only support renaming the main file, so we drop |
| 357 | // replacements not for the main file. In the future, we might consider to |
| 358 | // support: |
| 359 | // * rename in any included header |
| 360 | // * rename only in the "main" header |
| 361 | // * provide an error if there are symbols we won't rename (e.g. |
| 362 | // std::vector) |
| 363 | // * rename globally in project |
| 364 | // * rename in open files |
| 365 | if (Rep.getFilePath() == File) |
| 366 | Replacements.push_back(Rep); |
| 367 | } |
| 368 | } |
| 369 | return Replacements; |
| 370 | } |
| 371 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 372 | std::string ClangdServer::getDocument(PathRef File) { |
| 373 | auto draft = DraftMgr.getDraft(File); |
| 374 | assert(draft.Draft && "File is not tracked, cannot get contents"); |
| 375 | return *draft.Draft; |
| 376 | } |
| 377 | |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 378 | std::string ClangdServer::dumpAST(PathRef File) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 379 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 380 | assert(Resources && "dumpAST is called for non-added document"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 381 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 382 | std::string Result; |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 383 | Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 384 | llvm::raw_string_ostream ResultOS(Result); |
| 385 | if (AST) { |
| 386 | clangd::dumpAST(*AST, ResultOS); |
| 387 | } else { |
| 388 | ResultOS << "<no-ast>"; |
| 389 | } |
| 390 | ResultOS.flush(); |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 391 | }); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 392 | return Result; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 393 | } |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 394 | |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 395 | llvm::Expected<Tagged<std::vector<Location>>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 396 | ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 397 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 398 | |
| 399 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 400 | if (!Resources) |
| 401 | return llvm::make_error<llvm::StringError>( |
| 402 | "findDefinitions called on non-added file", |
| 403 | llvm::errc::invalid_argument); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 404 | |
| 405 | std::vector<Location> Result; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 406 | Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 407 | if (!AST) |
| 408 | return; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 409 | Result = clangd::findDefinitions(Ctx, *AST, Pos); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 410 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 411 | return make_tagged(std::move(Result), TaggedFS.Tag); |
| 412 | } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 413 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 414 | llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) { |
| 415 | |
| 416 | StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", |
| 417 | ".c++", ".m", ".mm"}; |
| 418 | StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; |
| 419 | |
| 420 | StringRef PathExt = llvm::sys::path::extension(Path); |
| 421 | |
| 422 | // Lookup in a list of known extensions. |
| 423 | auto SourceIter = |
| 424 | std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions), |
| 425 | [&PathExt](PathRef SourceExt) { |
| 426 | return SourceExt.equals_lower(PathExt); |
| 427 | }); |
| 428 | bool IsSource = SourceIter != std::end(SourceExtensions); |
| 429 | |
| 430 | auto HeaderIter = |
| 431 | std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions), |
| 432 | [&PathExt](PathRef HeaderExt) { |
| 433 | return HeaderExt.equals_lower(PathExt); |
| 434 | }); |
| 435 | |
| 436 | bool IsHeader = HeaderIter != std::end(HeaderExtensions); |
| 437 | |
| 438 | // We can only switch between extensions known extensions. |
| 439 | if (!IsSource && !IsHeader) |
| 440 | return llvm::None; |
| 441 | |
| 442 | // Array to lookup extensions for the switch. An opposite of where original |
| 443 | // extension was found. |
| 444 | ArrayRef<StringRef> NewExts; |
| 445 | if (IsSource) |
| 446 | NewExts = HeaderExtensions; |
| 447 | else |
| 448 | NewExts = SourceExtensions; |
| 449 | |
| 450 | // Storage for the new path. |
| 451 | SmallString<128> NewPath = StringRef(Path); |
| 452 | |
| 453 | // Instance of vfs::FileSystem, used for file existence checks. |
| 454 | auto FS = FSProvider.getTaggedFileSystem(Path).Value; |
| 455 | |
| 456 | // Loop through switched extension candidates. |
| 457 | for (StringRef NewExt : NewExts) { |
| 458 | llvm::sys::path::replace_extension(NewPath, NewExt); |
| 459 | if (FS->exists(NewPath)) |
| 460 | return NewPath.str().str(); // First str() to convert from SmallString to |
| 461 | // StringRef, second to convert from StringRef |
| 462 | // to std::string |
Ilya Biryukov | 3333494 | 2017-10-06 14:39:39 +0000 | [diff] [blame] | 463 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 464 | // Also check NewExt in upper-case, just in case. |
| 465 | llvm::sys::path::replace_extension(NewPath, NewExt.upper()); |
| 466 | if (FS->exists(NewPath)) |
| 467 | return NewPath.str().str(); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return llvm::None; |
| 471 | } |
| 472 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 473 | llvm::Expected<tooling::Replacements> |
| 474 | ClangdServer::formatCode(llvm::StringRef Code, PathRef File, |
| 475 | ArrayRef<tooling::Range> Ranges) { |
| 476 | // Call clang-format. |
| 477 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 478 | auto StyleOrError = |
| 479 | format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get()); |
| 480 | if (!StyleOrError) { |
| 481 | return StyleOrError.takeError(); |
| 482 | } else { |
| 483 | return format::reformat(StyleOrError.get(), Code, Ranges, File); |
| 484 | } |
| 485 | } |
| 486 | |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 487 | llvm::Expected<Tagged<std::vector<DocumentHighlight>>> |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 488 | ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File, |
| 489 | Position Pos) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 490 | auto FileContents = DraftMgr.getDraft(File); |
| 491 | if (!FileContents.Draft) |
| 492 | return llvm::make_error<llvm::StringError>( |
| 493 | "findDocumentHighlights called on non-added file", |
| 494 | llvm::errc::invalid_argument); |
| 495 | |
| 496 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 497 | |
| 498 | std::shared_ptr<CppFile> Resources = Units.getFile(File); |
| 499 | if (!Resources) |
| 500 | return llvm::make_error<llvm::StringError>( |
| 501 | "findDocumentHighlights called on non-added file", |
| 502 | llvm::errc::invalid_argument); |
| 503 | |
| 504 | std::vector<DocumentHighlight> Result; |
| 505 | llvm::Optional<llvm::Error> Err; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 506 | Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err, |
| 507 | &Result](ParsedAST *AST) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 508 | if (!AST) { |
| 509 | Err = llvm::make_error<llvm::StringError>("Invalid AST", |
| 510 | llvm::errc::invalid_argument); |
| 511 | return; |
| 512 | } |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 513 | Result = clangd::findDocumentHighlights(Ctx, *AST, Pos); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 514 | }); |
| 515 | |
| 516 | if (Err) |
| 517 | return std::move(*Err); |
| 518 | return make_tagged(Result, TaggedFS.Tag); |
| 519 | } |
| 520 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 521 | std::future<Context> ClangdServer::scheduleReparseAndDiags( |
| 522 | Context Ctx, PathRef File, VersionedDraft Contents, |
| 523 | std::shared_ptr<CppFile> Resources, |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 524 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) { |
| 525 | |
| 526 | assert(Contents.Draft && "Draft must have contents"); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 527 | UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)> |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 528 | DeferredRebuild = |
| 529 | Resources->deferRebuild(*Contents.Draft, TaggedFS.Value); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 530 | std::promise<Context> DonePromise; |
| 531 | std::future<Context> DoneFuture = DonePromise.get_future(); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 532 | |
| 533 | DocVersion Version = Contents.Version; |
| 534 | Path FileStr = File; |
| 535 | VFSTag Tag = TaggedFS.Tag; |
| 536 | auto ReparseAndPublishDiags = |
| 537 | [this, FileStr, Version, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 538 | Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>( |
| 539 | const Context &)> |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 540 | DeferredRebuild, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 541 | std::promise<Context> DonePromise, Context Ctx) -> void { |
| 542 | auto Guard = onScopeExit([&]() { DonePromise.set_value(std::move(Ctx)); }); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 543 | |
| 544 | auto CurrentVersion = DraftMgr.getVersion(FileStr); |
| 545 | if (CurrentVersion != Version) |
| 546 | return; // This request is outdated |
| 547 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 548 | auto Diags = DeferredRebuild(Ctx); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 549 | if (!Diags) |
| 550 | return; // A new reparse was requested before this one completed. |
Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 551 | |
| 552 | // We need to serialize access to resulting diagnostics to avoid calling |
| 553 | // `onDiagnosticsReady` in the wrong order. |
| 554 | std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex); |
| 555 | DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr]; |
| 556 | // FIXME(ibiryukov): get rid of '<' comparison here. In the current |
| 557 | // implementation diagnostics will not be reported after version counters' |
| 558 | // overflow. This should not happen in practice, since DocVersion is a |
| 559 | // 64-bit unsigned integer. |
| 560 | if (Version < LastReportedDiagsVersion) |
| 561 | return; |
| 562 | LastReportedDiagsVersion = Version; |
| 563 | |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 564 | DiagConsumer.onDiagnosticsReady(FileStr, |
| 565 | make_tagged(std::move(*Diags), Tag)); |
| 566 | }; |
| 567 | |
| 568 | WorkScheduler.addToFront(std::move(ReparseAndPublishDiags), |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 569 | std::move(DeferredRebuild), std::move(DonePromise), |
| 570 | std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 571 | return DoneFuture; |
| 572 | } |
| 573 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 574 | std::future<Context> |
| 575 | ClangdServer::scheduleCancelRebuild(Context Ctx, |
| 576 | std::shared_ptr<CppFile> Resources) { |
| 577 | std::promise<Context> DonePromise; |
| 578 | std::future<Context> DoneFuture = DonePromise.get_future(); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 579 | if (!Resources) { |
| 580 | // No need to schedule any cleanup. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 581 | DonePromise.set_value(std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 582 | return DoneFuture; |
| 583 | } |
| 584 | |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 585 | UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 586 | auto CancelReparses = [Resources](std::promise<Context> DonePromise, |
| 587 | UniqueFunction<void()> DeferredCancel, |
| 588 | Context Ctx) { |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 589 | DeferredCancel(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 590 | DonePromise.set_value(std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 591 | }; |
| 592 | WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise), |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 593 | std::move(DeferredCancel), std::move(Ctx)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 594 | return DoneFuture; |
| 595 | } |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 596 | |
| 597 | void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
| 598 | // FIXME: Do nothing for now. This will be used for indexing and potentially |
| 599 | // invalidating other caches. |
| 600 | } |