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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
// language-server.cpp
// This file implements the language server for Slang, conforming to the Language Server Protocol.
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include "../../source/core/slang-secure-crt.h"
#include "../../slang-com-helper.h"
#include "language-server-protocol.h"
#include "language-server.h"
namespace Slang
{
using namespace LanguageServerProtocol;
SlangResult LanguageServer::init(const InitializeParams& args)
{
SLANG_RETURN_ON_FAIL(m_connection->initWithStdStreams());
m_workspaceFolders = args.workspaceFolders;
return SLANG_OK;
}
slang::IGlobalSession* LanguageServer::getOrCreateGlobalSession()
{
if (!m_session)
{
// Just create the global session in the regular way if there isn't one set
if (SLANG_FAILED(slang_createGlobalSession(SLANG_API_VERSION, m_session.writeRef())))
{
return nullptr;
}
}
return m_session;
}
SlangResult LanguageServer::_executeSingle()
{
// If we don't have a message, we can quit for now
if (!m_connection->hasMessage())
{
return SLANG_OK;
}
const JSONRPCMessageType msgType = m_connection->getMessageType();
switch (msgType)
{
case JSONRPCMessageType::Call:
{
JSONRPCCall call;
SLANG_RETURN_ON_FAIL(m_connection->getRPCOrSendError(&call));
// Do different things
if (call.method == ExitParams::methodName)
{
m_quit = true;
return SLANG_OK;
}
else if (call.method == ShutdownParams::methodName)
{
m_connection->sendResult(NullResponse::get(), call.id);
return SLANG_OK;
}
else if (call.method == InitializeParams::methodName)
{
InitializeParams args = {};
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
init(args);
InitializeResult result = {};
result.serverInfo.name = "SlangLanguageServer";
result.serverInfo.version = "1.0";
result.capabilities.positionEncoding = "utf-8";
result.capabilities.textDocumentSync.openClose = true;
result.capabilities.textDocumentSync.change = (int)TextDocumentSyncKind::Full;
m_connection->sendResult(&result, call.id);
return SLANG_OK;
}
else if (call.method == DidOpenTextDocumentParams::methodName)
{
DidOpenTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didOpenTextDocument(args);
}
else if (call.method == DidCloseTextDocumentParams::methodName)
{
DidCloseTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didCloseTextDocument(args);
}
else if (call.method == DidChangeTextDocumentParams::methodName)
{
DidChangeTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didChangeTextDocument(args);
}
else if (call.method == "initialized")
{
return SLANG_OK;
}
else
{
return m_connection->sendError(JSONRPC::ErrorCode::MethodNotFound, call.id);
}
}
default:
{
return m_connection->sendError(
JSONRPC::ErrorCode::InvalidRequest, m_connection->getCurrentMessageId());
}
}
return SLANG_OK;
}
SlangResult LanguageServer::didOpenTextDocument(const DidOpenTextDocumentParams& args)
{
return SLANG_OK;
}
SlangResult LanguageServer::didCloseTextDocument(const DidCloseTextDocumentParams& args)
{
return SLANG_OK;
}
SlangResult LanguageServer::didChangeTextDocument(const DidChangeTextDocumentParams& args)
{
return SLANG_OK;
}
void LanguageServer::update()
{
}
SlangResult LanguageServer::execute()
{
m_connection = new JSONRPCConnection();
m_connection->initWithStdStreams();
while (m_connection->isActive() && !m_quit)
{
// Consume all messages first.
while (m_connection->tryReadMessage() == SLANG_OK)
{
const SlangResult res = _executeSingle();
}
// Now we can use this time to reparse user's code, report diagnostics, etc.
update();
}
return SLANG_OK;
}
} // namespace LanguageServer
int main(int argc, const char* const* argv)
{
bool isDebug = false;
for (auto i = 1; i < argc; i++)
{
if (Slang::UnownedStringSlice(argv[i]) == "--debug")
{
isDebug = true;
}
}
if (isDebug)
{
std::this_thread::sleep_for(std::chrono::seconds(10));
}
Slang::LanguageServer server;
SLANG_RETURN_ON_FAIL(server.execute());
return 0;
}
|