-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.volt
249 lines (226 loc) · 6.59 KB
/
main.volt
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2018, Bernard Helyer.
// SPDX-License-Identifier: BSL-1.0
module main;
import core.rt.format : vrt_format_i64;
import core.rt.thread;
import lsp = vls.lsp;
import watt = [watt.path, watt.io, watt.io.file, watt.text.string,
watt.process.spawn, watt.process.environment, watt.text.sink,
watt.text.getopt, watt.io.streams];
import monotonic = watt.io.monotonic;
import json = watt.json;
import vlsc.util.aio;
import outputThread = vlsc.outputThread;
import inputThread = vls.lsp.inputThread;
import streams = watt.io.streams;
import diagnostics = vlsc.diagnostics;
import workspaces = vlsc.workspaces;
fn main(args: string[]) i32
{
inputFilename: string;
if (watt.getopt(ref args, "input", ref inputFilename)) {
inputThread.setInputFile(inputFilename);
} else {
inputThread.setStandardInput();
}
chdirToExtensionDirectory();
loop();
return 0;
}
//! Change directory to the extension directory.
fn chdirToExtensionDirectory()
{
watt.chdir(watt.dirName(watt.getExecFile()));
}
//! Launch the language server process.
fn spawnVls() watt.Pid
{
return watt.spawnProcess("vls.exe", null);
}
//! Spawn a language server and wait for it to exit cleanly.
fn loop()
{
ithread := vrt_thread_start_fn(inputThread.threadFunction);
othread := vrt_thread_start_fn(outputThread.threadFunction);
pool := new AsyncProcessPool();
scope (exit) pool.cleanup();
vls: AsyncProcess;
buildServer: AsyncProcess;
retval: u32 = 1;
// TODO: Make this cleaner, not duplicated nonsense
fn vlsReport(process: AsyncProcess, reason: AsyncProcessPool.InterruptReason)
{
if (reason == AsyncProcessPool.InterruptReason.ProcessComplete) {
if (process.closedRetval == 0) {
retval = process.closedRetval;
} else {
vls = pool.respawn(process, "vls.exe", null);
}
} else if (reason == AsyncProcessPool.InterruptReason.ReadComplete) {
str := process.readResult();
while (str.length != 0) {
msg: lsp.LspMessage;
str = lsp.parseLspMessage(str, out msg);
if (watt.indexOf(msg.content, "textDocument/publishDiagnostics") > 0) {
ro := new lsp.RequestObject(msg.content);
if (ro.methodName == "textDocument/publishDiagnostics") {
uri := lsp.getStringKey(ro.params, "uri");
if (uri !is null) {
diagnostics.emitLanguageServerDiagnostic(uri, msg.content);
return;
}
}
}
outputThread.addTask(msg.content);
}
}
}
fn buildReport(process: AsyncProcess, reason: AsyncProcessPool.InterruptReason)
{
if (reason == AsyncProcessPool.InterruptReason.ProcessComplete) {
vls = pool.respawn(process, "VlsBuildServer.exe", null);
} else if (reason == AsyncProcessPool.InterruptReason.ReadComplete) {
str := process.readResult();
while (str.length != 0) {
msg: lsp.LspMessage;
str = lsp.parseLspMessage(str, out msg);
ro := new lsp.RequestObject(msg.content);
handleBuildServerRequest(ro, vls);
}
}
}
vls = pool.spawn(vlsReport, "vls.exe", null);
buildServer = pool.spawn(buildReport, "VlsBuildServer.exe", null);
do {
message: lsp.LspMessage;
if (inputThread.getMessage(out message)) {
ro := new lsp.RequestObject(message.content);
if (workspaces.handleRequest(ro)) {
continue;
} else if (handleBuildMessage(ro, ref message)) {
outputThread.addTask(message.content, buildServer);
} else {
outputThread.addTask(message.content, vls);
}
}
pool.wait(ms:1);
} while (retval != 0 && !inputThread.done());
outputThread.stop();
vrt_thread_join(ithread);
vrt_thread_join(othread);
}
fn handleBuildServerRequest(ro: lsp.RequestObject, vlsOutput: watt.OutputStream)
{
switch (ro.methodName) {
case "textDocument/publishDiagnostics":
uri := lsp.getStringKey(ro.params, "uri");
if (uri !is null) {
buildTag := lsp.getStringKey(ro.params, "buildTag");
diagnostics.emitBuildServerDiagnostic(uri, buildTag, ro.originalText);
if (buildTag !is null) {
outputThread.addTask(lsp.buildShowMessage(lsp.MessageType.Error, new "Build failure: ${buildTag}"));
}
}
break;
case "vls/buildSuccess":
buildTag := lsp.getStringKey(ro.params, "buildTag");
if (buildTag !is null) {
diagnostics.clearBuildTag(buildTag);
}
outputThread.addTask(lsp.buildShowMessage(lsp.MessageType.Info, new "Build success: ${buildTag}"));
break;
case "vls/buildFailure":
buildTag := lsp.getStringKey(ro.params, "buildTag");
outputThread.addTask(lsp.buildShowMessage(lsp.MessageType.Error, new "Build failure: ${buildTag}"));
break;
case "vls/buildPending":
buildTag := lsp.getStringKey(ro.params, "buildTag");
outputThread.addTask(lsp.buildShowMessage(lsp.MessageType.Warning, new "Old build still running: ${buildTag}"));
break;
case "vls/toolchainPresent":
outputThread.addTask(ro.originalText, vlsOutput);
break;
case "vls/toolchainRetrievalFailure":
if (shouldShowDownloadFailureMessage()) {
outputThread.addTask(lsp.buildShowMessage(lsp.MessageType.Warning, "Toolchain archive could not be retrieved."));
}
break;
default:
break;
}
}
/*!
* Is given request a build server request?
*
* If `ro` is a message that should be passed to the
* build server, perform any modifications required
* to it and return `true`. Otherwise the request is
* untouched, and `false` is returned.
*/
fn handleBuildMessage(ro: lsp.RequestObject, ref message: lsp.LspMessage) bool
{
if (ro.methodName != "workspace/executeCommand") {
return false;
}
command := lsp.getStringKey(ro.params, "command");
switch (command) {
case "vls.buildProject":
return true;
case "vls.buildAllProjects":
message.content = getBuildAllContent(ro);
return true;
default:
return false;
}
}
fn getBuildAllContent(ro: lsp.RequestObject) string
{
ss: watt.StringSink;
ss.sink(`{"jsonrpc":"2.0","id":`);
vrt_format_i64(ss.sink, ro.id.integer());
ss.sink(`,"method":"workspace/executeCommand","params":{"command":"vls.buildAllProjects","arguments":[{},[]],`);
ss.sink(`"workspaceUris":[`);
uris := workspaces.getUris();
foreach (i, uri; uris) {
ss.sink(`"`);
ss.sink(uri);
ss.sink(`"`);
if (i < uris.length - 1) {
ss.sink(`,`);
}
}
ss.sink(`]}}`);
return ss.toString();
}
private:
global gLastDownloadFailure: i64;
global gNextDownloadFailure: i64;
global gStep: i64; // seconds
fn shouldShowDownloadFailureMessage() bool
{
if (monotonic.ticks() >= gNextDownloadFailure) {
gLastDownloadFailure = monotonic.ticks();
nextStep();
return true;
}
return false;
}
fn nextStep()
{
switch (gStep) {
case 0:
gStep += 5;
break;
case 5:
gStep += 10;
break;
case 15:
gStep = 60;
break;
case 60:
break;
default:
assert(false);
}
gNextDownloadFailure = gLastDownloadFailure + (gStep * monotonic.ticksPerSecond);
}