blob: 9e6ab28c7a7fbf484befa572b82d6e043d909012 [file] [log] [blame]
Ilya Biryukovcd5eb002018-02-12 11:37:28 +00001//===--- 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
11namespace clang {
12namespace clangd {
13
14namespace {
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));
19template <typename T> struct CaptureProxy {
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000020 CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) {
21 assert(!Target.hasValue());
22 }
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000023
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 Biryukov40bf17c2018-02-15 15:41:49 +000035 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 Biryukovcd5eb002018-02-12 11:37:28 +000040 }
41
42 ~CaptureProxy() {
43 if (!Target)
44 return;
45 assert(Future.valid() && "conversion to callback was not called");
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000046 assert(!Target->hasValue());
Ilya Biryukov40bf17c2018-02-15 15:41:49 +000047 Target->emplace(std::move(*Future.get()));
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000048 }
49
50private:
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000051 llvm::Optional<T> *Target;
Ilya Biryukov40bf17c2018-02-15 15:41:49 +000052 // 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 Biryukovcd5eb002018-02-12 11:37:28 +000057};
58
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000059template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) {
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000060 return CaptureProxy<T>(Target);
61}
62} // namespace
63
64Tagged<CompletionList>
65runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
66 clangd::CodeCompleteOptions Opts,
67 llvm::Optional<StringRef> OverridenContents) {
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000068 llvm::Optional<Tagged<CompletionList>> Result;
Ilya Biryukovcd5eb002018-02-12 11:37:28 +000069 Server.codeComplete(File, Pos, Opts, capture(Result), OverridenContents);
Ilya Biryukov2c5e8e82018-02-15 13:15:47 +000070 return std::move(*Result);
71}
72
73llvm::Expected<Tagged<SignatureHelp>>
74runSignatureHelp(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
81llvm::Expected<Tagged<std::vector<Location>>>
82runFindDefinitions(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
88llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
89runFindDocumentHighlights(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
95llvm::Expected<std::vector<tooling::Replacement>>
96runRename(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
102std::string runDumpAST(ClangdServer &Server, PathRef File) {
103 llvm::Optional<std::string> Result;
104 Server.dumpAST(File, capture(Result));
105 return std::move(*Result);
Ilya Biryukovcd5eb002018-02-12 11:37:28 +0000106}
107
108} // namespace clangd
109} // namespace clang