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" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 12 | #include "Headers.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 13 | #include "SourceCode.h" |
Sam McCall | a66d2cb | 2017-12-19 17:06:07 +0000 | [diff] [blame] | 14 | #include "XRefs.h" |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 15 | #include "index/Merge.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 16 | #include "clang/Format/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CompilerInstance.h" |
| 18 | #include "clang/Frontend/CompilerInvocation.h" |
| 19 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 9e11c4c | 2017-11-15 18:04:56 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/Refactoring/RefactoringResultConsumer.h" |
| 21 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
Sam McCall | b5f5eb6 | 2018-01-25 17:01:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ScopeExit.h" |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Errc.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <future> |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 30 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 31 | using namespace clang::clangd; |
| 32 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 35 | void ignoreError(llvm::Error Err) { |
| 36 | handleAllErrors(std::move(Err), [](const llvm::ErrorInfoBase &) {}); |
| 37 | } |
| 38 | |
Ilya Biryukov | a46f7a9 | 2017-06-28 10:34:50 +0000 | [diff] [blame] | 39 | std::string getStandardResourceDir() { |
| 40 | static int Dummy; // Just an address in this process. |
| 41 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 42 | } |
| 43 | |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 44 | class RefactoringResultCollector final |
| 45 | : public tooling::RefactoringResultConsumer { |
| 46 | public: |
| 47 | void handleError(llvm::Error Err) override { |
| 48 | assert(!Result.hasValue()); |
| 49 | // FIXME: figure out a way to return better message for DiagnosticError. |
| 50 | // clangd uses llvm::toString to convert the Err to string, however, for |
| 51 | // DiagnosticError, only "clang diagnostic" will be generated. |
| 52 | Result = std::move(Err); |
| 53 | } |
| 54 | |
| 55 | // Using the handle(SymbolOccurrences) from parent class. |
| 56 | using tooling::RefactoringResultConsumer::handle; |
| 57 | |
| 58 | void handle(tooling::AtomicChanges SourceReplacements) override { |
| 59 | assert(!Result.hasValue()); |
| 60 | Result = std::move(SourceReplacements); |
| 61 | } |
| 62 | |
| 63 | Optional<Expected<tooling::AtomicChanges>> Result; |
| 64 | }; |
| 65 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 66 | } // namespace |
| 67 | |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 68 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 69 | RealFileSystemProvider::getTaggedFileSystem(PathRef File) { |
Ilya Biryukov | 2260299 | 2017-05-30 15:11:02 +0000 | [diff] [blame] | 70 | return make_tagged(vfs::getRealFileSystem(), VFSTag()); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 73 | ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB, |
| 74 | DiagnosticsConsumer &DiagConsumer, |
| 75 | FileSystemProvider &FSProvider, |
| 76 | unsigned AsyncThreadsCount, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 77 | bool StorePreamblesInMemory, |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 78 | bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 79 | llvm::Optional<StringRef> ResourceDir) |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 80 | : CompileArgs(CDB, |
| 81 | ResourceDir ? ResourceDir->str() : getStandardResourceDir()), |
| 82 | DiagConsumer(DiagConsumer), FSProvider(FSProvider), |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 83 | FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr), |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 84 | PCHs(std::make_shared<PCHContainerOperations>()), |
| 85 | // Pass a callback into `WorkScheduler` to extract symbols from a newly |
| 86 | // parsed file and rebuild the file index synchronously each time an AST |
| 87 | // is parsed. |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 88 | // FIXME(ioeric): this can be slow and we may be able to index on less |
| 89 | // critical paths. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 90 | WorkScheduler(AsyncThreadsCount, StorePreamblesInMemory, |
| 91 | FileIdx |
| 92 | ? [this](PathRef Path, |
| 93 | ParsedAST *AST) { FileIdx->update(Path, AST); } |
| 94 | : ASTParsedCallback()) { |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 95 | if (FileIdx && StaticIdx) { |
| 96 | MergedIndex = mergeIndex(FileIdx.get(), StaticIdx); |
| 97 | Index = MergedIndex.get(); |
| 98 | } else if (FileIdx) |
| 99 | Index = FileIdx.get(); |
| 100 | else if (StaticIdx) |
| 101 | Index = StaticIdx; |
| 102 | else |
| 103 | Index = nullptr; |
| 104 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 105 | |
Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 106 | void ClangdServer::setRootPath(PathRef RootPath) { |
| 107 | std::string NewRootPath = llvm::sys::path::convert_to_slash( |
| 108 | RootPath, llvm::sys::path::Style::posix); |
| 109 | if (llvm::sys::fs::is_directory(NewRootPath)) |
| 110 | this->RootPath = NewRootPath; |
| 111 | } |
| 112 | |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 113 | void ClangdServer::addDocument(PathRef File, StringRef Contents, |
| 114 | WantDiagnostics WantDiags) { |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 115 | DocVersion Version = DraftMgr.updateDraft(File, Contents); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 116 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 117 | scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()}, |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 118 | WantDiags, std::move(TaggedFS)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 121 | void ClangdServer::removeDocument(PathRef File) { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 122 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 123 | CompileArgs.invalidate(File); |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 124 | WorkScheduler.remove(File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 127 | void ClangdServer::forceReparse(PathRef File) { |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 128 | auto FileContents = DraftMgr.getDraft(File); |
| 129 | assert(FileContents.Draft && |
| 130 | "forceReparse() was called for non-added document"); |
| 131 | |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 132 | // forceReparse promises to request new compilation flags from CDB, so we |
| 133 | // remove any cahced flags. |
| 134 | CompileArgs.invalidate(File); |
| 135 | |
Ilya Biryukov | 91dbf5b | 2017-08-14 08:37:32 +0000 | [diff] [blame] | 136 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 137 | scheduleReparseAndDiags(File, std::move(FileContents), WantDiagnostics::Yes, |
| 138 | std::move(TaggedFS)); |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 141 | void ClangdServer::codeComplete( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 142 | PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, |
| 143 | UniqueFunction<void(Tagged<CompletionList>)> Callback, |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 144 | llvm::Optional<StringRef> OverridenContents, |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 145 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 146 | using CallbackType = UniqueFunction<void(Tagged<CompletionList>)>; |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 147 | |
Ilya Biryukov | af0c04b | 2017-06-14 09:46:44 +0000 | [diff] [blame] | 148 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | ed99e4c | 2017-07-31 17:09:29 +0000 | [diff] [blame] | 149 | if (UsedFS) |
| 150 | *UsedFS = TaggedFS.Value; |
| 151 | |
Ilya Biryukov | d3b04e3 | 2017-12-05 10:42:57 +0000 | [diff] [blame] | 152 | // Copy completion options for passing them to async task handler. |
| 153 | auto CodeCompleteOpts = Opts; |
Sam McCall | 0faecf0 | 2018-01-15 12:33:00 +0000 | [diff] [blame] | 154 | if (!CodeCompleteOpts.Index) // Respect overridden index. |
| 155 | CodeCompleteOpts.Index = Index; |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 156 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 157 | std::string Contents; |
| 158 | if (OverridenContents) { |
| 159 | Contents = OverridenContents->str(); |
| 160 | } else { |
| 161 | VersionedDraft Latest = DraftMgr.getDraft(File); |
| 162 | assert(Latest.Draft && "codeComplete called for non-added document"); |
| 163 | Contents = *Latest.Draft; |
| 164 | } |
| 165 | |
Ilya Biryukov | f6e2b4c | 2018-01-09 14:39:27 +0000 | [diff] [blame] | 166 | // Copy PCHs to avoid accessing this->PCHs concurrently |
| 167 | std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs; |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 168 | auto Task = [PCHs, Pos, TaggedFS, CodeCompleteOpts]( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 169 | std::string Contents, Path File, CallbackType Callback, |
| 170 | llvm::Expected<InputsAndPreamble> IP) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 171 | assert(IP && "error when trying to read preamble for codeComplete"); |
| 172 | auto PreambleData = IP->Preamble; |
| 173 | auto &Command = IP->Inputs.CompileCommand; |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 174 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 175 | // FIXME(ibiryukov): even if Preamble is non-null, we may want to check |
| 176 | // both the old and the new version in case only one of them matches. |
| 177 | CompletionList Result = clangd::codeComplete( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 178 | File, Command, PreambleData ? &PreambleData->Preamble : nullptr, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 179 | Contents, Pos, TaggedFS.Value, PCHs, CodeCompleteOpts); |
Ilya Biryukov | 90bbcfd | 2017-10-25 09:35:10 +0000 | [diff] [blame] | 180 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 181 | Callback(make_tagged(std::move(Result), std::move(TaggedFS.Tag))); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 184 | WorkScheduler.runWithPreamble("CodeComplete", File, |
| 185 | BindWithForward(Task, std::move(Contents), |
| 186 | File.str(), |
| 187 | std::move(Callback))); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 188 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 189 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 190 | void ClangdServer::signatureHelp( |
| 191 | PathRef File, Position Pos, |
| 192 | UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback, |
| 193 | llvm::Optional<StringRef> OverridenContents, |
| 194 | IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) { |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 195 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 196 | if (UsedFS) |
| 197 | *UsedFS = TaggedFS.Value; |
| 198 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 199 | std::string Contents; |
| 200 | if (OverridenContents) { |
| 201 | Contents = OverridenContents->str(); |
| 202 | } else { |
| 203 | VersionedDraft Latest = DraftMgr.getDraft(File); |
| 204 | if (!Latest.Draft) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 205 | return Callback(llvm::make_error<llvm::StringError>( |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 206 | "signatureHelp is called for non-added document", |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 207 | llvm::errc::invalid_argument)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 208 | Contents = std::move(*Latest.Draft); |
| 209 | } |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 210 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 211 | auto PCHs = this->PCHs; |
| 212 | auto Action = [Contents, Pos, TaggedFS, |
| 213 | PCHs](Path File, decltype(Callback) Callback, |
| 214 | llvm::Expected<InputsAndPreamble> IP) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 215 | if (!IP) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 216 | return Callback(IP.takeError()); |
| 217 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 218 | auto PreambleData = IP->Preamble; |
| 219 | auto &Command = IP->Inputs.CompileCommand; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 220 | Callback(make_tagged( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 221 | clangd::signatureHelp(File, Command, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 222 | PreambleData ? &PreambleData->Preamble : nullptr, |
| 223 | Contents, Pos, TaggedFS.Value, PCHs), |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 224 | TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 225 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 226 | |
| 227 | WorkScheduler.runWithPreamble( |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 228 | "SignatureHelp", File, |
| 229 | BindWithForward(Action, File.str(), std::move(Callback))); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 232 | llvm::Expected<tooling::Replacements> |
| 233 | ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 234 | size_t Begin = positionToOffset(Code, Rng.start); |
| 235 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 236 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 237 | } |
| 238 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 239 | llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code, |
| 240 | PathRef File) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 241 | // Format everything. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 242 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 243 | } |
| 244 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 245 | llvm::Expected<tooling::Replacements> |
| 246 | ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 247 | // Look for the previous opening brace from the character position and |
| 248 | // format starting from there. |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 249 | size_t CursorPos = positionToOffset(Code, Pos); |
| 250 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 251 | if (PreviousLBracePos == StringRef::npos) |
| 252 | PreviousLBracePos = CursorPos; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 253 | size_t Len = CursorPos - PreviousLBracePos; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 254 | |
| 255 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 256 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 257 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 258 | void ClangdServer::rename( |
| 259 | PathRef File, Position Pos, llvm::StringRef NewName, |
| 260 | UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)> |
| 261 | Callback) { |
| 262 | auto Action = [Pos](Path File, std::string NewName, |
| 263 | decltype(Callback) Callback, |
| 264 | Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 265 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 266 | return Callback(InpAST.takeError()); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 267 | auto &AST = InpAST->AST; |
| 268 | |
| 269 | RefactoringResultCollector ResultCollector; |
| 270 | const SourceManager &SourceMgr = AST.getASTContext().getSourceManager(); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 271 | const FileEntry *FE = |
| 272 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 273 | if (!FE) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 274 | return Callback(llvm::make_error<llvm::StringError>( |
| 275 | "rename called for non-added document", |
| 276 | llvm::errc::invalid_argument)); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 277 | SourceLocation SourceLocationBeg = |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 278 | clangd::getBeginningOfIdentifier(AST, Pos, FE); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 279 | tooling::RefactoringRuleContext Context( |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 280 | AST.getASTContext().getSourceManager()); |
| 281 | Context.setASTContext(AST.getASTContext()); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 282 | auto Rename = clang::tooling::RenameOccurrences::initiate( |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 283 | Context, SourceRange(SourceLocationBeg), NewName); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 284 | if (!Rename) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 285 | return Callback(Rename.takeError()); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 286 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 287 | Rename->invoke(ResultCollector, Context); |
| 288 | |
| 289 | assert(ResultCollector.Result.hasValue()); |
| 290 | if (!ResultCollector.Result.getValue()) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 291 | return Callback(ResultCollector.Result->takeError()); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 292 | |
| 293 | std::vector<tooling::Replacement> Replacements; |
| 294 | for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { |
| 295 | tooling::Replacements ChangeReps = Change.getReplacements(); |
| 296 | for (const auto &Rep : ChangeReps) { |
| 297 | // FIXME: Right now we only support renaming the main file, so we |
| 298 | // drop replacements not for the main file. In the future, we might |
| 299 | // consider to support: |
| 300 | // * rename in any included header |
| 301 | // * rename only in the "main" header |
| 302 | // * provide an error if there are symbols we won't rename (e.g. |
| 303 | // std::vector) |
| 304 | // * rename globally in project |
| 305 | // * rename in open files |
| 306 | if (Rep.getFilePath() == File) |
| 307 | Replacements.push_back(Rep); |
| 308 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 309 | } |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 310 | return Callback(Replacements); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 311 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 312 | |
| 313 | WorkScheduler.runWithAST( |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 314 | "Rename", File, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 315 | BindWithForward(Action, File.str(), NewName.str(), std::move(Callback))); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 318 | Expected<tooling::Replacements> |
| 319 | ClangdServer::insertInclude(PathRef File, StringRef Code, |
| 320 | llvm::StringRef Header) { |
| 321 | std::string ToInclude; |
| 322 | if (Header.startswith("<") || Header.startswith("\"")) { |
| 323 | ToInclude = Header; |
| 324 | } else { |
| 325 | auto U = URI::parse(Header); |
| 326 | if (!U) |
| 327 | return U.takeError(); |
| 328 | auto Resolved = URI::resolve(*U, /*HintPath=*/File); |
| 329 | if (!Resolved) |
| 330 | return Resolved.takeError(); |
| 331 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 332 | tooling::CompileCommand CompileCommand = |
| 333 | CompileArgs.getCompileCommand(File); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 334 | auto Include = |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 335 | calculateIncludePath(File, Code, *Resolved, CompileCommand, |
| 336 | FSProvider.getTaggedFileSystem(File).Value); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 337 | if (!Include) |
| 338 | return Include.takeError(); |
| 339 | if (Include->empty()) |
| 340 | return tooling::Replacements(); |
| 341 | ToInclude = std::move(*Include); |
| 342 | } |
| 343 | |
| 344 | auto Style = format::getStyle("file", File, "llvm"); |
| 345 | if (!Style) { |
| 346 | llvm::consumeError(Style.takeError()); |
| 347 | // FIXME(ioeric): needs more consistent style support in clangd server. |
| 348 | Style = format::getLLVMStyle(); |
| 349 | } |
| 350 | // Replacement with offset UINT_MAX and length 0 will be treated as include |
| 351 | // insertion. |
| 352 | tooling::Replacement R(File, /*Offset=*/UINT_MAX, 0, "#include " + ToInclude); |
| 353 | return format::cleanupAroundReplacements(Code, tooling::Replacements(R), |
| 354 | *Style); |
| 355 | } |
| 356 | |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 357 | llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) { |
| 358 | auto Latest = DraftMgr.getDraft(File); |
| 359 | if (!Latest.Draft) |
| 360 | return llvm::None; |
| 361 | return std::move(*Latest.Draft); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 364 | void ClangdServer::dumpAST(PathRef File, |
| 365 | UniqueFunction<void(std::string)> Callback) { |
| 366 | auto Action = [](decltype(Callback) Callback, |
| 367 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 368 | if (!InpAST) { |
| 369 | ignoreError(InpAST.takeError()); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 370 | return Callback("<no-ast>"); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 371 | } |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 372 | std::string Result; |
| 373 | |
| 374 | llvm::raw_string_ostream ResultOS(Result); |
| 375 | clangd::dumpAST(InpAST->AST, ResultOS); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 376 | ResultOS.flush(); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 377 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 378 | Callback(Result); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 379 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 380 | |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 381 | WorkScheduler.runWithAST("DumpAST", File, |
| 382 | BindWithForward(Action, std::move(Callback))); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 383 | } |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 384 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 385 | void ClangdServer::findDefinitions( |
| 386 | PathRef File, Position Pos, |
| 387 | UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)> |
| 388 | Callback) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 389 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 390 | auto Action = [Pos, TaggedFS](decltype(Callback) Callback, |
| 391 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 392 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 393 | return Callback(InpAST.takeError()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 394 | auto Result = clangd::findDefinitions(InpAST->AST, Pos); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 395 | Callback(make_tagged(std::move(Result), TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 396 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 397 | |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 398 | WorkScheduler.runWithAST("Definitions", File, |
| 399 | BindWithForward(Action, std::move(Callback))); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 400 | } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 401 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 402 | llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) { |
| 403 | |
| 404 | StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", |
| 405 | ".c++", ".m", ".mm"}; |
| 406 | StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; |
| 407 | |
| 408 | StringRef PathExt = llvm::sys::path::extension(Path); |
| 409 | |
| 410 | // Lookup in a list of known extensions. |
| 411 | auto SourceIter = |
| 412 | std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions), |
| 413 | [&PathExt](PathRef SourceExt) { |
| 414 | return SourceExt.equals_lower(PathExt); |
| 415 | }); |
| 416 | bool IsSource = SourceIter != std::end(SourceExtensions); |
| 417 | |
| 418 | auto HeaderIter = |
| 419 | std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions), |
| 420 | [&PathExt](PathRef HeaderExt) { |
| 421 | return HeaderExt.equals_lower(PathExt); |
| 422 | }); |
| 423 | |
| 424 | bool IsHeader = HeaderIter != std::end(HeaderExtensions); |
| 425 | |
| 426 | // We can only switch between extensions known extensions. |
| 427 | if (!IsSource && !IsHeader) |
| 428 | return llvm::None; |
| 429 | |
| 430 | // Array to lookup extensions for the switch. An opposite of where original |
| 431 | // extension was found. |
| 432 | ArrayRef<StringRef> NewExts; |
| 433 | if (IsSource) |
| 434 | NewExts = HeaderExtensions; |
| 435 | else |
| 436 | NewExts = SourceExtensions; |
| 437 | |
| 438 | // Storage for the new path. |
| 439 | SmallString<128> NewPath = StringRef(Path); |
| 440 | |
| 441 | // Instance of vfs::FileSystem, used for file existence checks. |
| 442 | auto FS = FSProvider.getTaggedFileSystem(Path).Value; |
| 443 | |
| 444 | // Loop through switched extension candidates. |
| 445 | for (StringRef NewExt : NewExts) { |
| 446 | llvm::sys::path::replace_extension(NewPath, NewExt); |
| 447 | if (FS->exists(NewPath)) |
| 448 | return NewPath.str().str(); // First str() to convert from SmallString to |
| 449 | // StringRef, second to convert from StringRef |
| 450 | // to std::string |
Ilya Biryukov | 3333494 | 2017-10-06 14:39:39 +0000 | [diff] [blame] | 451 | |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 452 | // Also check NewExt in upper-case, just in case. |
| 453 | llvm::sys::path::replace_extension(NewPath, NewExt.upper()); |
| 454 | if (FS->exists(NewPath)) |
| 455 | return NewPath.str().str(); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return llvm::None; |
| 459 | } |
| 460 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 461 | llvm::Expected<tooling::Replacements> |
| 462 | ClangdServer::formatCode(llvm::StringRef Code, PathRef File, |
| 463 | ArrayRef<tooling::Range> Ranges) { |
| 464 | // Call clang-format. |
| 465 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 466 | auto StyleOrError = |
| 467 | format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get()); |
| 468 | if (!StyleOrError) { |
| 469 | return StyleOrError.takeError(); |
| 470 | } else { |
| 471 | return format::reformat(StyleOrError.get(), Code, Ranges, File); |
| 472 | } |
| 473 | } |
| 474 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 475 | void ClangdServer::findDocumentHighlights( |
| 476 | PathRef File, Position Pos, |
| 477 | UniqueFunction<void(llvm::Expected<Tagged<std::vector<DocumentHighlight>>>)> |
| 478 | Callback) { |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 479 | auto FileContents = DraftMgr.getDraft(File); |
| 480 | if (!FileContents.Draft) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 481 | return Callback(llvm::make_error<llvm::StringError>( |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 482 | "findDocumentHighlights called on non-added file", |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 483 | llvm::errc::invalid_argument)); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 484 | |
| 485 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 486 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 487 | auto Action = [TaggedFS, Pos](decltype(Callback) Callback, |
| 488 | llvm::Expected<InputsAndAST> InpAST) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 489 | if (!InpAST) |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 490 | return Callback(InpAST.takeError()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 491 | auto Result = clangd::findDocumentHighlights(InpAST->AST, Pos); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 492 | Callback(make_tagged(std::move(Result), TaggedFS.Tag)); |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 493 | }; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 494 | |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 495 | WorkScheduler.runWithAST("Highlights", File, |
| 496 | BindWithForward(Action, std::move(Callback))); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 499 | void ClangdServer::findHover( |
| 500 | PathRef File, Position Pos, |
| 501 | UniqueFunction<void(llvm::Expected<Tagged<Hover>>)> Callback) { |
| 502 | Hover FinalHover; |
| 503 | auto FileContents = DraftMgr.getDraft(File); |
| 504 | if (!FileContents.Draft) |
| 505 | return Callback(llvm::make_error<llvm::StringError>( |
| 506 | "findHover called on non-added file", llvm::errc::invalid_argument)); |
| 507 | |
| 508 | auto TaggedFS = FSProvider.getTaggedFileSystem(File); |
| 509 | |
| 510 | auto Action = [Pos, TaggedFS](decltype(Callback) Callback, |
| 511 | llvm::Expected<InputsAndAST> InpAST) { |
| 512 | if (!InpAST) |
| 513 | return Callback(InpAST.takeError()); |
| 514 | |
| 515 | Hover Result = clangd::getHover(InpAST->AST, Pos); |
| 516 | Callback(make_tagged(std::move(Result), TaggedFS.Tag)); |
| 517 | }; |
| 518 | |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 519 | WorkScheduler.runWithAST("Hover", File, |
| 520 | BindWithForward(Action, std::move(Callback))); |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 523 | void ClangdServer::scheduleReparseAndDiags( |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 524 | PathRef File, VersionedDraft Contents, WantDiagnostics WantDiags, |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 525 | Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 526 | tooling::CompileCommand Command = CompileArgs.getCompileCommand(File); |
Ilya Biryukov | 929697b | 2018-01-25 14:19:21 +0000 | [diff] [blame] | 527 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 528 | DocVersion Version = Contents.Version; |
| 529 | Path FileStr = File.str(); |
| 530 | VFSTag Tag = std::move(TaggedFS.Tag); |
| 531 | |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 532 | auto Callback = [this, Version, FileStr, |
| 533 | Tag](std::vector<DiagWithFixIts> Diags) { |
Ilya Biryukov | 47f2202 | 2017-09-20 12:58:55 +0000 | [diff] [blame] | 534 | // We need to serialize access to resulting diagnostics to avoid calling |
| 535 | // `onDiagnosticsReady` in the wrong order. |
| 536 | std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex); |
| 537 | DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr]; |
| 538 | // FIXME(ibiryukov): get rid of '<' comparison here. In the current |
| 539 | // implementation diagnostics will not be reported after version counters' |
| 540 | // overflow. This should not happen in practice, since DocVersion is a |
| 541 | // 64-bit unsigned integer. |
| 542 | if (Version < LastReportedDiagsVersion) |
| 543 | return; |
| 544 | LastReportedDiagsVersion = Version; |
| 545 | |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 546 | DiagConsumer.onDiagnosticsReady( |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 547 | FileStr, make_tagged(std::move(Diags), std::move(Tag))); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 548 | }; |
| 549 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 550 | WorkScheduler.update(File, |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 551 | ParseInputs{std::move(Command), |
| 552 | std::move(TaggedFS.Value), |
| 553 | std::move(*Contents.Draft)}, |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame^] | 554 | WantDiags, std::move(Callback)); |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 555 | } |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 556 | |
| 557 | void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
| 558 | // FIXME: Do nothing for now. This will be used for indexing and potentially |
| 559 | // invalidating other caches. |
| 560 | } |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 561 | |
| 562 | std::vector<std::pair<Path, std::size_t>> |
| 563 | ClangdServer::getUsedBytesPerFile() const { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 564 | return WorkScheduler.getUsedBytesPerFile(); |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 565 | } |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 566 | |
| 567 | LLVM_NODISCARD bool |
| 568 | ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) { |
| 569 | return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds)); |
| 570 | } |