Source file theory.ml
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
module Handle = struct
type t =
{ doc : Doc.t
; cp_requests : Int.Set.t
; pt_requests : (int * (int * int)) list
}
let pt_eq (id1, (l1, c1)) (id2, (l2, c2)) = id1 = id2 && l1 = l2 && c1 = c2
let pt_gt x y =
let lx, cx = x in
let ly, cy = y in
lx > ly || (lx = ly && cx > cy)
let doc_table : (Lang.LUri.File.t, t) Hashtbl.t = Hashtbl.create 39
let create ~uri ~doc =
(match Hashtbl.find_opt doc_table uri with
| None -> ()
| Some _ ->
Io.Log.trace "do_open" "file %a not properly closed by client"
Lang.LUri.File.pp uri);
Hashtbl.add doc_table uri
{ doc; cp_requests = Int.Set.empty; pt_requests = [] }
let close ~uri = Hashtbl.remove doc_table uri
let with_doc ~kind ~f ~uri ~default =
match Hashtbl.find_opt doc_table uri with
| None ->
Io.Log.trace kind "document %a not available" Lang.LUri.File.pp uri;
default ()
| Some handle -> f handle handle.doc
let _find_opt ~uri = Hashtbl.find_opt doc_table uri
let _update_doc ~handle ~(doc : Doc.t) =
Hashtbl.replace doc_table doc.uri { handle with doc }
let pt_ids l = List.map (fun (id, _) -> id) l |> Int.Set.of_list
let clear_requests ~uri =
match Hashtbl.find_opt doc_table uri with
| None -> Int.Set.empty
| Some { cp_requests; pt_requests; doc } ->
let invalid_reqs = Int.Set.union cp_requests (pt_ids pt_requests) in
Hashtbl.replace doc_table uri
{ doc; cp_requests = Int.Set.empty; pt_requests = [] };
invalid_reqs
let update_doc_version ~(doc : Doc.t) =
let invalid_reqs = clear_requests ~uri:doc.uri in
Hashtbl.replace doc_table doc.uri
{ doc; cp_requests = Int.Set.empty; pt_requests = [] };
invalid_reqs
let map_cp_requests ~uri ~f =
match Hashtbl.find_opt doc_table uri with
| Some { doc; cp_requests; pt_requests } ->
let cp_requests = f cp_requests in
Hashtbl.replace doc_table uri { doc; cp_requests; pt_requests }
| None -> ()
let attach_cp_request ~uri ~id =
let f cp_requests = Int.Set.add id cp_requests in
map_cp_requests ~uri ~f
let remove_cp_request ~uri ~id =
let f cp_requests = Int.Set.remove id cp_requests in
map_cp_requests ~uri ~f
let map_pt_requests ~uri ~f =
match Hashtbl.find_opt doc_table uri with
| Some { doc; cp_requests; pt_requests } ->
let pt_requests = f pt_requests in
Hashtbl.replace doc_table uri { doc; cp_requests; pt_requests }
| None -> ()
let pt_insert x xs = CList.insert pt_gt x xs
let attach_pt_request ~uri ~id ~point =
let f pt_requests = pt_insert (id, point) pt_requests in
map_pt_requests ~uri ~f
let remove_pt_request ~uri ~id ~point =
let f pt_requests = CList.remove pt_eq (id, point) pt_requests in
map_pt_requests ~uri ~f
let do_requests ~doc ~handle =
let handle = { handle with doc } in
match doc.completed with
| Yes _ ->
let pt_ids = pt_ids handle.pt_requests in
let wake_up = Int.Set.union pt_ids handle.cp_requests in
let pt_requests = [] in
let cp_requests = Int.Set.empty in
({ handle with cp_requests; pt_requests }, wake_up)
| Stopped range ->
let fullfilled, delayed =
List.partition
(fun (_id, point) -> Doc.Target.reached ~range point)
handle.pt_requests
in
let handle = { handle with pt_requests = delayed } in
(handle, pt_ids fullfilled)
| Failed _ -> (handle, Int.Set.empty)
let update_doc_info ~handle ~(doc : Doc.t) =
let handle, requests = do_requests ~doc ~handle in
Hashtbl.replace doc_table doc.uri handle;
requests
end
module Register : sig
(** Run an action before a constructing the document root state. *)
module InjectRequire : sig
type t = io:Io.CallBack.t -> Coq.Workspace.Require.t list
val add : t -> unit
val fire : io:Io.CallBack.t -> Coq.Workspace.Require.t list
end
(** Run an action when a document has completed checking, attention, with or
without errors. *)
module Completed : sig
type t = io:Io.CallBack.t -> token:Coq.Limits.Token.t -> doc:Doc.t -> unit
val add : t -> unit
val fire : io:Io.CallBack.t -> token:Coq.Limits.Token.t -> doc:Doc.t -> unit
end
end = struct
module InjectRequire = struct
type t = io:Io.CallBack.t -> Coq.Workspace.Require.t list
let callback : t list ref = ref []
let add fn = callback := fn :: !callback
let fire ~io = List.concat_map (fun f -> f ~io) !callback
end
module Completed = struct
type t = io:Io.CallBack.t -> token:Coq.Limits.Token.t -> doc:Doc.t -> unit
let callback : t list ref = ref []
let add fn = callback := fn :: !callback
let fire ~io ~token ~doc =
let io = io in
List.iter (fun f -> f ~io ~token ~doc) !callback
end
end
let send_diags ~io ~token:_ ~doc =
let diags = Doc.diags doc in
if List.length diags > 0 || !Config.v.send_diags then
let uri, version = (doc.uri, doc.version) in
Io.Report.diagnostics ~io ~uri ~version diags
let send_perf_data ~io ~token:_ ~(doc : Doc.t) =
if !Config.v.send_perf_data then
let uri, version = (doc.uri, doc.version) in
Io.Report.perfData ~io ~uri ~version (Perf_analysis.make doc)
let () = Register.Completed.add send_perf_data
let () = Register.Completed.add send_diags
module Check : sig
val schedule : uri:Lang.LUri.File.t -> unit
val deschedule : uri:Lang.LUri.File.t -> unit
val maybe_check :
io:Io.CallBack.t -> token:Coq.Limits.Token.t -> (Int.Set.t * Doc.t) option
val set_scheduler_hint : uri:Lang.LUri.File.t -> point:int * int -> unit
end = struct
let pending = ref []
let pend_pop = function
| [] -> []
| _ :: p -> p
(** Push to front; beware of complexity here? Usually we don't have more than
10-20 files open in this use case; other use cases may require a more
efficient data-structure. *)
let pend_push uri pend = uri :: CList.remove Lang.LUri.File.equal uri pend
(** Try until [Some] *)
let rec pend_try f = function
| [] -> None
| l :: tt -> (
match f l with
| None -> pend_try f tt
| Some r -> Some r)
let hint : (Lang.LUri.File.t * (int * int)) option ref = ref None
let get_check_target ~(doc : Doc.t) pt_requests =
let target_of_pt_handle (_, (l, c)) = Doc.Target.Position (l, c) in
match Option.map target_of_pt_handle (List.nth_opt pt_requests 0) with
| None ->
Option.bind !hint (fun (uri, (l, c)) ->
if Lang.LUri.File.equal uri doc.uri then
match doc.completed with
| Yes _ | Failed _ -> None
| Stopped range when Doc.Target.reached ~range (l, c) -> None
| Stopped _ -> Some (Doc.Target.Position (l, c))
else None)
| Some t -> Some t
let report_start ~io doc =
let uri = doc.Doc.uri in
let uri_short = Lang.LUri.File.to_string_file uri |> Filename.basename in
Io.Report.serverStatus ~io (ServerInfo.Status.Running uri_short)
let report_idle ~io =
let mem =
Format.asprintf "%a" Stats.pp_words
(Gc.((quick_stat ()).heap_words) |> Float.of_int)
in
Io.Report.serverStatus ~io (ServerInfo.Status.Idle mem)
let do_check ~io ~token ~handle ~doc target =
let () = report_start ~io doc in
let doc = Doc.check ~io ~token ~target ~doc () in
let () = report_idle ~io in
let requests = Handle.update_doc_info ~handle ~doc in
if Doc.Completion.is_completed doc.completed then (
Register.Completed.fire ~io ~token ~doc;
pending := pend_pop !pending);
(requests, doc)
let check ~io ~token ~uri =
Io.Log.trace "process_queue" "resuming document checking";
let kind = "Check.check" in
let default () =
pending := pend_pop !pending;
None
in
let f (handle : Handle.t) doc =
let target = get_check_target ~doc handle.pt_requests in
match target with
| None
when !Config.v.check_only_on_request
&& Int.Set.is_empty handle.cp_requests ->
pending := pend_pop !pending;
None
| (None | Some _) as tgt ->
let target = Option.default Doc.Target.End tgt in
Some (do_check ~io ~token ~handle ~doc target)
in
Handle.with_doc ~kind ~uri ~default ~f
let maybe_check ~io ~token =
pend_try (fun uri -> check ~io ~token ~uri) !pending
let schedule ~uri = pending := pend_push uri !pending
let deschedule ~uri =
pending := CList.remove Lang.LUri.File.equal uri !pending
let set_scheduler_hint ~uri ~point =
if CList.is_empty !pending then
let () = hint := Some (uri, point) in
schedule ~uri
else if not (Option.is_empty !hint) then hint := Some (uri, point)
end
let create ~io ~token ~env ~uri ~raw ~version =
let = Register.InjectRequire.fire ~io in
let env = Doc.Env.inject_requires ~extra_requires env in
let doc = Doc.create ~token ~env ~uri ~raw ~version in
Handle.create ~uri ~doc;
Check.schedule ~uri
let change ~io:_ ~token ~(doc : Doc.t) ~version ~raw =
let uri = doc.uri in
Io.Log.trace "bump file" "%a / version: %d" Lang.LUri.File.pp uri version;
let tb = Unix.gettimeofday () in
let doc = Doc.bump_version ~token ~version ~raw doc in
let diff = Unix.gettimeofday () -. tb in
Io.Log.trace "bump file" "took %f seconds" diff;
let invalid = Handle.update_doc_version ~doc in
Check.schedule ~uri;
invalid
let change ~io ~token ~(doc : Doc.t) ~version ~raw =
if version > doc.version then change ~io ~token ~doc ~version ~raw
else
Int.Set.empty
let change ~io ~token ~uri ~version ~raw =
let kind = "Theory.change" in
let default () = Int.Set.empty in
let f _ doc = change ~io ~token ~doc ~version ~raw in
Handle.with_doc ~kind ~f ~uri ~default
let close ~uri =
Check.deschedule ~uri;
Handle.close ~uri
module Request = struct
type request =
| Immediate
| FullDoc
| PosInDoc of
{ point : int * int
; version : int option
}
type t =
{ id : int
; uri : Lang.LUri.File.t
; postpone : bool
; request : request
}
type action =
| Now of Doc.t
| Postpone
| Cancel
let request_in_range ~(doc : Doc.t) ~version (line, col) =
let in_range =
match doc.completed with
| Yes _ -> true
| Failed range | Stopped range -> Doc.Target.reached ~range (line, col)
in
let in_range =
match version with
| None -> in_range
| Some version -> doc.version >= version && in_range
in
in_range
(** Add a request to be served; returns [true] if request is added to the
queue , [false] if the request can be already answered. *)
let add { id; uri; postpone; request } =
let kind = "Request.add" in
let default () = Cancel in
match request with
| Immediate -> Handle.with_doc ~kind ~default ~uri ~f:(fun _ doc -> Now doc)
| FullDoc ->
Handle.with_doc ~kind ~default ~uri ~f:(fun _ doc ->
match (Doc.Completion.is_completed doc.completed, postpone) with
| true, _ -> Now doc
| false, false -> Cancel
| false, true ->
Handle.attach_cp_request ~uri ~id;
Check.schedule ~uri;
Postpone)
| PosInDoc { point; version } ->
Handle.with_doc ~kind ~default ~uri ~f:(fun _ doc ->
let in_range = request_in_range ~doc ~version point in
match (in_range, postpone) with
| true, _ -> Now doc
| false, true ->
Handle.attach_pt_request ~uri ~id ~point;
Check.schedule ~uri;
Postpone
| false, false -> Cancel)
(** Removes the request from the list of things to wake up *)
let remove { id; uri; postpone = _; request } =
match request with
| Immediate -> ()
| FullDoc -> Handle.remove_cp_request ~uri ~id
| PosInDoc { point; _ } -> Handle.remove_pt_request ~uri ~id ~point
end