123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135(************************************************************************)(* * The Coq Proof Assistant / The Coq Development Team *)(* v * INRIA, CNRS and contributors - Copyright 1999-2018 *)(* <O___,, * (see CREDITS file for the list of authors) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)(************************************************************************)(* Coq Language Server Protocol *)(* Copyright 2019 MINES ParisTech -- Dual License LGPL 2.1 / GPL3+ *)(* Copyright 2019-2022 Inria -- Dual License LGPL 2.1 / GPL3+ *)(* Written by: Emilio J. Gallego Arias *)(************************************************************************)moduleF=FormatmoduleJ=Yojson.Safeletfn=ref(fun_->())letset_log_fnf=fn:=fletread_raw_requestic=letcl=input_lineicinletsin=Scanf.Scanning.from_stringclinletraw_obj=Scanf.bscanfsin"Content-Length: %d\r"(funsize->letbuf=Bytes.createsizein(* Consume the second \r\n or header *)letohdr=input_lineicin(* If the second line is a return, then no more headers *)let()=ifohdr.[0]='\r'then()else(* Fixme (or use ocaml-lsp) Skip the Content-type header *)ignore(input_lineic)inreally_inputicbuf0size;Bytes.to_stringbuf)inJ.from_stringraw_objexceptionReadErrorofstringletread_raw_requestic=trySome(read_raw_requestic)with(* if the end of input is encountered while some more characters are needed to
read the current conversion specification, or the lsp server closes *)|End_of_file->None(* if the input does not match the format. *)|Scanf.Scan_failuremsg(* if a conversion to a number is not possible. *)|Failuremsg(* if the format string is invalid. *)|Invalid_argumentmsg->raise(ReadErrormsg)letmut=Mutex.create()letlog=ref(fun__->())letsend_jsonfmtobj=Mutex.lockmut;ifFleche.Debug.sendthen!log"send"obj;letmsg=if!Fleche.Config.v.pp_jsonthenF.asprintf"%a"J.(pretty_print~std:true)objelseJ.to_stringobj^"\n"inletsize=String.lengthmsginF.fprintffmt"Content-Length: %d\r\n\r\n%s%!"sizemsg;Mutex.unlockmut(** Logging *)moduleTraceValue=structtypet=|Off|Messages|Verboseletof_string=function|"messages"->OkMessages|"verbose"->OkVerbose|"off"->OkOff|v->Error("TraceValue.parse: "^v)letto_string=function|Off->"off"|Messages->"messages"|Verbose->"verbose"endlettrace_value=refTraceValue.Offletset_trace_valuevalue=trace_value:=valueletlogMessage~lvl~message=letmethod_="window/logMessage"inletparams=`Assoc[("type",`Intlvl);("message",`Stringmessage)]inletmsg=Base.mk_notification~method_~paramsin!fnmsgletlogTrace~message~extra=letmethod_="$/logTrace"inletparams=match(!trace_value,extra)with|Verbose,Someextra->`Assoc[("message",`Stringmessage);("verbose",`Stringextra)]|_,_->`Assoc[("message",`Stringmessage)]inBase.mk_notification~method_~params|>!fnlettracehdr?extramsg=letmessage=Format.asprintf"[%s]: @[%s@]"hdrmsginlogTrace~message~extralettrace_objecthdrobj=letmessage=Format.asprintf"[%s]: @[%a@]"hdrYojson.Safe.(pretty_print~std:false)objin(* Fixme, use the extra parameter *)tracehdrmessagelet()=log:=trace_object(** Misc helpers *)letrecread_requestic=matchread_raw_requesticwith|None->None(* EOF *)|Somecom->(ifFleche.Debug.readthentrace_object"read"com;matchBase.Message.from_yojsoncomwith|Okmsg->Somemsg|Errormsg->trace"read_request"("error: "^msg);read_requestic)