From 95505f8294e10a25283246e76b32c5567f0f8f85 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 20 Jul 2024 19:08:54 -0700 Subject: [PATCH 1/4] feat: install refmt manpage --- src/refmt/dune | 10 ++++++++ src/refmt/refmt.ml | 59 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/refmt/dune b/src/refmt/dune index 97ad91864..63da7a622 100644 --- a/src/refmt/dune +++ b/src/refmt/dune @@ -12,3 +12,13 @@ (progn (bash "echo let version = \\\"$(git rev-parse --verify HEAD)\\\"") (bash "echo let short_version = \\\"$(git rev-parse --short HEAD)\\\""))))) + +(rule + (with-stdout-to + reason.1 + (run %{bin:refmt} --help=groff))) + +(install + (section man) + (package reason) + (files reason.1)) diff --git a/src/refmt/refmt.ml b/src/refmt/refmt.ml index e2704353a..81a6c780e 100644 --- a/src/refmt/refmt.ml +++ b/src/refmt/refmt.ml @@ -100,9 +100,66 @@ let refmt (* FIXME: Reason_syntax_util.report_error Format.err_formatter exn; *) exit 1 +let split_lines s = + let rec loop ~last_is_cr ~acc i j = + if j = String.length s + then ( + let acc = + if j = i || (j = i + 1 && last_is_cr) + then acc + else String.sub s i (j - i) :: acc + in + List.rev acc) + else ( + match s.[j] with + | '\r' -> loop ~last_is_cr:true ~acc i (j + 1) + | '\n' -> + let line = + let len = if last_is_cr then j - i - 1 else j - i in + String.sub s i len + in + loop ~acc:(line :: acc) (j + 1) (j + 1) ~last_is_cr:false + | _ -> loop ~acc i (j + 1) ~last_is_cr:false) + in + loop ~acc:[] 0 0 ~last_is_cr:false +;; + +let examples = function + | [] -> `Blocks [] + | _ :: _ as examples -> + let block_of_example index (intro, ex) = + let prose = `I (Int.to_string (index + 1) ^ ".", String.trim intro ^ ":") + and code_lines = + ex + |> String.trim + |> split_lines + |> List.concat_map (fun codeline -> [ `Noblank; `Pre (" " ^ codeline) ]) + (* suppress initial blank *) + |> List.tl + in + `Blocks (prose :: code_lines) + in + let example_blocks = examples |> List.mapi block_of_example in + `Blocks (`S "EXAMPLES" :: example_blocks) +;; + + let top_level_info = let doc = "Reason's Parser & Pretty-printer" in - let man = [`S "DESCRIPTION"; `P "refmt lets you format Reason files, parse them, and convert them between OCaml syntax and Reason syntax."] in + let man = + [`S "DESCRIPTION" + ; `P "refmt lets you format Reason files, parse them, and convert them between OCaml syntax and Reason syntax." + ; (examples + [ "Initialise a new project named `foo'", "dune init project foo" + ; "Format a Reason implementation file", "refmt file.re" + ; "Format a Reason interface file", "refmt file.rei" + ; "Format interface code from the command line", "echo 'let x: int' | refmt --interface=true" + ; "Convert an OCaml file to Reason", "refmt file.ml" + ; "Convert a Reason file to OCaml", "refmt file.re --print ml" + ; "Convert OCaml from the command line to Reason", "echo 'let x = 1' | refmt --parse ml" + ]) + ] + in let version = "Reason " ^ Package.version ^ " @ " ^ Package.git_short_version in Cmd.info "refmt" ~version ~doc ~man From 448321956a770a47edcf3c54bc2a189a2d4ebf80 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 20 Jul 2024 19:12:51 -0700 Subject: [PATCH 2/4] restore exit behavior --- src/refmt/refmt.ml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/refmt/refmt.ml b/src/refmt/refmt.ml index 81a6c780e..4ef9418be 100644 --- a/src/refmt/refmt.ml +++ b/src/refmt/refmt.ml @@ -181,5 +181,6 @@ let refmt_t: [ `Error of bool * string | `Ok of unit ] Cmd.t = let () = match Cmd.eval_value' refmt_t with + | `Exit 0 -> exit 0 | `Exit _ -> exit 1 | _ -> exit 0 From d49f05540a7305afe55a41304be20115c7580449 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 20 Jul 2024 19:22:17 -0700 Subject: [PATCH 3/4] 4.06 compat --- src/refmt/refmt.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/refmt/refmt.ml b/src/refmt/refmt.ml index 4ef9418be..2389c580e 100644 --- a/src/refmt/refmt.ml +++ b/src/refmt/refmt.ml @@ -128,7 +128,7 @@ let examples = function | [] -> `Blocks [] | _ :: _ as examples -> let block_of_example index (intro, ex) = - let prose = `I (Int.to_string (index + 1) ^ ".", String.trim intro ^ ":") + let prose = `I (string_of_int (index + 1) ^ ".", String.trim intro ^ ":") and code_lines = ex |> String.trim From 1f8d8847d52cc62479b05d460f4a24dca83af830 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 20 Jul 2024 19:31:13 -0700 Subject: [PATCH 4/4] wip --- src/refmt/refmt.ml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/refmt/refmt.ml b/src/refmt/refmt.ml index 2389c580e..b3a060fc4 100644 --- a/src/refmt/refmt.ml +++ b/src/refmt/refmt.ml @@ -124,6 +124,14 @@ let split_lines s = loop ~acc:[] 0 0 ~last_is_cr:false ;; +let[@tail_mod_cons] rec concat_map f = function + | [] -> [] + | x::xs -> prepend_concat_map (f x) f xs +and[@tail_mod_cons] prepend_concat_map ys f xs = + match ys with + | [] -> concat_map f xs + | y :: ys -> y :: prepend_concat_map ys f xs + let examples = function | [] -> `Blocks [] | _ :: _ as examples -> @@ -133,7 +141,7 @@ let examples = function ex |> String.trim |> split_lines - |> List.concat_map (fun codeline -> [ `Noblank; `Pre (" " ^ codeline) ]) + |> concat_map (fun codeline -> [ `Noblank; `Pre (" " ^ codeline) ]) (* suppress initial blank *) |> List.tl in