[ebe0f0d] | 1 | #include <cstdio> |
---|
| 2 | |
---|
[b52abe0] | 3 | #include <iostream> |
---|
| 4 | #include <string> |
---|
| 5 | #include <vector> |
---|
| 6 | |
---|
[ebe0f0d] | 7 | #include <unistd.h> |
---|
| 8 | |
---|
[b52abe0] | 9 | #include "json.hpp" |
---|
| 10 | #include "log.hpp" |
---|
[ebe0f0d] | 11 | #include "server.hpp" |
---|
[b52abe0] | 12 | |
---|
| 13 | static void request(std::vector<char> & buf); |
---|
[ebe0f0d] | 14 | static void reply(const json::obj & ); |
---|
[b52abe0] | 15 | |
---|
[ebe0f0d] | 16 | json::obj noop( const json::obj & ) { |
---|
| 17 | return {}; |
---|
[b52abe0] | 18 | } |
---|
| 19 | |
---|
| 20 | std::unordered_map<std::string, json::obj (*)( const json::obj & )> actions { |
---|
[ebe0f0d] | 21 | { "initialize", server::init }, |
---|
| 22 | { "initialized", noop } |
---|
[b52abe0] | 23 | }; |
---|
| 24 | |
---|
| 25 | int main(int argc, char * argv[]) { |
---|
| 26 | g_argc = argc; |
---|
| 27 | g_argv = argv; |
---|
| 28 | |
---|
| 29 | log() << "Language Server called with args:" << std::endl; |
---|
| 30 | for(int i = 0; i < argc; i++) { |
---|
| 31 | log() << "\t- " << argv[i] << std::endl; |
---|
| 32 | } |
---|
| 33 | |
---|
[ebe0f0d] | 34 | // std::cout.setf(std::ios::unitbuf); |
---|
| 35 | |
---|
| 36 | // char buff[1000]; |
---|
| 37 | // int r = 0; |
---|
| 38 | // while(0 != (r = read(STDIN_FILENO, buff, 1000))) { |
---|
| 39 | // for(int i = 0; i < r; i++) { |
---|
| 40 | // log() << buff[i]; |
---|
| 41 | // } |
---|
| 42 | // } |
---|
| 43 | // log() << std::endl; |
---|
| 44 | |
---|
| 45 | // return 0; |
---|
| 46 | |
---|
[b52abe0] | 47 | std::vector<char> buffer; |
---|
| 48 | while(true) { |
---|
| 49 | request(buffer); |
---|
| 50 | if(buffer.size() == 0) break; |
---|
| 51 | |
---|
| 52 | auto top = json::parse(buffer); |
---|
| 53 | |
---|
| 54 | auto action = actions.find( top["method"] ); |
---|
| 55 | if(action != actions.end()) { |
---|
| 56 | log() << "Executing request '" << action->first << "'" << std::endl; |
---|
| 57 | json::obj response = { |
---|
| 58 | {"id", top["id"] } |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | try{ |
---|
| 62 | response["result"] = action->second( top["params"] ); |
---|
| 63 | } catch( const json::obj & error ) { |
---|
| 64 | response["result"] = ""; |
---|
| 65 | response["error"] = error; |
---|
| 66 | } |
---|
[ebe0f0d] | 67 | |
---|
| 68 | reply(response); |
---|
[b52abe0] | 69 | } |
---|
| 70 | else { |
---|
| 71 | log() << "WARNING no action for method '" << top["method"].dump(4) << "'" << std::endl; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | log() << "Language Server closing after end of file" << std::endl; |
---|
| 76 | |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static void request(std::vector<char> & buffer) { |
---|
| 81 | std::string lenstr; |
---|
| 82 | std::getline( std::cin, lenstr ); |
---|
| 83 | if(std::cin.eof()) { buffer.clear(); return; }; |
---|
| 84 | |
---|
| 85 | if (lenstr.rfind("Content-Length: ", 0) != 0) { |
---|
| 86 | log() << "Expected content length bug got '" << lenstr << "' instead " << std::endl; |
---|
| 87 | std::exit(EXIT_FAILURE); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | size_t idx = sizeof("Content-Length:"); |
---|
| 91 | size_t end; |
---|
| 92 | size_t len = std::stoul(lenstr.substr(idx), &end); |
---|
| 93 | if(lenstr.length() != (idx + end + 1)) { |
---|
| 94 | log() << "Inconsistent size of integer in content length: " << lenstr.length() << " vs " << (idx + end) << std::endl; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | log() << "New request of " << len << " bytes" << std::endl; |
---|
| 98 | buffer.resize( len + 2 ); |
---|
| 99 | std::cin.read(buffer.data(), buffer.size()); |
---|
[ebe0f0d] | 100 | |
---|
| 101 | for(char c : buffer) { |
---|
| 102 | log() << c; |
---|
| 103 | } |
---|
| 104 | log() << std::endl; |
---|
[b52abe0] | 105 | return; |
---|
[ebe0f0d] | 106 | } |
---|
| 107 | |
---|
| 108 | static void reply( const json::obj & response) { |
---|
| 109 | if(response["result"].is_null()) { |
---|
| 110 | log() << "No response needed" << std::endl; |
---|
| 111 | return; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | log() << "Responding with : " << response.dump(4) << std::endl; |
---|
| 115 | const std::string & r = response.dump(); |
---|
| 116 | std::cout << "Content-Length: " << r.length() << "\r\n\r\n"; |
---|
| 117 | std::cout.flush(); |
---|
| 118 | std::cout << r << "\r\n"; |
---|
| 119 | std::cout.flush(); |
---|
[b52abe0] | 120 | } |
---|