12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576(************************************************************************)(* * 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.Safeletread_request_rawic=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_requestic=tryread_request_rawicwith(* 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->raise(ReadError"EOF")(* 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()letsend_jsonfmtobj=Mutex.lockmut;ifFleche.Debug.sendthenLog.log_object"send"obj;letmsg=F.asprintf"%a"J.(pretty_print~std:true)objinletsize=String.lengthmsginF.fprintffmt"Content-Length: %d\r\n\r\n%s%!"sizemsg;Mutex.unlockmutletlogMessagefmt~lvl~message=letmethod_="window/logMessage"inletparams=[("type",`Intlvl);("message",`Stringmessage)]inletmsg=Base.mk_notification~method_~paramsinsend_jsonfmtmsgletlogTracefmt~message?verbose()=letmethod_="$/logTrace"inletverbose=Option.cata(funv->[("verbose",`Stringv)])[]verboseinletparams=[("message",`Stringmessage)]@verboseinletmsg=Base.mk_notification~method_~paramsinsend_jsonfmtmsg