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