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