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