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