| 1 |
|
| 2 |
#include "LibLsp/lsp/general/exit.h" |
| 3 |
#include "LibLsp/lsp/textDocument/declaration_definition.h" |
| 4 |
#include "LibLsp/lsp/textDocument/signature_help.h" |
| 5 |
#include "LibLsp/lsp/general/initialize.h" |
| 6 |
#include "LibLsp/lsp/ProtocolJsonHandler.h" |
| 7 |
#include "LibLsp/lsp/textDocument/typeHierarchy.h" |
| 8 |
#include "LibLsp/lsp/AbsolutePath.h" |
| 9 |
#include "LibLsp/lsp/textDocument/resolveCompletionItem.h" |
| 10 |
#include <network/uri.hpp> |
| 11 |
|
| 12 |
|
| 13 |
#include "LibLsp/JsonRpc/Endpoint.h" |
| 14 |
#include "LibLsp/JsonRpc/stream.h" |
| 15 |
#include "LibLsp/JsonRpc/TcpServer.h" |
| 16 |
#include "LibLsp/lsp/textDocument/document_symbol.h" |
| 17 |
#include "LibLsp/lsp/workspace/execute_command.h" |
| 18 |
|
| 19 |
#include <boost/filesystem.hpp> |
| 20 |
#include <boost/asio.hpp> |
| 21 |
#include <iostream> |
| 22 |
#include <thread> |
| 23 |
|
| 24 |
using namespace boost::asio::ip; |
| 25 |
using namespace std; |
| 26 |
class DummyLog :public lsp::Log |
| 27 |
{ |
| 28 |
public: |
| 29 |
|
| 30 |
void log(Level level, std::wstring&& msg) |
| 31 |
{ |
| 32 |
std::wcout << msg << std::endl; |
| 33 |
}; |
| 34 |
void log(Level level, const std::wstring& msg) |
| 35 |
{ |
| 36 |
std::wcout << msg << std::endl; |
| 37 |
}; |
| 38 |
void log(Level level, std::string&& msg) |
| 39 |
{ |
| 40 |
std::cout << msg << std::endl; |
| 41 |
}; |
| 42 |
void log(Level level, const std::string& msg) |
| 43 |
{ |
| 44 |
std::cout << msg << std::endl; |
| 45 |
}; |
| 46 |
}; |
| 47 |
|
| 48 |
std::string _address = "127.0.0.1"; |
| 49 |
std::string _port = "9333"; |
| 50 |
|
| 51 |
class Server |
| 52 |
{ |
| 53 |
public: |
| 54 |
|
| 55 |
|
| 56 |
Server():server(_address,_port,protocol_json_handler, endpoint, _log) |
| 57 |
{ |
| 58 |
server.point.registerHandler( |
| 59 |
[&](const td_initialize::request& req) |
| 60 |
->lsp::ResponseOrError< td_initialize::response >{ |
| 61 |
|
| 62 |
td_initialize::response rsp; |
| 63 |
CodeLensOptions code_lens_options; |
| 64 |
code_lens_options.resolveProvider = true; |
| 65 |
rsp.result.capabilities.codeLensProvider = code_lens_options; |
| 66 |
|
| 67 |
return rsp; |
| 68 |
}); |
| 69 |
server.point.registerHandler([&](const td_definition::request& req |
| 70 |
,const CancelMonitor& monitor) -> lsp::ResponseOrError<td_definition::response> |
| 71 |
{ |
| 72 |
|
| 73 |
std::this_thread::sleep_for(std::chrono::seconds(8)); |
| 74 |
|
| 75 |
if( monitor && monitor() ) |
| 76 |
{ |
| 77 |
_log.info("textDocument/definition request had been cancel."); |
| 78 |
Rsp_Error rsp; |
| 79 |
rsp.error.code = lsErrorCodes::RequestCancelled; |
| 80 |
rsp.error.message = "textDocument/definition request had been cancel."; |
| 81 |
return rsp; |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
td_definition::response rsp; |
| 86 |
rsp.result.first= std::vector<lsLocation>(); |
| 87 |
return rsp; |
| 88 |
} |
| 89 |
|
| 90 |
}); |
| 91 |
|
| 92 |
server.point.registerHandler([=](Notify_Exit::notify& notify) |
| 93 |
{ |
| 94 |
std::cout << notify.ToJson() << std::endl; |
| 95 |
}); |
| 96 |
std::thread([&]() |
| 97 |
{ |
| 98 |
server.run(); |
| 99 |
}).detach(); |
| 100 |
} |
| 101 |
~Server() |
| 102 |
{ |
| 103 |
server.stop(); |
| 104 |
} |
| 105 |
std::shared_ptr < lsp::ProtocolJsonHandler > protocol_json_handler = std::make_shared < lsp::ProtocolJsonHandler >(); |
| 106 |
DummyLog _log; |
| 107 |
|
| 108 |
std::shared_ptr < GenericEndpoint > endpoint = std::make_shared<GenericEndpoint>(_log); |
| 109 |
lsp::TcpServer server; |
| 110 |
|
| 111 |
}; |
| 112 |
|
| 113 |
class Client |
| 114 |
{ |
| 115 |
public: |
| 116 |
struct iostream :public lsp::base_iostream<tcp::iostream> |
| 117 |
{ |
| 118 |
explicit iostream(boost::asio::basic_socket_iostream<tcp>& _t) |
| 119 |
: base_iostream<boost::asio::basic_socket_iostream<tcp>>(_t) |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
std::string what() override |
| 124 |
{ |
| 125 |
auto msg = _impl.error().message(); |
| 126 |
return msg; |
| 127 |
} |
| 128 |
|
| 129 |
}; |
| 130 |
Client() :remote_end_point_(protocol_json_handler, endpoint, _log) |
| 131 |
{ |
| 132 |
tcp::endpoint end_point( make_address(_address), 9333); |
| 133 |
|
| 134 |
socket_ = std::make_shared<tcp::iostream>(); |
| 135 |
socket_->connect(end_point); |
| 136 |
if (!socket_) |
| 137 |
{ |
| 138 |
string temp = "Unable to connect: " + socket_->error().message(); |
| 139 |
std::cout << temp << std::endl; |
| 140 |
} |
| 141 |
|
| 142 |
vector<string> args; |
| 143 |
socket_proxy = std::make_shared<iostream>(*socket_.get()); |
| 144 |
|
| 145 |
remote_end_point_.startProcessingMessages(socket_proxy, socket_proxy); |
| 146 |
} |
| 147 |
~Client() |
| 148 |
{ |
| 149 |
remote_end_point_.stop(); |
| 150 |
std::this_thread::sleep_for(std::chrono::milliseconds (1000)); |
| 151 |
socket_->close(); |
| 152 |
} |
| 153 |
|
| 154 |
std::shared_ptr < lsp::ProtocolJsonHandler > protocol_json_handler = std::make_shared< lsp::ProtocolJsonHandler>(); |
| 155 |
DummyLog _log; |
| 156 |
|
| 157 |
std::shared_ptr<GenericEndpoint> endpoint = std::make_shared<GenericEndpoint>(_log); |
| 158 |
|
| 159 |
std::shared_ptr < iostream> socket_proxy; |
| 160 |
std::shared_ptr<tcp::iostream> socket_; |
| 161 |
RemoteEndPoint remote_end_point_; |
| 162 |
}; |
| 163 |
|
| 164 |
int main() |
| 165 |
{ |
| 166 |
|
| 167 |
Server server; |
| 168 |
Client client; |
| 169 |
|
| 170 |
{ |
| 171 |
td_initialize::request req; |
| 172 |
auto rsp = client.remote_end_point_.waitResponse(req); |
| 173 |
if (rsp) |
| 174 |
{ |
| 175 |
std::cout << rsp->response.ToJson() << std::endl; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
std::cout << "get initialze response time out" << std::endl; |
| 180 |
} |
| 181 |
} |
| 182 |
{ |
| 183 |
td_definition::request req; |
| 184 |
auto future_rsp = client.remote_end_point_.send(req); |
| 185 |
client.remote_end_point_.cancelRequest(req.id); |
| 186 |
|
| 187 |
auto state = future_rsp.wait_for(std::chrono::seconds(16)); |
| 188 |
if (lsp::future_status::timeout == state) |
| 189 |
{ |
| 190 |
std::cout << "get textDocument/definition response time out" << std::endl; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
auto rsp = future_rsp.get(); |
| 194 |
if (rsp.is_error) |
| 195 |
{ |
| 196 |
std::cout << "get textDocument/definition response error :" << rsp.ToJson() << std::endl; |
| 197 |
|
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
std::cout << rsp.response.ToJson() << std::endl; |
| 202 |
} |
| 203 |
} |
| 204 |
Notify_Exit::notify notify; |
| 205 |
client.remote_end_point_.send(notify); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|