| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 1 | //===--- ClangDMain.cpp - clangd server loop ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "DocumentStore.h" |
| 11 | #include "JSONRPCDispatcher.h" |
| 12 | #include "ProtocolHandlers.h" |
| 13 | #include "llvm/Support/FileSystem.h" |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame^] | 14 | #include "llvm/Support/Program.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 15 | #include <iostream> |
| 16 | #include <string> |
| 17 | using namespace clang::clangd; |
| 18 | |
| 19 | int main(int argc, char *argv[]) { |
| 20 | llvm::raw_ostream &Outs = llvm::outs(); |
| 21 | llvm::raw_ostream &Logs = llvm::errs(); |
| 22 | |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame^] | 23 | // Change stdin to binary to not lose \r\n on windows. |
| 24 | llvm::sys::ChangeStdinToBinary(); |
| 25 | |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 26 | // Set up a document store and intialize all the method handlers for JSONRPC |
| 27 | // dispatching. |
| 28 | DocumentStore Store; |
| 29 | JSONRPCDispatcher Dispatcher(llvm::make_unique<Handler>(Outs, Logs)); |
| 30 | Dispatcher.registerHandler("initialize", |
| 31 | llvm::make_unique<InitializeHandler>(Outs, Logs)); |
| 32 | Dispatcher.registerHandler("shutdown", |
| 33 | llvm::make_unique<ShutdownHandler>(Outs, Logs)); |
| 34 | Dispatcher.registerHandler( |
| 35 | "textDocument/didOpen", |
| 36 | llvm::make_unique<TextDocumentDidOpenHandler>(Outs, Logs, Store)); |
| 37 | // FIXME: Implement textDocument/didClose. |
| 38 | Dispatcher.registerHandler( |
| 39 | "textDocument/didChange", |
| 40 | llvm::make_unique<TextDocumentDidChangeHandler>(Outs, Logs, Store)); |
| 41 | Dispatcher.registerHandler( |
| 42 | "textDocument/rangeFormatting", |
| 43 | llvm::make_unique<TextDocumentRangeFormattingHandler>(Outs, Logs, Store)); |
| 44 | Dispatcher.registerHandler( |
| 45 | "textDocument/formatting", |
| 46 | llvm::make_unique<TextDocumentFormattingHandler>(Outs, Logs, Store)); |
| 47 | |
| 48 | while (std::cin.good()) { |
| 49 | // A Language Server Protocol message starts with a HTTP header, delimited |
| 50 | // by \r\n. |
| 51 | std::string Line; |
| 52 | std::getline(std::cin, Line); |
| 53 | |
| 54 | // Skip empty lines. |
| 55 | llvm::StringRef LineRef(Line); |
| 56 | if (LineRef.trim().empty()) |
| 57 | continue; |
| 58 | |
| 59 | unsigned long long Len = 0; |
| 60 | // FIXME: Content-Type is a specified header, but does nothing. |
| 61 | // Content-Length is a mandatory header. It specifies the length of the |
| 62 | // following JSON. |
| 63 | if (LineRef.consume_front("Content-Length: ")) |
| 64 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, Len); |
| 65 | |
| 66 | // Check if the next line only contains \r\n. If not this is another header, |
| 67 | // which we ignore. |
| 68 | char NewlineBuf[2]; |
| 69 | std::cin.read(NewlineBuf, 2); |
| 70 | if (std::memcmp(NewlineBuf, "\r\n", 2) != 0) |
| 71 | continue; |
| 72 | |
| 73 | // Now read the JSON. Insert a trailing null byte as required by the YAML |
| 74 | // parser. |
| Benjamin Kramer | d588b0f | 2017-02-07 11:49:03 +0000 | [diff] [blame] | 75 | std::vector<char> JSON(Len + 1, '\0'); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 76 | std::cin.read(JSON.data(), Len); |
| 77 | |
| 78 | if (Len > 0) { |
| Benjamin Kramer | d588b0f | 2017-02-07 11:49:03 +0000 | [diff] [blame] | 79 | llvm::StringRef JSONRef(JSON.data(), Len); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 80 | // Log the message. |
| Benjamin Kramer | d588b0f | 2017-02-07 11:49:03 +0000 | [diff] [blame] | 81 | Logs << "<-- " << JSONRef << '\n'; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 82 | Logs.flush(); |
| 83 | |
| 84 | // Finally, execute the action for this JSON message. |
| Benjamin Kramer | d588b0f | 2017-02-07 11:49:03 +0000 | [diff] [blame] | 85 | if (!Dispatcher.call(JSONRef)) |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 86 | Logs << "JSON dispatch failed!\n"; |
| 87 | } |
| 88 | } |
| 89 | } |