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