forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOni_Syntax_Server.re
222 lines (194 loc) · 6.36 KB
/
Oni_Syntax_Server.re
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
/*
Syntax Server
*/
module Protocol = Oni_Syntax.Protocol;
module ClientToServer = Protocol.ClientToServer;
type message =
| Log(string)
| Message(ClientToServer.t)
| Exception(string);
let start = (~healthCheck) => {
Stdlib.set_binary_mode_out(Stdlib.stdout, true);
Stdlib.set_binary_mode_in(Stdlib.stdin, true);
// A list of pending messages for us to handle
let messageQueue: ref(list(message)) = ref([]);
// Mutex to guard accessing the queue from multiple threads
let messageMutex = Mutex.create();
// And a semaphore for signaling when we have a new message
let messageCondition = Condition.create();
let outputMutex = Mutex.create();
let queue = msg => {
Mutex.lock(messageMutex);
messageQueue := [msg, ...messageQueue^];
Condition.signal(messageCondition);
Mutex.unlock(messageMutex);
};
let write = (msg: Protocol.ServerToClient.t) => {
Mutex.lock(outputMutex);
Marshal.to_channel(Stdlib.stdout, msg, []);
Stdlib.flush(Stdlib.stdout);
Mutex.unlock(outputMutex);
};
let log = msg => write(Protocol.ServerToClient.Log(msg));
let buffer = Buffer.create(0);
// Route Core.Log logging to be sent
// to main process.
let formatter =
Format.make_formatter(
Buffer.add_substring(buffer),
() => {
log(Buffer.contents(buffer));
Buffer.clear(buffer);
},
);
Logs.format_reporter(~app=formatter, ~dst=formatter, ())
|> Logs.set_reporter;
let hasPendingMessage = () =>
switch (messageQueue^) {
| [] => false
| _ => true
};
let flush = () => {
let result = messageQueue^;
messageQueue := [];
result;
};
let isRunning = ref(true);
let parentPid = Unix.getenv("__ONI2_PARENT_PID__") |> int_of_string;
let _runThread: Thread.t =
Thread.create(
() => {
log(
"Starting up server. Parent PID is: " ++ string_of_int(parentPid),
);
write(Protocol.ServerToClient.Initialized);
let state = ref(State.empty);
let map = f => state := f(state^);
let handleProtocol =
ClientToServer.(
fun
| Echo(m) => {
write(Protocol.ServerToClient.EchoReply(m));
log("handled echo");
}
| Initialize(languageInfo, setup) => {
map(State.initialize(~log, languageInfo, setup));
log("Initialized!");
}
| RunHealthCheck => {
let res = healthCheck();
write(Protocol.ServerToClient.HealthCheckPass(res == 0));
}
| BufferEnter(id, filetype) => {
log(
Printf.sprintf(
"Buffer enter - id: %d filetype: %s",
id,
filetype,
),
);
map(State.bufferEnter(id));
}
| ConfigurationChanged(config) => {
map(State.updateConfiguration(config));
let treeSitterEnabled =
Oni_Core.Configuration.getValue(
c => c.experimentalTreeSitter,
config,
);
log(
"got new config - treesitter enabled:"
++ (treeSitterEnabled ? "true" : "false"),
);
}
| ThemeChanged(theme) => {
map(State.updateTheme(theme));
log("handled theme changed");
}
| BufferUpdate(bufferUpdate, lines, scope) => {
map(State.bufferUpdate(~bufferUpdate, ~lines, ~scope));
log(
Printf.sprintf(
"Received buffer update - %d | %d lines",
bufferUpdate.id,
Array.length(lines),
),
);
}
| VisibleRangesChanged(visibilityUpdate) => {
map(State.updateVisibility(visibilityUpdate));
}
| Close => {
write(Protocol.ServerToClient.Closing);
exit(0);
}
| v => log("Unhandled message: " ++ ClientToServer.show(v))
);
let handleMessage =
fun
| Log(msg) => log(msg)
| Exception(msg) => log("Exception encountered: " ++ msg)
| Message(protocol) => handleProtocol(protocol);
while (isRunning^) {
log("Waiting for incoming message...");
// Wait for pending incoming messages
Mutex.lock(messageMutex);
while (!hasPendingMessage() && !State.anyPendingWork(state^)) {
Condition.wait(messageCondition, messageMutex);
};
// Once we have them, let's track and run them
let messages = flush();
Mutex.unlock(messageMutex);
// Handle messages
messages
// Messages are queued in inverse order, so we need to fix that...
|> List.rev
|> List.iter(handleMessage);
// TODO: Do this in a loop
// If the messages incurred work, do it!
if (State.anyPendingWork(state^)) {
log("Running unit of work...");
map(State.doPendingWork);
log("Unit of work completed.");
} else {
log("No pending work.");
};
let tokenUpdates = State.getTokenUpdates(state^);
write(Protocol.ServerToClient.TokenUpdate(tokenUpdates));
log("Token updates sent.");
map(State.clearTokenUpdates);
};
},
(),
);
let _readThread: Thread.t =
Thread.create(
() => {
while (isRunning^) {
try({
let msg: Oni_Syntax.Protocol.ClientToServer.t =
Marshal.from_channel(Stdlib.stdin);
queue(Message(msg));
}) {
| ex => queue(Exception(Printexc.to_string(ex)))
};
}
},
(),
);
// On Windows, we have to wait on the parent to close...
// If we don't do this, the app will never close.
if (Sys.win32) {
let (_exitCode, _status) = Thread.wait_pid(parentPid);
();
} else {
// On Linux / OSX, the syntax process will close when
// the parent closes.
let () = Thread.join(_readThread);
();
};
isRunning := false;
Stdlib.close_out_noerr(Stdlib.stdout);
Stdlib.close_in_noerr(Stdlib.stdin);
();
};