Skip to content

Commit f06d63f

Browse files
committed
Cleanup
1 parent 223c888 commit f06d63f

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

server/src/server.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,8 @@ let projectsFiles: Map<
4040
> = new Map();
4141
// ^ caching AND states AND distributed system. Why does LSP has to be stupid like this
4242

43-
type messageHandler = (send: (msg: m.Message) => void, message: m.Message) => void;
44-
45-
let makeStdioChannel = () => {
46-
let writer = new rpc.StreamMessageWriter(process.stdout);
47-
let reader = new rpc.StreamMessageReader(process.stdin);
48-
let send = (msg: m.Message) => writer.write(msg);
49-
return {
50-
onMessage: ((func: messageHandler) => {
51-
let callback = (message: m.Message) => {
52-
func(send, message);
53-
}
54-
reader.listen(callback);
55-
}),
56-
send,
57-
};
58-
}
59-
60-
let makeNodeIpcChannel = () => {
61-
let send = (msg: m.Message) => process.send!(msg);
62-
return {
63-
onMessage: (func: messageHandler) => {
64-
process.on("message", (msg: m.Message) => {
65-
func(send, msg);
66-
})
67-
},
68-
send,
69-
};
70-
}
71-
72-
let channel = (
73-
process.argv.includes("--stdio")
74-
? makeStdioChannel()
75-
: makeNodeIpcChannel()
76-
);
43+
// will be properly defined below
44+
let send: (msg: m.Message) => void = (_) => { }
7745

7846
let sendUpdatedDiagnostics = () => {
7947
projectsFiles.forEach(({ filesWithDiagnostics }, projectRootPath) => {
@@ -96,7 +64,7 @@ let sendUpdatedDiagnostics = () => {
9664
method: "textDocument/publishDiagnostics",
9765
params: params,
9866
};
99-
channel.send(notification);
67+
send(notification);
10068

10169
filesWithDiagnostics.add(file);
10270
});
@@ -114,7 +82,7 @@ let sendUpdatedDiagnostics = () => {
11482
method: "textDocument/publishDiagnostics",
11583
params: params,
11684
};
117-
channel.send(notification);
85+
send(notification);
11886
filesWithDiagnostics.delete(file);
11987
}
12088
});
@@ -134,7 +102,7 @@ let deleteProjectDiagnostics = (projectRootPath: string) => {
134102
method: "textDocument/publishDiagnostics",
135103
params: params,
136104
};
137-
channel.send(notification);
105+
send(notification);
138106
});
139107

140108
projectsFiles.delete(projectRootPath);
@@ -203,7 +171,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
203171
method: "window/showMessageRequest",
204172
params: params,
205173
};
206-
channel.send(request);
174+
send(request);
207175
// the client might send us back the "start build" action, which we'll
208176
// handle in the isResponseMessage check in the message handling way
209177
// below
@@ -252,7 +220,22 @@ let getOpenedFileContent = (fileUri: string) => {
252220
return content;
253221
};
254222

255-
channel.onMessage((send, msg: m.Message) => {
223+
// Start listening now!
224+
// We support two modes: the regular node RPC mode for VSCode, and the --stdio
225+
// mode for other editors The latter is _technically unsupported_. It's an
226+
// implementation detail that might change at any time
227+
if (process.argv.includes("--stdio")) {
228+
let writer = new rpc.StreamMessageWriter(process.stdout);
229+
let reader = new rpc.StreamMessageReader(process.stdin);
230+
// proper `this` scope for writer
231+
send = (msg: m.Message) => writer.write(msg)
232+
reader.listen(onMessage)
233+
} else {
234+
// proper `this` scope for process
235+
send = (msg: m.Message) => process.send!(msg)
236+
process.on("message", onMessage)
237+
}
238+
function onMessage(msg: m.Message) {
256239
if (m.isNotificationMessage(msg)) {
257240
// notification message, aka the client ends it and doesn't want a reply
258241
if (!initialized && msg.method !== "exit") {
@@ -541,4 +524,4 @@ channel.onMessage((send, msg: m.Message) => {
541524
}
542525
}
543526
}
544-
});
527+
};

0 commit comments

Comments
 (0)