Source file documentManager.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
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
509
510
511
512
513
514
515
516
open Lsp.Types
open Protocol
open Protocol.LspWrapper
open Protocol.Printing
open Types
let Log log = Log.mk_log "documentManager"
type observe_id = Id of Types.sentence_id | Top
let to_sentence_id = function
| Top -> None
| Id id -> Some id
type state = {
uri : DocumentUri.t;
init_vs : Vernacstate.t;
opts : Coqargs.injection_command list;
document : Document.document;
execution_state : ExecutionManager.state;
observe_id : observe_id option;
}
type event =
| Execute of {
id : Types.sentence_id;
vst_for_next_todo : Vernacstate.t;
todo : ExecutionManager.prepared_task list;
started : float;
background: bool;
}
| ExecutionManagerEvent of ExecutionManager.event
let pp_event fmt = function
| Execute { id; todo; started; _ } ->
let time = Unix.gettimeofday () -. started in
Stdlib.Format.fprintf fmt "ExecuteToLoc %d (%d tasks left, started %2.3f ago)" (Stateid.to_int id) (List.length todo) time
| ExecutionManagerEvent _ -> Stdlib.Format.fprintf fmt "ExecutionManagerEvent"
let inject_em_event x = Sel.Event.map (fun e -> ExecutionManagerEvent e) x
let inject_em_events events = List.map inject_em_event events
type events = event Sel.Event.t list
type exec_overview = {
processing : Range.t list;
processed : Range.t list;
}
let merge_adjacent_ranges (r1,l) r2 =
if Position.compare r1.Range.end_ r2.Range.start == 0 then
Range.{ start = r1.Range.start; end_ = r2.Range.end_ }, l
else
r2, r1 :: l
let compress_sorted_ranges = function
| [] -> []
| range :: tl ->
let r, l = List.fold_left merge_adjacent_ranges (range,[]) tl in
r :: l
let compress_ranges ranges =
let ranges = List.sort (fun { Range.start = s1 } { Range.start = s2 } -> Position.compare s1 s2) ranges in
compress_sorted_ranges ranges
let executed_ranges doc execution_state loc =
let ranges_of l =
compress_ranges @@ List.map (Document.range_of_id_with_blank_space doc) l
in
let ids_before_loc = List.map (fun s -> s.Document.id) @@ Document.sentences_before doc loc in
let processed_ids = List.filter (fun x -> ExecutionManager.is_executed execution_state x || ExecutionManager.is_remotely_executed execution_state x) ids_before_loc in
log @@ Printf.sprintf "highlight: processed: %s" (String.concat " " (List.map Stateid.to_string processed_ids));
{
processing = [];
processed = ranges_of processed_ids;
}
let executed_ranges st =
let loc = match st.observe_id with
| None -> max_int
| Some Top -> 0
| Some (Id id) -> match Document.get_sentence st.document id with
| None -> failwith "observe_id does not exist"
| Some { stop } -> stop
in
executed_ranges st.document st.execution_state loc
let observe_id_range st =
let doc = Document.raw_document st.document in
match st.observe_id with
| None -> None
| Some Top -> None
| Some (Id id) -> match Document.get_sentence st.document id with
| None -> failwith "observe_id does not exist"
| Some { start; stop } ->
let start = RawDocument.position_of_loc doc start in
let end_ = RawDocument.position_of_loc doc stop in
let range = Range.{ start; end_ } in
Some range
let make_diagnostic doc range oloc message severity =
let range =
match oloc with
| None -> range
| Some loc ->
RawDocument.range_of_loc (Document.raw_document doc) loc
in
Diagnostic.create ~range ~message ~severity ()
let mk_diag st (id,(lvl,oloc,msg)) =
let lvl = DiagnosticSeverity.of_feedback_level lvl in
make_diagnostic st.document (Document.range_of_id st.document id) oloc (Pp.string_of_ppcmds msg) lvl
let mk_error_diag st (id,(oloc,msg)) = mk_diag st (id,(Feedback.Error,oloc, msg))
let mk_parsing_error_diag st Document.{ msg = (oloc,msg); start; stop } =
let doc = Document.raw_document st.document in
let severity = DiagnosticSeverity.Error in
let start = RawDocument.position_of_loc doc start in
let end_ = RawDocument.position_of_loc doc stop in
let range = Range.{ start; end_ } in
make_diagnostic st.document range oloc msg severity
let all_diagnostics st =
let parse_errors = Document.parse_errors st.document in
let all_exec_errors = ExecutionManager.all_errors st.execution_state in
let all_feedback = ExecutionManager.all_feedback st.execution_state in
let exists (id,_) = Option.has_some (Document.get_sentence st.document id) in
let exec_errors = all_exec_errors |> List.filter exists in
let feedback = all_feedback |> List.filter exists in
List.map (mk_parsing_error_diag st) parse_errors @
List.map (mk_error_diag st) exec_errors @
List.map (mk_diag st) feedback
let id_of_pos st pos =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match Document.find_sentence_before st.document loc with
| None -> None
| Some { id } -> Some id
let id_of_pos_opt st = function
| None -> begin match st.observe_id with None | Some Top -> None | Some Id id -> Some id end
| Some pos -> id_of_pos st pos
let get_messages st pos =
match id_of_pos_opt st pos with
| None -> log "get_messages: Could not find id";[]
| Some id -> log "get_messages: Found id";
let error = ExecutionManager.error st.execution_state id in
let feedback = ExecutionManager.feedback st.execution_state id in
let feedback = List.map (fun (lvl,_oloc,msg) -> DiagnosticSeverity.of_feedback_level lvl, pp_of_coqpp msg) feedback in
match error with
| Some (_oloc,msg) -> (DiagnosticSeverity.Error, pp_of_coqpp msg) :: feedback
| None -> feedback
let observe ~background state id : (state * event Sel.Event.t list) =
match Document.get_sentence state.document id with
| None -> (state, [])
| Some {id } ->
let vst_for_next_todo, todo = ExecutionManager.build_tasks_for (Document.schedule state.document) state.execution_state id in
if CList.is_empty todo then
(state, [])
else
let priority = if background then None else Some PriorityManager.execution in
let event = Sel.now ?priority (Execute {id; vst_for_next_todo; todo; started = Unix.gettimeofday (); background }) in
(state, [ event ])
let clear_observe_id st =
{ st with observe_id = None }
let reset_to_top st =
{ st with observe_id = Some Top }
let interpret_to st id =
let observe_id = if st.observe_id = None then None else (Some (Id id)) in
let st = { st with observe_id} in
observe ~background:false st id
let interpret_to_position st pos =
match id_of_pos st pos with
| None -> (st, [])
| Some id -> interpret_to st id
let get_next_range st pos =
match id_of_pos st pos with
| None -> None
| Some id ->
match Document.get_sentence st.document id with
| None -> None
| Some { stop } ->
match Document.find_sentence_after st.document (stop+1) with
| None -> Some (Document.range_of_id st.document id)
| Some { id } -> Some (Document.range_of_id st.document id)
let get_previous_range st pos =
match id_of_pos st pos with
| None -> None
| Some id ->
match Document.get_sentence st.document id with
| None -> None
| Some { start } ->
match Document.find_sentence_before st.document (start) with
| None -> Some (Document.range_of_id st.document id)
| Some { id } -> Some (Document.range_of_id st.document id)
let interpret_to_previous st =
match st.observe_id with
| None -> failwith "interpret to previous with no observe_id"
| Some Top -> (st, [])
| Some (Id id) ->
match Document.get_sentence st.document id with
| None -> (st, [])
| Some { start } ->
match Document.find_sentence_before st.document start with
| None ->
Vernacstate.unfreeze_full_state st.init_vs;
{ st with observe_id=(Some Top)}, []
| Some { id } -> interpret_to st id
let interpret_to_next st =
match st.observe_id with
| None -> failwith "interpret to next with no observe_id"
| Some Top ->
begin match Document.get_first_sentence st.document with
| None -> (st, [])
| Some {id} -> interpret_to st id
end
| Some (Id id) ->
match Document.get_sentence st.document id with
| None -> (st, [])
| Some { stop } ->
match Document.find_sentence_after st.document (stop+1) with
| None -> (st, [])
| Some {id } -> interpret_to st id
let interpret_to_end st =
match Document.get_last_sentence st.document with
| None -> (st, [])
| Some {id} -> log ("interpret_to_end id = " ^ Stateid.to_string id); interpret_to st id
let interpret_in_background st =
match Document.get_last_sentence st.document with
| None -> (st, [])
| Some {id} -> log ("interpret_to_end id = " ^ Stateid.to_string id); observe ~background:true st id
let is_above st id1 id2 =
let range1 = Document.range_of_id st id1 in
let range2 = Document.range_of_id st id2 in
Position.compare range1.start range2.start < 0
let validate_document state =
let unchanged_id, invalid_roots, document = Document.validate_document state.document in
let observe_id = match unchanged_id, state.observe_id with
| _, None -> None
| None, Some (Id _) -> None
| _, Some Top -> Some Top
| Some id, Some (Id id') -> if is_above state.document id id' then Some (Id id) else state.observe_id
in
let execution_state =
List.fold_left (fun st id ->
ExecutionManager.invalidate (Document.schedule state.document) id st
) state.execution_state (Stateid.Set.elements invalid_roots) in
{ state with document; execution_state; observe_id }
let init init_vs ~opts uri ~text observe_id =
Vernacstate.unfreeze_full_state init_vs;
let top = try Coqargs.(dirpath_of_top (TopPhysical (DocumentUri.to_path uri))) with
e -> raise e
in
Coqinit.start_library ~top opts;
let init_vs = Vernacstate.freeze_full_state () in
let document = Document.create_document init_vs.Vernacstate.synterp text in
let execution_state, feedback = ExecutionManager.init init_vs in
let st = { uri; opts; init_vs; document; execution_state; observe_id } in
validate_document st, [inject_em_event feedback]
let reset { uri; opts; init_vs; document; execution_state; observe_id } =
let text = RawDocument.text @@ Document.raw_document document in
Vernacstate.unfreeze_full_state init_vs;
let document = Document.create_document init_vs.synterp text in
ExecutionManager.destroy execution_state;
let execution_state, feedback = ExecutionManager.init init_vs in
let observe_id = if observe_id = None then None else (Some Top) in
let st = { uri; opts; init_vs; document; execution_state; observe_id} in
validate_document st, [inject_em_event feedback]
let apply_text_edits state edits =
let document = Document.apply_text_edits state.document edits in
let shift_diagnostics_locs exec_st (range, new_text) =
let edit_start = RawDocument.loc_of_position (Document.raw_document state.document) range.Range.start in
let edit_stop = RawDocument.loc_of_position (Document.raw_document state.document) range.Range.end_ in
let edit_length = edit_stop - edit_start in
ExecutionManager.shift_diagnostics_locs exec_st ~start:edit_stop ~offset:(String.length new_text - edit_length)
in
let execution_state = List.fold_left shift_diagnostics_locs state.execution_state edits in
validate_document { state with document; execution_state }
let handle_event ev st =
match ev with
| Execute { id; todo = []; started } ->
let time = Unix.gettimeofday () -. started in
log (Printf.sprintf "ExecuteToLoc %d ends after %2.3f" (Stateid.to_int id) time);
(Some st, [])
| Execute { id; vst_for_next_todo; started; todo = task :: todo; background } ->
let (execution_state,vst_for_next_todo,events,_interrupted) =
ExecutionManager.execute st.execution_state (vst_for_next_todo, [], false) task in
let priority = if background then None else Some PriorityManager.execution in
let event = Sel.now ?priority (Execute {id; vst_for_next_todo; todo; started; background }) in
(Some {st with execution_state}, inject_em_events events @ [event])
| ExecutionManagerEvent ev ->
let execution_state_update, events = ExecutionManager.handle_event ev st.execution_state in
(Option.map (fun execution_state -> {st with execution_state}) execution_state_update, inject_em_events events)
let get_proof st diff_mode pos =
let previous_st id =
let oid = fst @@ Scheduler.task_for_sentence (Document.schedule st.document) id in
Option.bind oid (ExecutionManager.get_vernac_state st.execution_state)
in
let observe_id =
match st.observe_id with
| None -> None
| Some observe_id -> to_sentence_id observe_id
in
let oid = Option.cata (id_of_pos st) observe_id pos in
let ost = Option.bind oid (ExecutionManager.get_vernac_state st.execution_state) in
let previous = Option.bind oid previous_st in
Option.bind ost (ProofState.get_proof ~previous diff_mode)
let context_of_id st = function
| None -> Some (ExecutionManager.get_initial_context st.execution_state)
| Some id -> ExecutionManager.get_context st.execution_state id
(** Get context at the start of the sentence containing [pos] *)
let get_context st pos = context_of_id st (id_of_pos st pos)
let get_completions st pos =
match id_of_pos st pos with
| None -> Error ("Can't get completions, no sentence found before the cursor")
| Some id ->
let ost = ExecutionManager.get_vernac_state st.execution_state id in
let settings = ExecutionManager.get_options () in
match Option.bind ost @@ CompletionSuggester.get_completions settings.completion_options with
| None -> Error ("Can't get completions")
| Some lemmas -> Ok (lemmas)
let parse_entry st pos entry pattern =
let pa = Pcoq.Parsable.make (Gramlib.Stream.of_string pattern) in
let st = match Document.find_sentence_before st.document pos with
| None -> st.init_vs.Vernacstate.synterp.parsing
| Some { synterp_state } -> synterp_state.Vernacstate.Synterp.parsing
in
Vernacstate.Parser.parse st entry pa
let about st pos ~pattern =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match get_context st pos with
| None -> Error ("No context found")
| Some (sigma, env) ->
try
let ref_or_by_not = parse_entry st loc (Pcoq.Prim.smart_global) pattern in
let udecl = None in
Ok (pp_of_coqpp @@ Prettyp.print_about env sigma ref_or_by_not udecl)
with e ->
let e, info = Exninfo.capture e in
Error (Pp.string_of_ppcmds @@ CErrors.iprint (e, info))
let search st ~id pos pattern =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match get_context st pos with
| None -> []
| Some (sigma, env) ->
let query, r = parse_entry st loc (G_vernac.search_queries) pattern in
SearchQuery.interp_search ~id env sigma query r
(** Try to generate hover text from [pattern] the context of the given [sentence] *)
let hover_of_sentence st loc pattern sentence =
match context_of_id st (Option.map (fun ({ id; _ }: Document.sentence) -> id) sentence) with
| None -> log "hover: no context found"; None
| Some (sigma, env) ->
try
let ref_or_by_not = parse_entry st loc (Pcoq.Prim.smart_global) pattern in
Language.Hover.get_hover_contents env sigma ref_or_by_not
with e ->
let e, info = Exninfo.capture e in
log ("Exception while handling hover: " ^ (Pp.string_of_ppcmds @@ CErrors.iprint (e, info)));
None
let hover st pos =
let opattern = RawDocument.word_at_position (Document.raw_document st.document) pos in
match opattern with
| None -> log "hover: no word found at cursor"; None
| Some pattern ->
log ("hover: found word at cursor: " ^ pattern);
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match hover_of_sentence st loc pattern (Document.find_sentence_before st.document loc) with
| Some _ as x -> x
| None ->
match Document.find_sentence_after st.document loc with
| None -> None
| Some sentence as opt ->
match hover_of_sentence st loc pattern opt with
| Some _ as x -> x
| None ->
match sentence.ast.classification with
| VtProofStep _ | VtStartProof _ ->
hover_of_sentence st loc pattern (Document.find_next_qed st.document loc)
| _ -> None
let check st pos ~pattern =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match get_context st pos with
| None -> Error ("No context found")
| Some (sigma,env) ->
let rc = parse_entry st loc Pcoq.Constr.lconstr pattern in
try
let redexpr = None in
Ok (pp_of_coqpp @@ Vernacentries.check_may_eval env sigma redexpr rc)
with e ->
let e, info = Exninfo.capture e in
Error (Pp.string_of_ppcmds @@ CErrors.iprint (e, info))
[%%if coq = "8.18" || coq = "8.19"]
let print_located_qualid _ qid = Prettyp.print_located_qualid qid
[%%else]
let print_located_qualid = Prettyp.print_located_qualid
[%%endif]
let locate st pos ~pattern =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match get_context st pos with
| None -> Error ("No context found")
| Some (sigma, env) ->
match parse_entry st loc (Pcoq.Prim.smart_global) pattern with
| { v = AN qid } -> Ok (pp_of_coqpp @@ print_located_qualid env qid)
| { v = ByNotation (ntn, sc)} ->
Ok( pp_of_coqpp @@ Notation.locate_notation
(Constrextern.without_symbols (Printer.pr_glob_constr_env env sigma)) ntn sc)
let print st pos ~pattern =
let loc = RawDocument.loc_of_position (Document.raw_document st.document) pos in
match get_context st pos with
| None -> Error("No context found")
| Some (sigma, env) ->
let qid = parse_entry st loc (Pcoq.Prim.smart_global) pattern in
let udecl = None in
Ok ( pp_of_coqpp @@ Prettyp.print_name env sigma qid udecl )
module Internal = struct
let document st =
st.document
let raw_document st =
Document.raw_document st.document
let execution_state st =
st.execution_state
let observe_id st =
match st.observe_id with
| None | Some Top -> None
| Some (Id id) -> Some id
let validate_document st =
validate_document st
let string_of_state st =
let code_lines = Document.code_lines_sorted_by_loc st.document in
let string_of_state id =
if ExecutionManager.is_executed st.execution_state id then "(executed)"
else if ExecutionManager.is_remotely_executed st.execution_state id then "(executed in worker)"
else "(not executed)"
in
let string_of_item item =
Document.Internal.string_of_item item ^ " " ^
match item with
| Sentence { id } -> string_of_state id
| ParsingError _ -> "(error)"
| Comment _ -> "(comment)"
in
String.concat "\n" @@ List.map string_of_item code_lines
end