| 1 |
|
| 2 |
#include "LibLsp/lsp/ProcessIoService.h" |
| 3 |
|
| 4 |
#include <boost/program_options.hpp> |
| 5 |
#include "LibLsp/lsp/general/exit.h" |
| 6 |
#include "LibLsp/lsp/textDocument/declaration_definition.h" |
| 7 |
|
| 8 |
#include "LibLsp/lsp/textDocument/signature_help.h" |
| 9 |
#include "LibLsp/lsp/general/initialize.h" |
| 10 |
#include "LibLsp/lsp/ProtocolJsonHandler.h" |
| 11 |
#include "LibLsp/lsp/textDocument/typeHierarchy.h" |
| 12 |
#include "LibLsp/lsp/AbsolutePath.h" |
| 13 |
#include "LibLsp/lsp/textDocument/resolveCompletionItem.h" |
| 14 |
#include <network/uri.hpp> |
| 15 |
#include "LibLsp/JsonRpc/Endpoint.h" |
| 16 |
#include "LibLsp/JsonRpc/stream.h" |
| 17 |
#include "LibLsp/JsonRpc/TcpServer.h" |
| 18 |
#include "LibLsp/lsp/textDocument/document_symbol.h" |
| 19 |
#include "LibLsp/lsp/workspace/execute_command.h" |
| 20 |
#include <boost/process.hpp> |
| 21 |
#include <boost/filesystem.hpp> |
| 22 |
#include <boost/asio.hpp> |
| 23 |
#include <iostream> |
| 24 |
|
| 25 |
#include <thread> |
| 26 |
|
| 27 |
using namespace boost::asio::ip; |
| 28 |
using namespace std; |
| 29 |
class DummyLog :public lsp::Log |
| 30 |
{ |
| 31 |
public: |
| 32 |
|
| 33 |
void log(Level level, std::wstring&& msg) |
| 34 |
{ |
| 35 |
|
| 36 |
std::wcerr << msg << std::endl; |
| 37 |
}; |
| 38 |
void log(Level level, const std::wstring& msg) |
| 39 |
{ |
| 40 |
std::wcerr << msg << std::endl; |
| 41 |
}; |
| 42 |
void log(Level level, std::string&& msg) |
| 43 |
{ |
| 44 |
std::cerr << msg << std::endl; |
| 45 |
}; |
| 46 |
void log(Level level, const std::string& msg) |
| 47 |
{ |
| 48 |
std::cerr << msg << std::endl; |
| 49 |
}; |
| 50 |
}; |
| 51 |
|
| 52 |
struct boost_process_ipstream : lsp::base_istream< boost::process::ipstream > |
| 53 |
{ |
| 54 |
explicit boost_process_ipstream(boost::process::ipstream& _t) |
| 55 |
: base_istream<boost::process::ipstream>(_t) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
std::string what() override |
| 60 |
{ |
| 61 |
return {}; |
| 62 |
} |
| 63 |
void clear() override |
| 64 |
{ |
| 65 |
|
| 66 |
} |
| 67 |
}; |
| 68 |
struct boost_process_opstream : lsp::base_ostream< boost::process::opstream > |
| 69 |
{ |
| 70 |
explicit boost_process_opstream(boost::process::opstream& _t) |
| 71 |
: lsp::base_ostream<boost::process::opstream>(_t) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
std::string what() override |
| 76 |
{ |
| 77 |
return {}; |
| 78 |
} |
| 79 |
void clear() override |
| 80 |
{ |
| 81 |
|
| 82 |
} |
| 83 |
}; |
| 84 |
class Client |
| 85 |
{ |
| 86 |
public: |
| 87 |
Client(std::string& execPath,const std::vector<std::string>& args) |
| 88 |
:point(protocol_json_handler, endpoint, _log) |
| 89 |
{ |
| 90 |
std::error_code ec; |
| 91 |
namespace bp = boost::process; |
| 92 |
c = std::make_shared<bp::child>(asio_io.getIOService(), execPath, |
| 93 |
bp::args = args, |
| 94 |
ec, |
| 95 |
|
| 96 |
bp::std_out > read_from_service, |
| 97 |
bp::std_in < write_to_service, |
| 98 |
bp::on_exit([&](int exit_code, const std::error_code& ec_in){ |
| 99 |
|
| 100 |
})); |
| 101 |
if (ec) |
| 102 |
{ |
| 103 |
// fail |
| 104 |
_log.log(lsp::Log::Level::SEVERE, "Start server failed."); |
| 105 |
} |
| 106 |
else { |
| 107 |
//success |
| 108 |
point.startProcessingMessages(read_from_service_proxy, write_to_service_proxy); |
| 109 |
} |
| 110 |
} |
| 111 |
~Client() |
| 112 |
{ |
| 113 |
point.stop(); |
| 114 |
std::this_thread::sleep_for(std::chrono::milliseconds (1000)); |
| 115 |
} |
| 116 |
|
| 117 |
lsp::ProcessIoService asio_io; |
| 118 |
std::shared_ptr < lsp::ProtocolJsonHandler > protocol_json_handler = std::make_shared< lsp::ProtocolJsonHandler>(); |
| 119 |
DummyLog _log; |
| 120 |
|
| 121 |
std::shared_ptr<GenericEndpoint> endpoint = std::make_shared<GenericEndpoint>(_log); |
| 122 |
|
| 123 |
boost::process::opstream write_to_service; |
| 124 |
boost::process::ipstream read_from_service; |
| 125 |
|
| 126 |
std::shared_ptr<lsp::ostream> write_to_service_proxy = std::make_shared<boost_process_opstream>(write_to_service); |
| 127 |
std::shared_ptr<lsp::istream> read_from_service_proxy = std::make_shared< boost_process_ipstream >(read_from_service); |
| 128 |
|
| 129 |
std::shared_ptr<boost::process::child> c; |
| 130 |
RemoteEndPoint point; |
| 131 |
}; |
| 132 |
|
| 133 |
int main(int argc, char* argv[]) |
| 134 |
{ |
| 135 |
using namespace boost::program_options; |
| 136 |
options_description desc("Allowed options"); |
| 137 |
desc.add_options() |
| 138 |
("help,h", "produce help message") |
| 139 |
("execPath", value<string>(), "LSP server's path"); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
variables_map vm; |
| 144 |
try { |
| 145 |
store(parse_command_line(argc, argv, desc), vm); |
| 146 |
} |
| 147 |
catch (std::exception& e) { |
| 148 |
std::cout << "Undefined input.Reason:" << e.what() << std::endl; |
| 149 |
return 0; |
| 150 |
} |
| 151 |
notify(vm); |
| 152 |
|
| 153 |
|
| 154 |
if (vm.count("help")) |
| 155 |
{ |
| 156 |
cout << desc << endl; |
| 157 |
return 1; |
| 158 |
} |
| 159 |
string execPath; |
| 160 |
if (vm.count("execPath")) |
| 161 |
{ |
| 162 |
execPath = vm["execPath"].as<string>(); |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
execPath = "StdIOServerExample"; |
| 167 |
} |
| 168 |
|
| 169 |
Client client(execPath, {}); |
| 170 |
{ |
| 171 |
td_initialize::request req; |
| 172 |
auto rsp = client.point.waitResponse(req); |
| 173 |
if (rsp) |
| 174 |
{ |
| 175 |
std::cerr << rsp->ToJson() << std::endl; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
std::cerr << "get initialze response time out" << std::endl; |
| 180 |
} |
| 181 |
} |
| 182 |
{ |
| 183 |
td_definition::request req; |
| 184 |
auto future_rsp = client.point.send(req); |
| 185 |
auto state = future_rsp.wait_for(std::chrono::seconds(4)); |
| 186 |
if (lsp::future_status::timeout == state) |
| 187 |
{ |
| 188 |
std::cerr << "get textDocument/definition response time out" << std::endl; |
| 189 |
return 0; |
| 190 |
} |
| 191 |
auto rsp = future_rsp.get(); |
| 192 |
if (rsp.is_error) |
| 193 |
{ |
| 194 |
std::cerr << "get textDocument/definition response error" << std::endl; |
| 195 |
|
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
std::cerr << rsp.response.ToJson() << std::endl; |
| 200 |
} |
| 201 |
} |
| 202 |
{ |
| 203 |
td_initialize::request req; |
| 204 |
auto rsp = client.point.waitResponse(req); |
| 205 |
if (rsp) |
| 206 |
{ |
| 207 |
std::cerr << rsp->ToJson() << std::endl; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
std::cerr << "get initialze response time out" << std::endl; |
| 212 |
} |
| 213 |
} |
| 214 |
Notify_Exit::notify notify; |
| 215 |
client.point.send(notify); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
|