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