Skip to content

Commit

Permalink
fmt: wrap exps into oneline if possibly
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jul 5, 2024
1 parent 68c11d7 commit 9f40478
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 42 deletions.
23 changes: 23 additions & 0 deletions src/lib/format_sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,26 @@ 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
Expand Down Expand Up @@ -636,6 +656,9 @@ module Make (Config : CONFIG) = struct
)
| Pragma (pragma, arg) -> char '$' ^^ string pragma ^^ space ^^ string arg ^^ hardline
| Block (always_hardline, exps) ->
let always_hardline =
match exps with [x] -> if can_chunks_list_wrap exps then false else always_hardline | _ -> always_hardline
in
let exps =
map_last
(fun no_semi chunks -> doc_block_exp_chunks (opts |> nonatomic |> statement_like) no_semi chunks)
Expand Down
20 changes: 5 additions & 15 deletions test/format/default/function_quant.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@ default Order dec
$include <prelude.sail>

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 = { () }
16 changes: 4 additions & 12 deletions test/format/default/struct_update.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
93 changes: 93 additions & 0 deletions test/format/default/wrap_into_oneline.expect
Original file line number Diff line number Diff line change
@@ -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 }
25 changes: 10 additions & 15 deletions test/format/function_quant.sail
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ default Order dec

$include <prelude.sail>

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 = { () }
27 changes: 27 additions & 0 deletions test/format/wrap_into_oneline.sail
Original file line number Diff line number Diff line change
@@ -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 */

0 comments on commit 9f40478

Please sign in to comment.