-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathLanguageServer.cpp
More file actions
106 lines (90 loc) · 2.56 KB
/
LanguageServer.cpp
File metadata and controls
106 lines (90 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "LanguageServer.h"
#include "Service/CodeActionService.h"
#include "Service/CommandService.h"
#include "Service/ConfigService.h"
#include "Service/DiagnosticService.h"
#include "Service/FormatService.h"
#include "Service/Service.h"
#include "Util/FileFinder.h"
#include "Util/Url.h"
#include "Util/format.h"
#include "asio.hpp"
#include <fstream>
#include <iterator>
LanguageServer::LanguageServer()
: _idCounter(0),
_ioc(1),
_lspHandle(this) {
}
void LanguageServer::InitializeService() {
AddService<FormatService>();
AddService<DiagnosticService>();
AddService<CommandService>();
AddService<CodeActionService>();
AddService<ConfigService>();
for (auto &service: _services) {
service->Initialize();
}
for (auto &service: _services) {
service->Start();
}
}
void LanguageServer::SetSession(std::shared_ptr<IOSession> session) {
_session = session;
}
std::shared_ptr<IOSession> LanguageServer::GetSession() {
return _session;
}
void LanguageServer::SendNotification(std::string_view method, std::shared_ptr<lsp::Serializable> param) {
auto json = nlohmann::json::object();
json["jsonrpc"] = "2.0";
json["method"] = method;
if (param != nullptr) {
json["params"] = param->Serialize();
} else {
json["params"] = nullptr;
}
if (_session) {
auto dumpResult = json.dump();
std::string message = util::format("Content-Length:{}\r\n\r\n", dumpResult.size());
message.append(dumpResult);
_session->Send(std::move(message));
}
}
void LanguageServer::SendRequest(std::string_view method, std::shared_ptr<lsp::Serializable> param) {
auto json = nlohmann::json::object();
json["jsonrpc"] = "2.0";
json["method"] = method;
json["id"] = GetRequestId();
if (param) {
json["params"] = param->Serialize();
} else {
json["params"] = nullptr;
}
if (_session) {
auto dumpResult = json.dump();
std::string message = util::format("Content-Length:{}\r\n\r\n", dumpResult.size());
message.append(dumpResult);
_session->Send(std::move(message));
}
}
int LanguageServer::Run() {
if (_session) {
int ret = _session->Run(*this);
_session = nullptr;
return ret;
}
return 1;
}
asio::io_context &LanguageServer::GetIOContext() {
return _ioc;
}
uint64_t LanguageServer::GetRequestId() {
return ++_idCounter;
}
LSPHandle &LanguageServer::GetLSPHandle() {
return _lspHandle;
}
VirtualFileSystem &LanguageServer::GetVFS() {
return _vfs;
}