clang-tools  14.0.0git
LSPClient.h
Go to the documentation of this file.
1 //===-- LSPClient.h - Helper for ClangdLSPServer tests ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <condition_variable>
10 #include <deque>
11 #include <llvm/ADT/Optional.h>
12 #include <llvm/Support/Error.h>
13 #include <llvm/Support/JSON.h>
14 #include <mutex>
15 
16 namespace clang {
17 namespace clangd {
18 class Transport;
19 
20 // A client library for talking to ClangdLSPServer in tests.
21 // Manages serialization of messages, pairing requests/repsonses, and implements
22 // the Transport abstraction.
23 class LSPClient {
24  class TransportImpl;
25  std::unique_ptr<TransportImpl> T;
26 
27 public:
28  // Represents the result of an LSP call: a promise for a result or error.
29  class CallResult {
30  public:
31  ~CallResult();
32  // Blocks up to 10 seconds for the result to be ready.
33  // Records a test failure if there was no reply.
34  llvm::Expected<llvm::json::Value> take();
35  // Like take(), but records a test failure if the result was an error.
37 
38  private:
39  // Should be called once to provide the value.
40  void set(llvm::Expected<llvm::json::Value> V);
41 
42  llvm::Optional<llvm::Expected<llvm::json::Value>> Value;
43  std::mutex Mu;
44  std::condition_variable CV;
45 
46  friend TransportImpl; // Calls set().
47  };
48 
49  LSPClient();
50  ~LSPClient();
51  LSPClient(LSPClient &&) = delete;
52  LSPClient &operator=(LSPClient &&) = delete;
53 
54  // Enqueue an LSP method call, returns a promise for the reply. Threadsafe.
55  CallResult &call(llvm::StringRef Method, llvm::json::Value Params);
56  // Enqueue an LSP notification. Threadsafe.
57  void notify(llvm::StringRef Method, llvm::json::Value Params);
58  // Returns matching notifications since the last call to takeNotifications.
59  std::vector<llvm::json::Value> takeNotifications(llvm::StringRef Method);
60  // The transport is shut down after all pending messages are sent.
61  void stop();
62 
63  // Shorthand for common LSP methods. Relative paths are passed to testPath().
64  static llvm::json::Value uri(llvm::StringRef Path);
65  static llvm::json::Value documentID(llvm::StringRef Path);
66  void didOpen(llvm::StringRef Path, llvm::StringRef Content);
67  void didChange(llvm::StringRef Path, llvm::StringRef Content);
68  void didClose(llvm::StringRef Path);
69  // Blocks until the server is idle (using the 'sync' protocol extension).
70  void sync();
71  // sync()s to ensure pending diagnostics arrive, and returns the newest set.
72  llvm::Optional<std::vector<llvm::json::Value>>
73  diagnostics(llvm::StringRef Path);
74 
75  // Get the transport used to connect this client to a ClangdLSPServer.
77 
78 private:
79 };
80 
81 } // namespace clangd
82 } // namespace clang
clang::clangd::LSPClient::didOpen
void didOpen(llvm::StringRef Path, llvm::StringRef Content)
Definition: LSPClient.cpp:176
clang::clangd::LSPClient::diagnostics
llvm::Optional< std::vector< llvm::json::Value > > diagnostics(llvm::StringRef Path)
Definition: LSPClient.cpp:194
clang::clangd::LSPClient::CallResult::takeValue
llvm::json::Value takeValue()
Definition: LSPClient.cpp:28
clang::clangd::LSPClient::CallResult::~CallResult
~CallResult()
Definition: LSPClient.cpp:48
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
clang::clangd::LSPClient::TransportImpl
Definition: LSPClient.cpp:59
clang::clangd::LSPClient::takeNotifications
std::vector< llvm::json::Value > takeNotifications(llvm::StringRef Method)
Definition: LSPClient.cpp:156
clang::clangd::LSPClient::LSPClient
LSPClient()
Definition: LSPClient.cpp:133
clang::clangd::LSPClient::CallResult
Definition: LSPClient.h:29
clang::clangd::LSPClient::~LSPClient
~LSPClient()
clang::clangd::LSPClient::notify
void notify(llvm::StringRef Method, llvm::json::Value Params)
Definition: LSPClient.cpp:147
clang::clangd::LSPClient::call
CallResult & call(llvm::StringRef Method, llvm::json::Value Params)
Definition: LSPClient.cpp:136
clang::clangd::LSPClient::didChange
void didChange(llvm::StringRef Path, llvm::StringRef Content)
Definition: LSPClient.cpp:182
clang::clangd::Transport
Definition: Transport.h:35
clang::clangd::LSPClient::operator=
LSPClient & operator=(LSPClient &&)=delete
clang::clangd::LSPClient::didClose
void didClose(llvm::StringRef Path)
Definition: LSPClient.cpp:187
clang::clangd::LSPClient::transport
Transport & transport()
Definition: LSPClient.cpp:162
clang::clangd::LSPClient::documentID
static llvm::json::Value documentID(llvm::StringRef Path)
Definition: LSPClient.cpp:172
clang::clangd::LSPClient::sync
void sync()
Definition: LSPClient.cpp:191
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::LSPClient::CallResult::take
llvm::Expected< llvm::json::Value > take()
Definition: LSPClient.cpp:16
clang::clangd::LSPClient::stop
void stop()
Definition: LSPClient.cpp:160
clang::clangd::LSPClient::uri
static llvm::json::Value uri(llvm::StringRef Path)
Definition: LSPClient.cpp:166
Value
static constexpr bool Value
Definition: SuspiciousCallArgumentCheck.cpp:72
clang::clangd::LSPClient
Definition: LSPClient.h:23