-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserver.ts
508 lines (491 loc) · 18.2 KB
/
server.ts
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import process from "process";
import * as p from "vscode-languageserver-protocol";
import * as m from "vscode-jsonrpc/lib/messages";
import * as v from "vscode-languageserver";
import * as path from "path";
import fs from "fs";
// TODO: check DidChangeWatchedFilesNotification.
import {
DidOpenTextDocumentNotification,
DidChangeTextDocumentNotification,
DidCloseTextDocumentNotification,
} from "vscode-languageserver-protocol";
import * as utils from "./utils";
import * as c from "./constants";
import * as chokidar from "chokidar";
import { assert } from "console";
import { fileURLToPath } from "url";
import { ChildProcess } from "child_process";
import {
binaryExists,
runDumpCommand,
runCompletionCommand,
} from "./RescriptEditorSupport";
// https://microsoft.github.io/language-server-protocol/specification#initialize
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
let initialized = false;
let serverSentRequestIdCounter = 0;
// https://microsoft.github.io/language-server-protocol/specification#exit
let shutdownRequestAlreadyReceived = false;
let stupidFileContentCache: Map<string, string> = new Map();
let projectsFiles: Map<
string, // project root path
{
openFiles: Set<string>;
filesWithDiagnostics: Set<string>;
bsbWatcherByEditor: null | ChildProcess;
}
> = new Map();
// ^ caching AND states AND distributed system. Why does LSP has to be stupid like this
let sendUpdatedDiagnostics = () => {
projectsFiles.forEach(({ filesWithDiagnostics }, projectRootPath) => {
let content = fs.readFileSync(
path.join(projectRootPath, c.compilerLogPartialPath),
{ encoding: "utf-8" }
);
let { done, result: filesAndErrors } = utils.parseCompilerLogOutput(
content
);
// diff
Object.keys(filesAndErrors).forEach((file) => {
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: filesAndErrors[file],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
process.send!(notification);
filesWithDiagnostics.add(file);
});
if (done) {
// clear old files
filesWithDiagnostics.forEach((file) => {
if (filesAndErrors[file] == null) {
// Doesn't exist in the new diagnostics. Clear this diagnostic
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: [],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
process.send!(notification);
filesWithDiagnostics.delete(file);
}
});
}
});
};
let deleteProjectDiagnostics = (projectRootPath: string) => {
let root = projectsFiles.get(projectRootPath);
if (root != null) {
root.filesWithDiagnostics.forEach((file) => {
let params: p.PublishDiagnosticsParams = {
uri: file,
diagnostics: [],
};
let notification: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: params,
};
process.send!(notification);
});
projectsFiles.delete(projectRootPath);
}
};
let compilerLogsWatcher = chokidar
.watch([], {
awaitWriteFinish: {
stabilityThreshold: 1,
},
})
.on("all", (_e, changedPath) => {
sendUpdatedDiagnostics();
});
let stopWatchingCompilerLog = () => {
// TODO: cleanup of compilerLogs?
compilerLogsWatcher.close();
};
type clientSentBuildAction = {
title: string;
projectRootPath: string;
};
let openedFile = (fileUri: string, fileContent: string) => {
let filePath = fileURLToPath(fileUri);
stupidFileContentCache.set(filePath, fileContent);
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
if (!projectsFiles.has(projectRootPath)) {
projectsFiles.set(projectRootPath, {
openFiles: new Set(),
filesWithDiagnostics: new Set(),
bsbWatcherByEditor: null,
});
compilerLogsWatcher.add(
path.join(projectRootPath, c.compilerLogPartialPath)
);
}
let root = projectsFiles.get(projectRootPath)!;
root.openFiles.add(filePath);
let firstOpenFileOfProject = root.openFiles.size === 1;
// check if .bsb.lock is still there. If not, start a bsb -w ourselves
// because otherwise the diagnostics info we'll display might be stale
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
let bsbPath = path.join(projectRootPath, c.bsbNodePartialPath);
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
// stale. Use that logic
// TODO: close watcher when lang-server shuts down
if (fs.existsSync(bsbPath)) {
let payload: clientSentBuildAction = {
title: c.startBuildAction,
projectRootPath: projectRootPath,
};
let params = {
type: p.MessageType.Info,
message: `Start a build for this project to get the freshest data?`,
actions: [payload],
};
let request: m.RequestMessage = {
jsonrpc: c.jsonrpcVersion,
id: serverSentRequestIdCounter++,
method: "window/showMessageRequest",
params: params,
};
process.send!(request);
// the client might send us back the "start build" action, which we'll
// handle in the isResponseMessage check in the message handling way
// below
} else {
// we should send something to say that we can't find bsb.exe. But right now we'll silently not do anything
}
}
// no need to call sendUpdatedDiagnostics() here; the watcher add will
// call the listener which calls it
}
};
let closedFile = (fileUri: string) => {
let filePath = fileURLToPath(fileUri);
stupidFileContentCache.delete(filePath);
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
let root = projectsFiles.get(projectRootPath);
if (root != null) {
root.openFiles.delete(filePath);
// clear diagnostics too if no open files open in said project
if (root.openFiles.size === 0) {
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.compilerLogPartialPath)
);
deleteProjectDiagnostics(projectRootPath);
if (root.bsbWatcherByEditor !== null) {
root.bsbWatcherByEditor.kill();
root.bsbWatcherByEditor = null;
}
}
}
}
};
let updateOpenedFile = (fileUri: string, fileContent: string) => {
let filePath = fileURLToPath(fileUri);
assert(stupidFileContentCache.has(filePath));
stupidFileContentCache.set(filePath, fileContent);
};
let getOpenedFileContent = (fileUri: string) => {
let filePath = fileURLToPath(fileUri);
let content = stupidFileContentCache.get(filePath)!;
assert(content != null);
return content;
};
process.on("message", (msg: m.Message) => {
if (m.isNotificationMessage(msg)) {
// notification message, aka the client ends it and doesn't want a reply
if (!initialized && msg.method !== "exit") {
// From spec: "Notifications should be dropped, except for the exit notification. This will allow the exit of a server without an initialize request"
// For us: do nothing. We don't have anything we need to clean up right now
// TODO: we might have things we need to clean up now... like some watcher stuff
} else if (msg.method === "exit") {
// The server should exit with success code 0 if the shutdown request has been received before; otherwise with error code 1
if (shutdownRequestAlreadyReceived) {
process.exit(0);
} else {
process.exit(1);
}
} else if (msg.method === DidOpenTextDocumentNotification.method) {
let params = msg.params as p.DidOpenTextDocumentParams;
let extName = path.extname(params.textDocument.uri);
if (extName === c.resExt || extName === c.resiExt) {
openedFile(params.textDocument.uri, params.textDocument.text);
}
} else if (msg.method === DidChangeTextDocumentNotification.method) {
let params = msg.params as p.DidChangeTextDocumentParams;
let extName = path.extname(params.textDocument.uri);
if (extName === c.resExt || extName === c.resiExt) {
let changes = params.contentChanges;
if (changes.length === 0) {
// no change?
} else {
// we currently only support full changes
updateOpenedFile(
params.textDocument.uri,
changes[changes.length - 1].text
);
}
}
} else if (msg.method === DidCloseTextDocumentNotification.method) {
let params = msg.params as p.DidCloseTextDocumentParams;
closedFile(params.textDocument.uri);
}
} else if (m.isRequestMessage(msg)) {
// request message, aka client sent request and waits for our mandatory reply
if (!initialized && msg.method !== "initialize") {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.ServerNotInitialized,
message: "Server not initialized.",
},
};
process.send!(response);
} else if (msg.method === "initialize") {
// send the list of features we support
let result: p.InitializeResult = {
// This tells the client: "hey, we support the following operations".
// Example: we want to expose "jump-to-definition".
// By adding `definitionProvider: true`, the client will now send "jump-to-definition" requests.
capabilities: {
// TODO: incremental sync?
textDocumentSync: v.TextDocumentSyncKind.Full,
documentFormattingProvider: true,
hoverProvider: binaryExists,
definitionProvider: binaryExists,
completionProvider: binaryExists
? { triggerCharacters: ["."] }
: undefined,
},
};
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: result,
};
initialized = true;
process.send!(response);
} else if (msg.method === "initialized") {
// sent from client after initialize. Nothing to do for now
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
process.send!(response);
} else if (msg.method === "shutdown") {
// https://microsoft.github.io/language-server-protocol/specification#shutdown
if (shutdownRequestAlreadyReceived) {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.InvalidRequest,
message: `Language server already received the shutdown request`,
},
};
process.send!(response);
} else {
shutdownRequestAlreadyReceived = true;
// TODO: recheck logic around init/shutdown...
stopWatchingCompilerLog();
// TODO: delete bsb watchers
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
process.send!(response);
}
} else if (msg.method === p.HoverRequest.method) {
let emptyHoverResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
// type result = Hover | null
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
result: null,
};
let result = runDumpCommand(msg);
if (result !== null && result.hover != null) {
let hoverResponse: m.ResponseMessage = {
...emptyHoverResponse,
result: { contents: result.hover },
};
process.send!(hoverResponse);
} else {
process.send!(emptyHoverResponse);
}
} else if (msg.method === p.DefinitionRequest.method) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
let emptyDefinitionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
// result should be: Location | Array<Location> | Array<LocationLink> | null
result: null,
// error: code and message set in case an exception happens during the definition request.
};
let result = runDumpCommand(msg);
if (result !== null && result.definition != null) {
let definitionResponse: m.ResponseMessage = {
...emptyDefinitionResponse,
result: {
uri: result.definition.uri || msg.params.textDocument.uri,
range: result.definition.range,
},
};
process.send!(definitionResponse);
} else {
process.send!(emptyDefinitionResponse);
}
} else if (msg.method === p.CompletionRequest.method) {
let emptyCompletionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
let code = getOpenedFileContent(msg.params.textDocument.uri);
let result = runCompletionCommand(msg, code);
if (result === null) {
process.send!(emptyCompletionResponse);
} else {
let definitionResponse: m.ResponseMessage = {
...emptyCompletionResponse,
result: result,
};
process.send!(definitionResponse);
}
} else if (msg.method === p.DocumentFormattingRequest.method) {
// technically, a formatting failure should reply with the error. Sadly
// the LSP alert box for these error replies sucks (e.g. doesn't actually
// display the message). In order to signal the client to display a proper
// alert box (sometime with actionable buttons), we need to first send
// back a fake success message (because each request mandates a
// response), then right away send a server notification to display a
// nicer alert. Ugh.
let fakeSuccessResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: [],
};
let params = msg.params as p.DocumentFormattingParams;
let filePath = fileURLToPath(params.textDocument.uri);
let extension = path.extname(params.textDocument.uri);
if (extension !== c.resExt && extension !== c.resiExt) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Not a ${c.resExt} or ${c.resiExt} file. Cannot format it.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
process.send!(fakeSuccessResponse);
process.send!(response);
} else {
// See comment on findBscExeDirOfFile for why we need
// to recursively search for bsc.exe upward
let bscExeDir = utils.findBscExeDirOfFile(filePath);
if (bscExeDir === null) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Cannot find a nearby ${c.bscExePartialPath}. It's needed for formatting.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "window/showMessage",
params: params,
};
process.send!(fakeSuccessResponse);
process.send!(response);
} else {
let resolvedBscExePath = path.join(bscExeDir, c.bscExePartialPath);
// code will always be defined here, even though technically it can be undefined
let code = getOpenedFileContent(params.textDocument.uri);
let formattedResult = utils.formatUsingValidBscExePath(
code,
resolvedBscExePath,
extension === c.resiExt
);
if (formattedResult.kind === "success") {
let result: p.TextEdit[] = [
{
range: {
start: { line: 0, character: 0 },
end: {
line: Number.MAX_VALUE,
character: Number.MAX_VALUE,
},
},
newText: formattedResult.result,
},
];
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: result,
};
process.send!(response);
} else {
// let the diagnostics logic display the updated syntax errors,
// from the build.
// Again, not sending the actual errors. See fakeSuccessResponse
// above for explanation
process.send!(fakeSuccessResponse);
}
}
}
} else {
let response: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
error: {
code: m.ErrorCodes.InvalidRequest,
message: "Unrecognized editor request.",
},
};
process.send!(response);
}
} else if (m.isResponseMessage(msg)) {
// response message. Currently the client should have only sent a response
// for asking us to start the build (see window/showMessageRequest in this
// file)
if (
msg.result != null &&
// @ts-ignore
msg.result.title != null &&
// @ts-ignore
msg.result.title === c.startBuildAction
) {
let msg_ = msg.result as clientSentBuildAction;
let projectRootPath = msg_.projectRootPath;
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
// TODO: sometime stale .bsb.lock dangling
// TODO: close watcher when lang-server shuts down. However, by Node's
// default, these subprocesses are automatically killed when this
// language-server process exits
if (fs.existsSync(bsbNodePath)) {
let bsbProcess = utils.runBsbWatcherUsingValidBsbNodePath(
bsbNodePath,
projectRootPath
);
let root = projectsFiles.get(projectRootPath)!;
root.bsbWatcherByEditor = bsbProcess;
// bsbProcess.on("message", (a) => console.log(a));
}
}
}
});