blob: eee0fb5aee350d1d6607eda4e33d06cd2c17951e [file] [log] [blame]
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00001//===--- 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 Kramer6a3d74e2017-02-07 12:40:59 +000014#include "llvm/Support/Program.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000015#include <iostream>
16#include <string>
17using namespace clang::clangd;
18
19int main(int argc, char *argv[]) {
20 llvm::raw_ostream &Outs = llvm::outs();
21 llvm::raw_ostream &Logs = llvm::errs();
22
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000023 // Change stdin to binary to not lose \r\n on windows.
24 llvm::sys::ChangeStdinToBinary();
25
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000026 // 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 Kramerd588b0f2017-02-07 11:49:03 +000075 std::vector<char> JSON(Len + 1, '\0');
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000076 std::cin.read(JSON.data(), Len);
77
78 if (Len > 0) {
Benjamin Kramerd588b0f2017-02-07 11:49:03 +000079 llvm::StringRef JSONRef(JSON.data(), Len);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000080 // Log the message.
Benjamin Kramerd588b0f2017-02-07 11:49:03 +000081 Logs << "<-- " << JSONRef << '\n';
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000082 Logs.flush();
83
84 // Finally, execute the action for this JSON message.
Benjamin Kramerd588b0f2017-02-07 11:49:03 +000085 if (!Dispatcher.call(JSONRef))
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000086 Logs << "JSON dispatch failed!\n";
87 }
88 }
89}