123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171openOcamlformatopenOcamlformat_stdlibmoduleIO=structtype'at='atypeic=In_channel.ttypeoc=Out_channel.tlet(>>=)xf=fxletreturnx=xletreadic=matchCsexp.inputicwith|Okx->return(Somex)|Error_->returnNoneletwriteoclx=List.iterlx~f:(Csexp.to_channeloc);Out_channel.flushoc;return()endmoduleRpc=Ocamlformat_rpc_lib_protocol.ProtocolmoduleProtocol=Rpc.Make(IO)moduleV=structlethandshakex=matchRpc.Version.of_stringxwith|Somev->`Handledv|None->`Propose_anotherRpc.Version.V2endtypestate=|Waiting_for_version|Version_definedof(Rpc.Version.t*Conf.t)letformatfgconfsource=letinput_name="<rpc input>"inTranslation_unit.parse_and_formatfg~input_name~sourceconfletrun_configconfc=letrecupdateconf=function|[]->Okconf|(name,value)::t->(matchConf.update_valueconf~name~valuewith|Okc->updatect|Errore->Error(`Config_errore))inupdateconfcletrun_pathpath=matchOcamlformat.Conf.build_config~enable_outside_detected_project:false~root:None~file:path~is_stdin:falsewith|Ok_asok->ok|Errore->Error(`Path_errore)letrun_formatconfx=List.fold_until~init:()~finish:(fun()->Error(`Format_error(Format.flush_str_formatter())))~f:(fun()try_formatting->matchtry_formattingconfxwith|Okformatted->Stop(Ok(`Formatformatted))|Errore->Translation_unit.Error.printFormat.str_formattere;Continue())(* The formatting functions are ordered in such a way that the ones
expecting a keyword first (like signatures) are placed before the more
general ones (like toplevel phrases). Parsing a file as `--impl` with
`ocamlformat` processes it as a use file (toplevel phrases) anyway.
`ocaml-lsp` should use core types, module types and signatures.
`ocaml-mdx` should use toplevel phrases, expressions and
signatures. *)[formatCore_type;formatSignature;formatModule_type;formatExpression;formatUse_file]letrun_format_with_args{Rpc.path;config}confx=letopenResultinOption.value_mappath~default:(Okconf)~f:run_path>>=funconf->Option.value_mapconfig~default:(Okconf)~f:(func->run_configconfc)>>=funconf->run_formatconfxlethandle_format_erroreoutput=outputstdout(`Errore)lethandle_path_erroreoutput=outputstdout(`Errore)lethandle_config_error(e:Config_option.Error.t)output=letmsg=matchewith|Bad_value(x,y)->Format.sprintf"Bad configuration value (%s, %s)"xy|Malformedx->Format.sprintf"Malformed configuration value %s"x|Misplaced(x,y)->Format.sprintf"Misplaced configuration value (%s, %s)"xy|Unknown(x,_)->Format.sprintf"Unknown configuration option %s"x|Version_mismatch{read;installed}->Format.sprintf"Version mismatch (%s, %s)"readinstalledinoutputstdout(`Errormsg)lethandle_erroreoutput=matchewith|`Format_errore->handle_format_erroreoutput|`Config_errore->handle_config_erroreoutput|`Path_errore->handle_path_erroreoutputletrecrpc_main=function|Waiting_for_version->(matchProtocol.Init.read_inputstdinwith|`Halt->Ok()|`Unknown->Ok()|`Versionvstr->(matchV.handshakevstrwith|`Handledv->Protocol.Init.outputstdout(`Versionvstr);rpc_main(Version_defined(v,Conf.default))|`Propose_anotherv->letvstr=Rpc.Version.to_stringvinProtocol.Init.outputstdout(`Versionvstr);rpc_mainWaiting_for_version))|Version_defined(v,conf)asstate->(matchvwith|V1->(matchProtocol.V1.read_inputstdinwith|`Halt->Ok()|`Unknown|`Error_->rpc_mainstate|`Formatx->letconf=matchrun_format_with_argsRpc.empty_argsconfxwith|Ok(`Formatformatted)->Protocol.V1.outputstdout(`Formatformatted);conf|Errore->handle_erroreProtocol.V1.output;confinrpc_main(Version_defined(v,conf))|`Configc->(matchrun_configconfcwith|Okconf->Protocol.V1.outputstdout(`Configc);rpc_main(Version_defined(v,conf))|Error(`Config_errore)->handle_config_erroreProtocol.V1.output;rpc_mainstate))|V2->(matchProtocol.V2.read_inputstdinwith|`Halt->Ok()|`Unknown|`Error_->rpc_mainstate|`Format(x,format_args)->letconf=matchrun_format_with_argsformat_argsconfxwith|Ok(`Formatformatted)->Protocol.V2.outputstdout(`Format(formatted,format_args));conf|Errore->handle_erroreProtocol.V2.output;confinrpc_main(Version_defined(v,conf))))letrun()=rpc_mainWaiting_for_version