2017-03-25 22:13:19 +00:00
|
|
|
#include "language_server_api.h"
|
|
|
|
|
2018-02-07 04:27:30 +00:00
|
|
|
#include "recorder.h"
|
2018-01-06 21:46:41 +00:00
|
|
|
#include "serializers/json.h"
|
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
#include <doctest/doctest.h>
|
|
|
|
#include <loguru.hpp>
|
2018-02-22 21:50:46 +00:00
|
|
|
#include <rapidjson/writer.h>
|
2017-09-13 03:35:27 +00:00
|
|
|
|
2018-01-25 05:32:05 +00:00
|
|
|
#include <stdio.h>
|
2018-01-10 08:15:58 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
MessageRegistry* MessageRegistry::instance_ = nullptr;
|
|
|
|
|
2017-09-22 02:25:33 +00:00
|
|
|
lsTextDocumentIdentifier
|
|
|
|
lsVersionedTextDocumentIdentifier::AsTextDocumentIdentifier() const {
|
|
|
|
lsTextDocumentIdentifier result;
|
|
|
|
result.uri = uri;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
// Reads a JsonRpc message. |read| returns the next input character.
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<std::string> ReadJsonRpcContentFrom(
|
|
|
|
std::function<optional<char>()> read) {
|
2017-09-13 03:35:27 +00:00
|
|
|
// Read the content length. It is terminated by the "\r\n" sequence.
|
|
|
|
int exit_seq = 0;
|
|
|
|
std::string stringified_content_length;
|
2017-03-25 22:13:19 +00:00
|
|
|
while (true) {
|
2017-09-13 03:35:27 +00:00
|
|
|
optional<char> opt_c = read();
|
|
|
|
if (!opt_c) {
|
|
|
|
LOG_S(INFO) << "No more input when reading content length header";
|
|
|
|
return nullopt;
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
2017-09-13 03:35:27 +00:00
|
|
|
char c = *opt_c;
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
if (exit_seq == 0 && c == '\r')
|
|
|
|
++exit_seq;
|
|
|
|
if (exit_seq == 1 && c == '\n')
|
|
|
|
break;
|
2017-09-13 17:53:13 +00:00
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
stringified_content_length += c;
|
|
|
|
}
|
2017-09-13 17:53:13 +00:00
|
|
|
const char* kContentLengthStart = "Content-Length: ";
|
2017-09-13 03:35:27 +00:00
|
|
|
assert(StartsWith(stringified_content_length, kContentLengthStart));
|
2017-09-22 01:14:57 +00:00
|
|
|
int content_length =
|
|
|
|
atoi(stringified_content_length.c_str() + strlen(kContentLengthStart));
|
2017-09-13 03:35:27 +00:00
|
|
|
|
|
|
|
// There is always a "\r\n" sequence before the actual content.
|
|
|
|
auto expect_char = [&](char expected) {
|
|
|
|
optional<char> opt_c = read();
|
|
|
|
return opt_c && *opt_c == expected;
|
|
|
|
};
|
|
|
|
if (!expect_char('\r') || !expect_char('\n')) {
|
|
|
|
LOG_S(INFO) << "Unexpected token (expected \r\n sequence)";
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-05-03 06:45:10 +00:00
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
// Read content.
|
|
|
|
std::string content;
|
|
|
|
content.reserve(content_length);
|
2018-01-31 05:55:53 +00:00
|
|
|
for (int i = 0; i < content_length; ++i) {
|
2017-09-13 03:35:27 +00:00
|
|
|
optional<char> c = read();
|
|
|
|
if (!c) {
|
|
|
|
LOG_S(INFO) << "No more input when reading content body";
|
|
|
|
return nullopt;
|
2017-05-19 01:13:51 +00:00
|
|
|
}
|
2017-09-13 03:35:27 +00:00
|
|
|
content += *c;
|
|
|
|
}
|
2017-09-13 17:53:13 +00:00
|
|
|
|
2018-02-07 04:27:30 +00:00
|
|
|
RecordInput(content);
|
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
return content;
|
|
|
|
}
|
2017-05-03 06:45:10 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::function<optional<char>()> MakeContentReader(std::string* content,
|
|
|
|
bool can_be_empty) {
|
2017-09-13 17:53:13 +00:00
|
|
|
return [content, can_be_empty]() -> optional<char> {
|
2017-09-13 03:35:27 +00:00
|
|
|
if (!can_be_empty)
|
|
|
|
REQUIRE(!content->empty());
|
|
|
|
if (content->empty())
|
|
|
|
return nullopt;
|
|
|
|
char c = (*content)[0];
|
|
|
|
content->erase(content->begin());
|
|
|
|
return c;
|
|
|
|
};
|
|
|
|
}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_SUITE("FindIncludeLine") {
|
|
|
|
TEST_CASE("ReadContentFromSource") {
|
|
|
|
auto parse_correct = [](std::string content) -> std::string {
|
|
|
|
auto reader = MakeContentReader(&content, false /*can_be_empty*/);
|
|
|
|
auto got = ReadJsonRpcContentFrom(reader);
|
|
|
|
REQUIRE(got);
|
|
|
|
return got.value();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto parse_incorrect = [](std::string content) -> optional<std::string> {
|
|
|
|
auto reader = MakeContentReader(&content, true /*can_be_empty*/);
|
|
|
|
return ReadJsonRpcContentFrom(reader);
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(parse_correct("Content-Length: 0\r\n\r\n") == "");
|
|
|
|
REQUIRE(parse_correct("Content-Length: 1\r\n\r\na") == "a");
|
|
|
|
REQUIRE(parse_correct("Content-Length: 4\r\n\r\nabcd") == "abcd");
|
|
|
|
|
|
|
|
REQUIRE(parse_incorrect("ggg") == optional<std::string>());
|
2017-11-19 22:11:54 +00:00
|
|
|
REQUIRE(parse_incorrect("Content-Length: 0\r\n") ==
|
|
|
|
optional<std::string>());
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") ==
|
|
|
|
optional<std::string>());
|
|
|
|
}
|
2017-09-13 03:35:27 +00:00
|
|
|
}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2017-09-13 03:35:27 +00:00
|
|
|
optional<char> ReadCharFromStdinBlocking() {
|
2018-01-25 07:58:16 +00:00
|
|
|
// We do not use std::cin because it does not read bytes once stuck in
|
2018-01-30 00:27:43 +00:00
|
|
|
// cin.bad(). We can call cin.clear() but C++ iostream has other annoyance
|
|
|
|
// like std::{cin,cout} is tied by default, which causes undesired cout flush
|
|
|
|
// for cin operations.
|
2018-01-25 05:32:05 +00:00
|
|
|
int c = getchar();
|
|
|
|
if (c >= 0)
|
2018-01-15 08:00:29 +00:00
|
|
|
return c;
|
|
|
|
return nullopt;
|
2017-09-13 03:35:27 +00:00
|
|
|
}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2018-01-19 22:31:49 +00:00
|
|
|
optional<std::string> MessageRegistry::ReadMessageFromStdin(
|
|
|
|
std::unique_ptr<BaseIpcMessage>* message) {
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<std::string> content =
|
|
|
|
ReadJsonRpcContentFrom(&ReadCharFromStdinBlocking);
|
2017-09-13 03:35:27 +00:00
|
|
|
if (!content) {
|
2018-02-02 07:10:37 +00:00
|
|
|
LOG_S(ERROR) << "Failed to read JsonRpc input; exiting";
|
2017-09-13 03:35:27 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-04-16 23:52:42 +00:00
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
rapidjson::Document document;
|
2017-09-13 03:35:27 +00:00
|
|
|
document.Parse(content->c_str(), content->length());
|
2017-03-25 22:13:19 +00:00
|
|
|
assert(!document.HasParseError());
|
|
|
|
|
2018-01-06 21:46:41 +00:00
|
|
|
JsonReader json_reader{&document};
|
2018-01-19 22:31:49 +00:00
|
|
|
return Parse(json_reader, message);
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:31:49 +00:00
|
|
|
optional<std::string> MessageRegistry::Parse(
|
|
|
|
Reader& visitor,
|
|
|
|
std::unique_ptr<BaseIpcMessage>* message) {
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!visitor.HasMember("jsonrpc") ||
|
2018-01-06 21:46:41 +00:00
|
|
|
std::string(visitor["jsonrpc"]->GetString()) != "2.0") {
|
2017-12-02 01:04:39 +00:00
|
|
|
LOG_S(FATAL) << "Bad or missing jsonrpc version";
|
2017-03-25 22:13:19 +00:00
|
|
|
exit(1);
|
2017-05-03 06:45:10 +00:00
|
|
|
}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
|
|
|
std::string method;
|
|
|
|
ReflectMember(visitor, "method", method);
|
|
|
|
|
2018-01-19 08:14:47 +00:00
|
|
|
if (allocators.find(method) == allocators.end())
|
|
|
|
return std::string("Unable to find registered handler for method '") +
|
|
|
|
method + "'";
|
2017-03-25 22:13:19 +00:00
|
|
|
|
|
|
|
Allocator& allocator = allocators[method];
|
2018-01-19 06:47:44 +00:00
|
|
|
try {
|
2018-01-19 22:31:49 +00:00
|
|
|
allocator(visitor, message);
|
|
|
|
return nullopt;
|
2018-01-19 06:47:44 +00:00
|
|
|
} catch (std::invalid_argument& e) {
|
2018-01-19 22:31:49 +00:00
|
|
|
// *message is partially deserialized but some field (e.g. |id|) are likely
|
|
|
|
// available.
|
2018-01-19 23:33:32 +00:00
|
|
|
return std::string("Fail to parse '") + method + "' " +
|
2018-01-30 00:27:43 +00:00
|
|
|
static_cast<JsonReader&>(visitor).GetPath() + ", expected " +
|
|
|
|
e.what();
|
2018-01-19 06:47:44 +00:00
|
|
|
}
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageRegistry* MessageRegistry::instance() {
|
|
|
|
if (!instance_)
|
|
|
|
instance_ = new MessageRegistry();
|
|
|
|
|
|
|
|
return instance_;
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:20:29 +00:00
|
|
|
lsBaseOutMessage::~lsBaseOutMessage() = default;
|
|
|
|
|
2018-02-22 21:50:46 +00:00
|
|
|
void lsBaseOutMessage::Write(std::ostream& out) {
|
|
|
|
rapidjson::StringBuffer output;
|
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(output);
|
|
|
|
JsonWriter json_writer{&writer};
|
|
|
|
ReflectWriter(json_writer);
|
|
|
|
|
|
|
|
out << "Content-Length: " << output.GetSize() << "\r\n\r\n"
|
|
|
|
<< output.GetString();
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
void lsResponseError::Write(Writer& visitor) {
|
|
|
|
auto& value = *this;
|
2017-05-21 23:22:00 +00:00
|
|
|
int code2 = static_cast<int>(this->code);
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2018-01-08 04:10:16 +00:00
|
|
|
visitor.StartObject();
|
2017-05-21 23:22:00 +00:00
|
|
|
REFLECT_MEMBER2("code", code2);
|
2017-03-25 22:13:19 +00:00
|
|
|
REFLECT_MEMBER(message);
|
|
|
|
if (data) {
|
|
|
|
visitor.Key("data");
|
|
|
|
data->Write(visitor);
|
|
|
|
}
|
|
|
|
visitor.EndObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
lsDocumentUri lsDocumentUri::FromPath(const std::string& path) {
|
|
|
|
lsDocumentUri result;
|
|
|
|
result.SetPath(path);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsDocumentUri::lsDocumentUri() {}
|
|
|
|
|
|
|
|
bool lsDocumentUri::operator==(const lsDocumentUri& other) const {
|
|
|
|
return raw_uri == other.raw_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lsDocumentUri::SetPath(const std::string& path) {
|
|
|
|
// file:///c%3A/Users/jacob/Desktop/superindex/indexer/full_tests
|
|
|
|
raw_uri = path;
|
|
|
|
|
|
|
|
size_t index = raw_uri.find(":");
|
2017-09-22 01:14:57 +00:00
|
|
|
if (index == 1) { // widows drive letters must always be 1 char
|
|
|
|
raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1,
|
|
|
|
"%3A");
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 01:26:09 +00:00
|
|
|
// subset of reserved characters from the URI standard
|
|
|
|
// http://www.ecma-international.org/ecma-262/6.0/#sec-uri-syntax-and-semantics
|
2018-01-29 06:11:53 +00:00
|
|
|
std::string t;
|
|
|
|
t.reserve(8 + raw_uri.size());
|
|
|
|
// TODO: proper fix
|
2017-03-28 05:27:06 +00:00
|
|
|
#if defined(_WIN32)
|
2018-01-29 06:11:53 +00:00
|
|
|
t += "file:///";
|
2017-03-28 05:27:06 +00:00
|
|
|
#else
|
2018-01-29 06:11:53 +00:00
|
|
|
t += "file://";
|
2017-03-28 05:27:06 +00:00
|
|
|
#endif
|
2018-01-29 06:11:53 +00:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
for (char c : raw_uri)
|
|
|
|
switch (c) {
|
|
|
|
case ' ': t += "%20"; break;
|
|
|
|
case '#': t += "%23"; break;
|
|
|
|
case '$': t += "%24"; break;
|
|
|
|
case '&': t += "%26"; break;
|
|
|
|
case '(': t += "%28"; break;
|
|
|
|
case ')': t += "%29"; break;
|
|
|
|
case '+': t += "%2B"; break;
|
|
|
|
case ',': t += "%2C"; break;
|
|
|
|
case ';': t += "%3B"; break;
|
|
|
|
case '?': t += "%3F"; break;
|
|
|
|
case '@': t += "%40"; break;
|
|
|
|
default: t += c; break;
|
|
|
|
}
|
|
|
|
// clang-format on
|
|
|
|
raw_uri = std::move(t);
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
std::string lsDocumentUri::GetPath() const {
|
2018-01-03 02:59:47 +00:00
|
|
|
if (raw_uri.compare(0, 8, "file:///"))
|
2018-01-23 17:02:36 +00:00
|
|
|
return raw_uri;
|
|
|
|
std::string ret;
|
2018-01-03 02:59:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
size_t i = 8;
|
2017-03-28 05:27:06 +00:00
|
|
|
#else
|
2018-01-03 02:59:47 +00:00
|
|
|
size_t i = 7;
|
2017-03-28 05:27:06 +00:00
|
|
|
#endif
|
2018-01-03 02:59:47 +00:00
|
|
|
auto from_hex = [](unsigned char c) {
|
|
|
|
return c - '0' < 10 ? c - '0' : (c | 32) - 'a' + 10;
|
|
|
|
};
|
|
|
|
for (; i < raw_uri.size(); i++) {
|
|
|
|
if (i + 3 <= raw_uri.size() && raw_uri[i] == '%') {
|
|
|
|
ret.push_back(from_hex(raw_uri[i + 1]) * 16 + from_hex(raw_uri[i + 2]));
|
|
|
|
i += 2;
|
|
|
|
} else
|
|
|
|
ret.push_back(raw_uri[i] == '\\' ? '/' : raw_uri[i]);
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
2018-01-03 02:59:47 +00:00
|
|
|
return ret;
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lsPosition::lsPosition() {}
|
2017-09-22 01:14:57 +00:00
|
|
|
lsPosition::lsPosition(int line, int character)
|
|
|
|
: line(line), character(character) {}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
|
|
|
bool lsPosition::operator==(const lsPosition& other) const {
|
|
|
|
return line == other.line && character == other.character;
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:04:08 +00:00
|
|
|
bool lsPosition::operator<(const lsPosition& other) const {
|
|
|
|
return line != other.line ? line < other.line : character < other.character;
|
|
|
|
}
|
|
|
|
|
2017-05-15 07:28:53 +00:00
|
|
|
std::string lsPosition::ToString() const {
|
|
|
|
return std::to_string(line) + ":" + std::to_string(character);
|
|
|
|
}
|
2017-11-22 17:52:33 +00:00
|
|
|
const lsPosition lsPosition::kZeroPosition = lsPosition();
|
2017-05-15 07:28:53 +00:00
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
lsRange::lsRange() {}
|
2017-04-05 08:06:18 +00:00
|
|
|
lsRange::lsRange(lsPosition start, lsPosition end) : start(start), end(end) {}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2018-02-13 01:15:19 +00:00
|
|
|
bool lsRange::operator==(const lsRange& o) const {
|
|
|
|
return start == o.start && end == o.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lsRange::operator<(const lsRange& o) const {
|
|
|
|
return !(start == o.start) ? start < o.start : end < o.end;
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lsLocation::lsLocation() {}
|
2017-09-22 01:14:57 +00:00
|
|
|
lsLocation::lsLocation(lsDocumentUri uri, lsRange range)
|
|
|
|
: uri(uri), range(range) {}
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2018-02-13 01:15:19 +00:00
|
|
|
bool lsLocation::operator==(const lsLocation& o) const {
|
|
|
|
return uri == o.uri && range == o.range;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lsLocation::operator<(const lsLocation& o) const {
|
|
|
|
return std::make_tuple(uri.raw_uri, range) <
|
|
|
|
std::make_tuple(o.uri.raw_uri, o.range);
|
2017-03-25 22:13:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 06:56:42 +00:00
|
|
|
bool lsTextEdit::operator==(const lsTextEdit& that) {
|
|
|
|
return range == that.range && newText == that.newText;
|
|
|
|
}
|
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
std::string Out_ShowLogMessage::method() {
|
|
|
|
if (display_type == DisplayType::Log)
|
|
|
|
return "window/logMessage";
|
|
|
|
return "window/showMessage";
|
2017-03-28 02:28:45 +00:00
|
|
|
}
|