From 6e3dc524ff26797a4877a77480921b3cf4135823 Mon Sep 17 00:00:00 2001 From: trdthg Date: Fri, 5 Jul 2024 08:09:57 +0000 Subject: [PATCH] fmt: wrap exps into oneline if possibly --- src/lib/format_sail.ml | 29 +++++- test/format/default/function_quant.expect | 20 ++--- test/format/default/struct_update.expect | 16 +--- test/format/default/wrap_into_oneline.expect | 93 ++++++++++++++++++++ test/format/function_quant.sail | 25 +++--- test/format/wrap_into_oneline.sail | 27 ++++++ 6 files changed, 167 insertions(+), 43 deletions(-) create mode 100644 test/format/default/wrap_into_oneline.expect create mode 100644 test/format/wrap_into_oneline.sail diff --git a/src/lib/format_sail.ml b/src/lib/format_sail.ml index 178e78a50..1415400eb 100644 --- a/src/lib/format_sail.ml +++ b/src/lib/format_sail.ml @@ -440,6 +440,27 @@ module type CONFIG = sig val config : config end +let rec can_chunks_list_wrap cqs = + match cqs with + | [] -> true + | [cq] -> ( + match List.of_seq (Queue.to_seq cq) with + | [] -> true + | [c] -> ( + match c with + (* Atom is ok *) + | Atom _ -> true + (* {{{ Atom }}} is ok *) + | Block (_, exps) -> can_chunks_list_wrap exps + | If_then_else (_, i, t, e) -> can_chunks_list_wrap [t; e] + | _ -> false + ) + | c :: cq -> + can_chunks_list_wrap [Queue.of_seq (List.to_seq [c])] + && can_chunks_list_wrap [Queue.of_seq (List.to_seq cq)] + ) + | cq :: cqs -> can_chunks_list_wrap [cq] && can_chunks_list_wrap cqs + module Make (Config : CONFIG) = struct let indent = Config.config.indent let preserve_structure = Config.config.preserve_structure @@ -636,6 +657,12 @@ module Make (Config : CONFIG) = struct ) | Pragma (pragma, arg) -> char '$' ^^ string pragma ^^ space ^^ string arg ^^ hardline | Block (always_hardline, exps) -> + let always_hardline, indent, b = + match exps with + | x :: [xs] -> (always_hardline, indent, 1) + | [x] -> if can_chunks_list_wrap exps then (false, 4, 1) else (always_hardline, indent, 1) + | _ -> (always_hardline, indent, 1) + in let exps = map_last (fun no_semi chunks -> doc_block_exp_chunks (opts |> nonatomic |> statement_like) no_semi chunks) @@ -643,7 +670,7 @@ module Make (Config : CONFIG) = struct in let sep = if always_hardline || List.exists snd exps then hardline else break 1 in let exps = List.map fst exps in - surround_hardline always_hardline indent 1 (char '{') (separate sep exps) (char '}') |> atomic_parens opts + surround_hardline always_hardline indent b (char '{') (separate sep exps) (char '}') |> atomic_parens opts | Block_binder (binder, x, y) -> if can_hang y then separate space diff --git a/test/format/default/function_quant.expect b/test/format/default/function_quant.expect index 5d0e0501f..b3bab1c1b 100644 --- a/test/format/default/function_quant.expect +++ b/test/format/default/function_quant.expect @@ -3,26 +3,16 @@ default Order dec $include function foo forall 'n 'm, 'n >= 0. -(x : int('n), y : int('m)) -> unit = { - () -} +(x : int('n), y : int('m)) -> unit = { () } function foo /* c */ forall 'n 'm, 'n >= 0. -(x : int('n), y : int('m)) -> unit = { - () -} +(x : int('n), y : int('m)) -> unit = { () } function foo forall /* c */ 'n 'm, 'n >= 0. -(x : int('n), y : int('m)) -> unit = { - () -} +(x : int('n), y : int('m)) -> unit = { () } function foo forall 'n 'm, /* c */ 'n >= 0. -(x : int('n), y : int('m)) -> unit = { - () -} +(x : int('n), y : int('m)) -> unit = { () } function foo forall 'very_long_identifier_that_will_cause_a_line_break 'm, 'n >= 0. -(x : int('very_long_identifier_that_will_cause_a_line_break), y : int('m)) -> unit = { - () -} +(x : int('very_long_identifier_that_will_cause_a_line_break), y : int('m)) -> unit = { () } diff --git a/test/format/default/struct_update.expect b/test/format/default/struct_update.expect index 88273bc44..8ecc92c38 100644 --- a/test/format/default/struct_update.expect +++ b/test/format/default/struct_update.expect @@ -44,22 +44,14 @@ enum E with f -> very_long_type_that_will_trigger_a_linebreak, } function has_loops () = { - foreach (n from 1 to 3) { - () - }; - foreach (n from 3 downto 1) { - () - }; - foreach (n from 0 to 4 by 2) { - () - }; + foreach (n from 1 to 3) { () }; + foreach (n from 3 downto 1) { () }; + foreach (n from 0 to 4 by 2) { () }; foreach (n from 10000000000000000000000000000000 to 444444444444444444444444444444444444444444444444444444444444) { () }; while true do (); - while true do { - () - }; + while true do { () }; repeat termination_measure { foo } () until true } diff --git a/test/format/default/wrap_into_oneline.expect b/test/format/default/wrap_into_oneline.expect new file mode 100644 index 000000000..c6fbcd8c1 --- /dev/null +++ b/test/format/default/wrap_into_oneline.expect @@ -0,0 +1,93 @@ +function f b = if b == bitone then { bitzero } else { bitone } +function f b = if b == bitone then { bitzero } else { bitone } +function f b = if b == bitone then { + let a = 1; + bitzero +} else { bitone } + +function f b = if b == bitone then { { bitzero } } else { bitone } +function f b = if b == bitone then { { { bitzero } } } else { bitone } +function f b = if b == bitone then { + { + let a = 1; + bitzero + } +} else { bitone } +function f b = if b == bitone then { + { + { + let a = 1; + bitzero + } + } +} else { bitone } +function f b = if b == bitone then { + { + { + { + let a = 1; + bitzero + } + } + } +} else { + { + { + { + let a = 1; + bitone + } + } + } +} +function f b = if b == bitone then { + { + { + { + let a = 1; + bitzero + } + } + } +} else { { { bitone } } } +function f b = if b == bitone then { + { + { + { + let a = 1; + bitzero + } + } + } +} else { + { + let a = 1; + { bitone } + } +} + +/* comment */ +function f /* comment */ b = if b == bitone then { bitzero } else { bitone } +function f /* comment */ b = if b == bitone then { bitzero } else { bitone } +function f b = /* comment */ if b == bitone then { bitzero } else { bitone } +function f b = /* comment */ if b == bitone then { bitzero } else { bitone } + +// TODO function f b =/* comment */if b == bitone then bitzero else bitone +function f b = if /* comment */ b == bitone then { bitzero } else { bitone } +function f b = if b == /* comment */ bitone then { bitzero } else { bitone } +function f b = if b == /* comment */ bitone then { bitzero } else { bitone } + +// TODO function f b = if b ==/* comment */bitone then bitzero else bitone +function f b = if b == bitone /* comment */ then { bitzero } else { bitone } +function f b = if b == bitone then { + /* comment */ bitzero +} else { bitone } +function f b = if b == bitone then { + bitzero /* comment */ +} else { bitone } +function f b = if b == bitone then { + bitzero /* comment */ +} else { bitone } +function f b = if b == bitone then { + bitzero /* comment */ +} else { bitone } diff --git a/test/format/function_quant.sail b/test/format/function_quant.sail index d30288de3..b3bab1c1b 100644 --- a/test/format/function_quant.sail +++ b/test/format/function_quant.sail @@ -2,22 +2,17 @@ default Order dec $include -function foo forall 'n 'm, 'n >= 0. (x: int('n), y: int('m)) -> unit = { - () -} +function foo forall 'n 'm, 'n >= 0. +(x : int('n), y : int('m)) -> unit = { () } -function foo /* c */ forall 'n 'm, 'n >= 0. (x: int('n), y: int('m)) -> unit = { - () -} +function foo /* c */ forall 'n 'm, 'n >= 0. +(x : int('n), y : int('m)) -> unit = { () } -function foo forall/* c */'n 'm, 'n >= 0. (x: int('n), y: int('m)) -> unit = { - () -} +function foo forall /* c */ 'n 'm, 'n >= 0. +(x : int('n), y : int('m)) -> unit = { () } -function foo forall 'n 'm, /* c */ 'n >= 0. (x: int('n), y: int('m)) -> unit = { - () -} +function foo forall 'n 'm, /* c */ 'n >= 0. +(x : int('n), y : int('m)) -> unit = { () } -function foo forall 'very_long_identifier_that_will_cause_a_line_break 'm, 'n >= 0. (x: int('very_long_identifier_that_will_cause_a_line_break), y: int('m)) -> unit = { - () -} +function foo forall 'very_long_identifier_that_will_cause_a_line_break 'm, 'n >= 0. +(x : int('very_long_identifier_that_will_cause_a_line_break), y : int('m)) -> unit = { () } diff --git a/test/format/wrap_into_oneline.sail b/test/format/wrap_into_oneline.sail new file mode 100644 index 000000000..0342c711b --- /dev/null +++ b/test/format/wrap_into_oneline.sail @@ -0,0 +1,27 @@ +function f b = if b == bitone then bitzero else bitone +function f b = if b == bitone then { bitzero } else { bitone } +function f b = if b == bitone then { let a = 1;bitzero } else { bitone } + +function f b = if b == bitone then { {bitzero } } else { bitone } +function f b = if b == bitone then { {{bitzero} } } else { bitone } +function f b = if b == bitone then { { let a = 1; bitzero } } else { bitone } +function f b = if b == bitone then { { {let a = 1; bitzero} } } else { bitone } +function f b = if b == bitone then { { { {let a = 1; bitzero}} } } else { {{{let a= 1; bitone}}} } +function f b = if b == bitone then { { { {let a = 1; bitzero}} } } else { {{bitone }}} +function f b = if b == bitone then { { { {let a = 1; bitzero}} } } else { {let a = 1;{bitone }}} + + /* comment */ +function/* comment */f b = if b == bitone then bitzero else bitone +function f/* comment */b = if b == bitone then bitzero else bitone +function f b/* comment */= if b == bitone then bitzero else bitone +function f b = /* comment */if b == bitone then bitzero else bitone +// TODO function f b =/* comment */if b == bitone then bitzero else bitone +function f b = if/* comment */b == bitone then bitzero else bitone +function f b = if b/* comment */== bitone then bitzero else bitone +function f b = if b == /* comment */ bitone then bitzero else bitone +// TODO function f b = if b ==/* comment */bitone then bitzero else bitone +function f b = if b == bitone/* comment */then bitzero else bitone +function f b = if b == bitone then/* comment */bitzero else bitone +function f b = if b == bitone then bitzero/* comment */else bitone +function f b = if b == bitone then bitzero else/* comment */bitone +function f b = if b == bitone then bitzero else bitone/* comment */