Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 1 | //===--- SyncAPI.cpp - Sync version of ClangdServer's API --------*- 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 | #include "SyncAPI.h" |
| 10 | |
| 11 | namespace clang { |
| 12 | namespace clangd { |
| 13 | |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 14 | void runAddDocument(ClangdServer &Server, PathRef File, StringRef Contents) { |
| 15 | Server.addDocument(File, Contents); |
| 16 | if (!Server.blockUntilIdleForTest()) |
| 17 | llvm_unreachable("not idle after addDocument"); |
| 18 | } |
| 19 | |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | /// A helper that waits for async callbacks to fire and exposes their result in |
| 22 | /// the output variable. Intended to be used in the following way: |
| 23 | /// T Result; |
| 24 | /// someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result)); |
| 25 | template <typename T> struct CaptureProxy { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 26 | CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) { |
| 27 | assert(!Target.hasValue()); |
| 28 | } |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 29 | |
| 30 | CaptureProxy(const CaptureProxy &) = delete; |
| 31 | CaptureProxy &operator=(const CaptureProxy &) = delete; |
| 32 | // We need move ctor to return a value from the 'capture' helper. |
| 33 | CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) { |
| 34 | Other.Target = nullptr; |
| 35 | } |
| 36 | CaptureProxy &operator=(CaptureProxy &&) = delete; |
| 37 | |
| 38 | operator UniqueFunction<void(T)>() && { |
| 39 | assert(!Future.valid() && "conversion to callback called multiple times"); |
| 40 | Future = Promise.get_future(); |
Sam McCall | 091557d | 2018-02-23 07:54:17 +0000 | [diff] [blame] | 41 | return Bind( |
Ilya Biryukov | 40bf17c | 2018-02-15 15:41:49 +0000 | [diff] [blame] | 42 | [](std::promise<std::shared_ptr<T>> Promise, T Value) { |
| 43 | Promise.set_value(std::make_shared<T>(std::move(Value))); |
| 44 | }, |
| 45 | std::move(Promise)); |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ~CaptureProxy() { |
| 49 | if (!Target) |
| 50 | return; |
| 51 | assert(Future.valid() && "conversion to callback was not called"); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 52 | assert(!Target->hasValue()); |
Ilya Biryukov | 40bf17c | 2018-02-15 15:41:49 +0000 | [diff] [blame] | 53 | Target->emplace(std::move(*Future.get())); |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 57 | llvm::Optional<T> *Target; |
Ilya Biryukov | 40bf17c | 2018-02-15 15:41:49 +0000 | [diff] [blame] | 58 | // Using shared_ptr to workaround compilation errors with MSVC. |
| 59 | // MSVC only allows default-construcitble and copyable objects as future<> |
| 60 | // arguments. |
| 61 | std::promise<std::shared_ptr<T>> Promise; |
| 62 | std::future<std::shared_ptr<T>> Future; |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 65 | template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) { |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 66 | return CaptureProxy<T>(Target); |
| 67 | } |
| 68 | } // namespace |
| 69 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 70 | llvm::Expected<CompletionList> |
| 71 | runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, |
| 72 | clangd::CodeCompleteOptions Opts) { |
| 73 | llvm::Optional<llvm::Expected<CompletionList>> Result; |
Sam McCall | db3ea4c | 2018-02-27 17:15:50 +0000 | [diff] [blame] | 74 | Server.codeComplete(File, Pos, Opts, capture(Result)); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 75 | return std::move(*Result); |
| 76 | } |
| 77 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 78 | llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server, |
| 79 | PathRef File, Position Pos) { |
| 80 | llvm::Optional<llvm::Expected<SignatureHelp>> Result; |
Sam McCall | db3ea4c | 2018-02-27 17:15:50 +0000 | [diff] [blame] | 81 | Server.signatureHelp(File, Pos, capture(Result)); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 82 | return std::move(*Result); |
| 83 | } |
| 84 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 85 | llvm::Expected<std::vector<Location>> |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 86 | runFindDefinitions(ClangdServer &Server, PathRef File, Position Pos) { |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 87 | llvm::Optional<llvm::Expected<std::vector<Location>>> Result; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 88 | Server.findDefinitions(File, Pos, capture(Result)); |
| 89 | return std::move(*Result); |
| 90 | } |
| 91 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 92 | llvm::Expected<std::vector<DocumentHighlight>> |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 93 | runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos) { |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 94 | llvm::Optional<llvm::Expected<std::vector<DocumentHighlight>>> Result; |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 95 | Server.findDocumentHighlights(File, Pos, capture(Result)); |
| 96 | return std::move(*Result); |
| 97 | } |
| 98 | |
| 99 | llvm::Expected<std::vector<tooling::Replacement>> |
| 100 | runRename(ClangdServer &Server, PathRef File, Position Pos, StringRef NewName) { |
| 101 | llvm::Optional<llvm::Expected<std::vector<tooling::Replacement>>> Result; |
| 102 | Server.rename(File, Pos, NewName, capture(Result)); |
| 103 | return std::move(*Result); |
| 104 | } |
| 105 | |
| 106 | std::string runDumpAST(ClangdServer &Server, PathRef File) { |
| 107 | llvm::Optional<std::string> Result; |
| 108 | Server.dumpAST(File, capture(Result)); |
| 109 | return std::move(*Result); |
Ilya Biryukov | cd5eb00 | 2018-02-12 11:37:28 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace clangd |
| 113 | } // namespace clang |