From 92dfcb653ff6ffceeaed2d5403d6290226bb30c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Wed, 23 Feb 2022 14:33:46 +0100
Subject: [PATCH] [server] export more API

---
 src/plugins/server/Makefile.in |  2 ++
 src/plugins/server/data.ml     |  2 ++
 src/plugins/server/data.mli    |  4 ++++
 src/plugins/server/jbuffer.ml  | 10 ++++++++--
 src/plugins/server/jbuffer.mli |  3 +++
 5 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/plugins/server/Makefile.in b/src/plugins/server/Makefile.in
index 8c59c658adf..943f93e2fe1 100644
--- a/src/plugins/server/Makefile.in
+++ b/src/plugins/server/Makefile.in
@@ -82,9 +82,11 @@ include $(FRAMAC_SHARE)/Makefile.dynamic
 
 SERVER_API= \
 	package.mli \
+	jbuffer.mli \
 	data.mli \
 	request.mli \
 	states.mli \
+	main.mli \
 	kernel_main.mli \
 	kernel_ast.mli \
 	kernel_properties.mli
diff --git a/src/plugins/server/data.ml b/src/plugins/server/data.ml
index 70416e8a3b3..8b9722ba5d0 100644
--- a/src/plugins/server/data.ml
+++ b/src/plugins/server/data.ml
@@ -243,6 +243,8 @@ struct
     declare ~package ~name:"text" ~descr jdef
 end
 
+let jpretty = Jbuffer.to_json
+
 (* -------------------------------------------------------------------------- *)
 (* --- Functional API                                                     --- *)
 (* -------------------------------------------------------------------------- *)
diff --git a/src/plugins/server/data.mli b/src/plugins/server/data.mli
index d3e2e79bdf8..bef386bf9e4 100644
--- a/src/plugins/server/data.mli
+++ b/src/plugins/server/data.mli
@@ -87,6 +87,10 @@ module Jtext : S with type t = json
 
 module Jmarkdown : S with type t = Markdown.text
 
+(** All-in-one formatter. Return the JSON encoding of formatted text. *)
+val jpretty : ?indent:int -> ?margin:int ->
+  (Format.formatter -> 'a -> unit) -> 'a -> Jtext.t
+
 (* -------------------------------------------------------------------------- *)
 (** {2 Constructors} *)
 (* -------------------------------------------------------------------------- *)
diff --git a/src/plugins/server/jbuffer.ml b/src/plugins/server/jbuffer.ml
index 0cbac3b7d5c..f03483fc4c1 100644
--- a/src/plugins/server/jbuffer.ml
+++ b/src/plugins/server/jbuffer.ml
@@ -100,6 +100,7 @@ let bprintf buffer msg = Format.fprintf buffer.fmt msg
 let formatter buffer = buffer.fmt
 
 let contents buffer : json =
+  Format.pp_print_flush buffer.fmt () ;
   flush buffer () ;
   while buffer.stack <> [] do
     pop_tag buffer ""
@@ -112,15 +113,20 @@ let contents buffer : json =
 let format ?indent ?margin msg =
   let buffer = create ?indent ?margin () in
   Format.kfprintf
-    (fun fmt -> Format.pp_print_flush fmt () ; contents buffer)
+    (fun _fmt -> contents buffer)
     buffer.fmt msg
 
 let to_json ?indent ?margin pp a =
   let buffer = create ?indent ?margin () in
   pp buffer.fmt a ;
-  Format.pp_print_flush buffer.fmt () ;
   contents buffer
 
+let rec is_empty (js : json) = match js with
+  | `Null -> true
+  | `List js -> List.for_all is_empty js
+  | `String "" -> true
+  | _ -> false
+
 let rec fprintf fmt = function
   | `Null -> ()
   | `String text -> Format.pp_print_string fmt text
diff --git a/src/plugins/server/jbuffer.mli b/src/plugins/server/jbuffer.mli
index 08cc4050f3a..f9e175210bd 100644
--- a/src/plugins/server/jbuffer.mli
+++ b/src/plugins/server/jbuffer.mli
@@ -55,6 +55,9 @@ val pop_tag : buffer -> Format.stag -> unit
     tags. *)
 val contents : buffer -> json
 
+(** When [is_empty js] holds, the JSON is sure to be empty. *)
+val is_empty : json -> bool
+
 (** Prints back a JSON encoding into the provided formatter.
     @raise Yojson.Basic.Util.Type_error in case of ill formatted buffer. *)
 val fprintf : Format.formatter -> json -> unit
-- 
GitLab