xref: /llvm-project/clang-tools-extra/clangd/unittests/SyncAPI.cpp (revision f71ffd3b735b4d6ae3c12be1806cdd6205b3b378)
1b804eef0SSam McCall //===--- SyncAPI.cpp - Sync version of ClangdServer's API --------*- C++-*-===//
2b804eef0SSam McCall //
3b804eef0SSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b804eef0SSam McCall // See https://llvm.org/LICENSE.txt for license information.
5b804eef0SSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b804eef0SSam McCall //
7b804eef0SSam McCall //===----------------------------------------------------------------------===//
8b804eef0SSam McCall 
9b804eef0SSam McCall #include "SyncAPI.h"
10d3606a33SKadir Cetinkaya #include "Protocol.h"
11b804eef0SSam McCall #include "index/Index.h"
1271f55735SKazu Hirata #include <optional>
13b804eef0SSam McCall 
14b804eef0SSam McCall namespace clang {
15b804eef0SSam McCall namespace clangd {
16b804eef0SSam McCall 
runAddDocument(ClangdServer & Server,PathRef File,llvm::StringRef Contents,llvm::StringRef Version,WantDiagnostics WantDiags,bool ForceRebuild)17b804eef0SSam McCall void runAddDocument(ClangdServer &Server, PathRef File,
182cd33e6fSSam McCall                     llvm::StringRef Contents, llvm::StringRef Version,
192cd33e6fSSam McCall                     WantDiagnostics WantDiags, bool ForceRebuild) {
202cd33e6fSSam McCall   Server.addDocument(File, Contents, Version, WantDiags, ForceRebuild);
21b804eef0SSam McCall   if (!Server.blockUntilIdleForTest())
22b804eef0SSam McCall     llvm_unreachable("not idle after addDocument");
23b804eef0SSam McCall }
24b804eef0SSam McCall 
25b804eef0SSam McCall namespace {
26b804eef0SSam McCall /// A helper that waits for async callbacks to fire and exposes their result in
27b804eef0SSam McCall /// the output variable. Intended to be used in the following way:
28b804eef0SSam McCall ///    T Result;
29b804eef0SSam McCall ///    someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result));
30b804eef0SSam McCall template <typename T> struct CaptureProxy {
CaptureProxyclang::clangd::__anon9caddfa70111::CaptureProxy31*f71ffd3bSKazu Hirata   CaptureProxy(std::optional<T> &Target) : Target(&Target) { assert(!Target); }
32b804eef0SSam McCall 
33b804eef0SSam McCall   CaptureProxy(const CaptureProxy &) = delete;
34b804eef0SSam McCall   CaptureProxy &operator=(const CaptureProxy &) = delete;
35b804eef0SSam McCall   // We need move ctor to return a value from the 'capture' helper.
CaptureProxyclang::clangd::__anon9caddfa70111::CaptureProxy36b804eef0SSam McCall   CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) {
37b804eef0SSam McCall     Other.Target = nullptr;
38b804eef0SSam McCall   }
39b804eef0SSam McCall   CaptureProxy &operator=(CaptureProxy &&) = delete;
40b804eef0SSam McCall 
operator llvm::unique_function<void(T)>clang::clangd::__anon9caddfa70111::CaptureProxy41b804eef0SSam McCall   operator llvm::unique_function<void(T)>() && {
42b804eef0SSam McCall     assert(!Future.valid() && "conversion to callback called multiple times");
43b804eef0SSam McCall     Future = Promise.get_future();
44b3c2f5d2SIlya Biryukov     return [Promise = std::move(Promise)](T Value) mutable {
45b804eef0SSam McCall       Promise.set_value(std::make_shared<T>(std::move(Value)));
46b3c2f5d2SIlya Biryukov     };
47b804eef0SSam McCall   }
48b804eef0SSam McCall 
~CaptureProxyclang::clangd::__anon9caddfa70111::CaptureProxy49b804eef0SSam McCall   ~CaptureProxy() {
50b804eef0SSam McCall     if (!Target)
51b804eef0SSam McCall       return;
52b804eef0SSam McCall     assert(Future.valid() && "conversion to callback was not called");
53b8df4093SKazu Hirata     assert(!Target->has_value());
54b804eef0SSam McCall     Target->emplace(std::move(*Future.get()));
55b804eef0SSam McCall   }
56b804eef0SSam McCall 
57b804eef0SSam McCall private:
58*f71ffd3bSKazu Hirata   std::optional<T> *Target;
59b804eef0SSam McCall   // Using shared_ptr to workaround compilation errors with MSVC.
60b7ecf1c1SKazuaki Ishizaki   // MSVC only allows default-constructible and copyable objects as future<>
61b804eef0SSam McCall   // arguments.
62b804eef0SSam McCall   std::promise<std::shared_ptr<T>> Promise;
63b804eef0SSam McCall   std::future<std::shared_ptr<T>> Future;
64b804eef0SSam McCall };
65b804eef0SSam McCall 
capture(std::optional<T> & Target)66*f71ffd3bSKazu Hirata template <typename T> CaptureProxy<T> capture(std::optional<T> &Target) {
67b804eef0SSam McCall   return CaptureProxy<T>(Target);
68b804eef0SSam McCall }
69b804eef0SSam McCall } // namespace
70b804eef0SSam McCall 
71b804eef0SSam McCall llvm::Expected<CodeCompleteResult>
runCodeComplete(ClangdServer & Server,PathRef File,Position Pos,clangd::CodeCompleteOptions Opts)72b804eef0SSam McCall runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
73b804eef0SSam McCall                 clangd::CodeCompleteOptions Opts) {
74*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<CodeCompleteResult>> Result;
75b804eef0SSam McCall   Server.codeComplete(File, Pos, Opts, capture(Result));
76b804eef0SSam McCall   return std::move(*Result);
77b804eef0SSam McCall }
78b804eef0SSam McCall 
runSignatureHelp(ClangdServer & Server,PathRef File,Position Pos,MarkupKind DocumentationFormat)79b804eef0SSam McCall llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server,
80d3606a33SKadir Cetinkaya                                                PathRef File, Position Pos,
81d3606a33SKadir Cetinkaya                                                MarkupKind DocumentationFormat) {
82*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<SignatureHelp>> Result;
83d3606a33SKadir Cetinkaya   Server.signatureHelp(File, Pos, DocumentationFormat, capture(Result));
84b804eef0SSam McCall   return std::move(*Result);
85b804eef0SSam McCall }
86b804eef0SSam McCall 
87b804eef0SSam McCall llvm::Expected<std::vector<LocatedSymbol>>
runLocateSymbolAt(ClangdServer & Server,PathRef File,Position Pos)88b804eef0SSam McCall runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos) {
89*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<std::vector<LocatedSymbol>>> Result;
90b804eef0SSam McCall   Server.locateSymbolAt(File, Pos, capture(Result));
91b804eef0SSam McCall   return std::move(*Result);
92b804eef0SSam McCall }
93b804eef0SSam McCall 
94b804eef0SSam McCall llvm::Expected<std::vector<DocumentHighlight>>
runFindDocumentHighlights(ClangdServer & Server,PathRef File,Position Pos)95b804eef0SSam McCall runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos) {
96*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<std::vector<DocumentHighlight>>> Result;
97b804eef0SSam McCall   Server.findDocumentHighlights(File, Pos, capture(Result));
98b804eef0SSam McCall   return std::move(*Result);
99b804eef0SSam McCall }
100b804eef0SSam McCall 
runRename(ClangdServer & Server,PathRef File,Position Pos,llvm::StringRef NewName,const RenameOptions & RenameOpts)1010f0cbcc4SHaojian Wu llvm::Expected<RenameResult> runRename(ClangdServer &Server, PathRef File,
10234d0e1bdSHaojian Wu                                        Position Pos, llvm::StringRef NewName,
10334d0e1bdSHaojian Wu                                        const RenameOptions &RenameOpts) {
104*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<RenameResult>> Result;
10534d0e1bdSHaojian Wu   Server.rename(File, Pos, NewName, RenameOpts, capture(Result));
106b804eef0SSam McCall   return std::move(*Result);
107b804eef0SSam McCall }
108b804eef0SSam McCall 
1099c09e205SHaojian Wu llvm::Expected<RenameResult>
runPrepareRename(ClangdServer & Server,PathRef File,Position Pos,std::optional<std::string> NewName,const RenameOptions & RenameOpts)1109c09e205SHaojian Wu runPrepareRename(ClangdServer &Server, PathRef File, Position Pos,
111*f71ffd3bSKazu Hirata                  std::optional<std::string> NewName,
1120f0cbcc4SHaojian Wu                  const RenameOptions &RenameOpts) {
113*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<RenameResult>> Result;
1149c09e205SHaojian Wu   Server.prepareRename(File, Pos, NewName, RenameOpts, capture(Result));
1150f0cbcc4SHaojian Wu   return std::move(*Result);
1160f0cbcc4SHaojian Wu }
1170f0cbcc4SHaojian Wu 
118ffa63ddeSSam McCall llvm::Expected<tooling::Replacements>
runFormatFile(ClangdServer & Server,PathRef File,std::optional<Range> Rng)119*f71ffd3bSKazu Hirata runFormatFile(ClangdServer &Server, PathRef File, std::optional<Range> Rng) {
120*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<tooling::Replacements>> Result;
121bca3e241SSam McCall   Server.formatFile(File, Rng, capture(Result));
122ffa63ddeSSam McCall   return std::move(*Result);
123ffa63ddeSSam McCall }
124ffa63ddeSSam McCall 
runFuzzyFind(const SymbolIndex & Index,llvm::StringRef Query)125b804eef0SSam McCall SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
126b804eef0SSam McCall   FuzzyFindRequest Req;
127adcd0268SBenjamin Kramer   Req.Query = std::string(Query);
128b804eef0SSam McCall   Req.AnyScope = true;
129b804eef0SSam McCall   return runFuzzyFind(Index, Req);
130b804eef0SSam McCall }
131b804eef0SSam McCall 
runFuzzyFind(const SymbolIndex & Index,const FuzzyFindRequest & Req)132b804eef0SSam McCall SymbolSlab runFuzzyFind(const SymbolIndex &Index, const FuzzyFindRequest &Req) {
133b804eef0SSam McCall   SymbolSlab::Builder Builder;
134b804eef0SSam McCall   Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
135b804eef0SSam McCall   return std::move(Builder).build();
136b804eef0SSam McCall }
137b804eef0SSam McCall 
getRefs(const SymbolIndex & Index,SymbolID ID)138b804eef0SSam McCall RefSlab getRefs(const SymbolIndex &Index, SymbolID ID) {
139b804eef0SSam McCall   RefsRequest Req;
140b804eef0SSam McCall   Req.IDs = {ID};
141b804eef0SSam McCall   RefSlab::Builder Slab;
142b804eef0SSam McCall   Index.refs(Req, [&](const Ref &S) { Slab.insert(ID, S); });
143b804eef0SSam McCall   return std::move(Slab).build();
144b804eef0SSam McCall }
145b804eef0SSam McCall 
1468f237f9bSSam McCall llvm::Expected<std::vector<SelectionRange>>
runSemanticRanges(ClangdServer & Server,PathRef File,const std::vector<Position> & Pos)1478f237f9bSSam McCall runSemanticRanges(ClangdServer &Server, PathRef File,
1488f237f9bSSam McCall                   const std::vector<Position> &Pos) {
149*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<std::vector<SelectionRange>>> Result;
15079d19bdfSUtkarsh Saxena   Server.semanticRanges(File, Pos, capture(Result));
15179d19bdfSUtkarsh Saxena   return std::move(*Result);
15279d19bdfSUtkarsh Saxena }
15379d19bdfSUtkarsh Saxena 
154*f71ffd3bSKazu Hirata llvm::Expected<std::optional<clangd::Path>>
runSwitchHeaderSource(ClangdServer & Server,PathRef File)155d6d5eddcSHaojian Wu runSwitchHeaderSource(ClangdServer &Server, PathRef File) {
156*f71ffd3bSKazu Hirata   std::optional<llvm::Expected<std::optional<clangd::Path>>> Result;
157d6d5eddcSHaojian Wu   Server.switchSourceHeader(File, capture(Result));
158d6d5eddcSHaojian Wu   return std::move(*Result);
159d6d5eddcSHaojian Wu }
160d6d5eddcSHaojian Wu 
runCustomAction(ClangdServer & Server,PathRef File,llvm::function_ref<void (InputsAndAST)> Action)16132626bccSSam McCall llvm::Error runCustomAction(ClangdServer &Server, PathRef File,
16232626bccSSam McCall                             llvm::function_ref<void(InputsAndAST)> Action) {
16332626bccSSam McCall   llvm::Error Result = llvm::Error::success();
16432626bccSSam McCall   Notification Done;
16532626bccSSam McCall   Server.customAction(File, "Custom", [&](llvm::Expected<InputsAndAST> AST) {
16632626bccSSam McCall     if (!AST)
16732626bccSSam McCall       Result = AST.takeError();
16832626bccSSam McCall     else
16932626bccSSam McCall       Action(*AST);
17032626bccSSam McCall     Done.notify();
17132626bccSSam McCall   });
17232626bccSSam McCall   Done.wait();
17332626bccSSam McCall   return Result;
17432626bccSSam McCall }
17532626bccSSam McCall 
176b804eef0SSam McCall } // namespace clangd
177b804eef0SSam McCall } // namespace clang
178