Skip to content

Commit

Permalink
indent_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jul 24, 2024
1 parent 74a2afe commit ab0e0ec
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions src/lib/format_sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ let is_comment chunk = match chunk with Comment (_, _, _, _, _) | Doc_comment _
let is_block chunk = match chunk with Block (_, _) -> true | _ -> false
let is_match chunk = match chunk with Match _ -> true | _ -> false
let is_let chunk = match chunk with Binder (_, _, _, _) -> true | _ -> false
let is_delta chunk = match chunk with Delim _ | Opt_delim _ -> true | _ -> false

let rec function_with_only_one_rule ?(ignore_rule = fun chunk -> false) chunks target_rule =
let rec function_with_only_one_rule ?(ignore_rule = is_delta) chunks target_rule =
let rec go chunks target_count other_count =
match chunks with
| [] -> (target_count, other_count)
Expand Down Expand Up @@ -465,41 +466,56 @@ module type CONFIG = sig
val config : config
end

let rec can_chunks_list_wrap cqs =
let rec can_chunks_list_wrap ?(nest = 0) cqs =
match cqs with
| [] -> true
| [cq] -> (
let count = ref 0 in
match List.of_seq (Queue.to_seq cq) with
| [] -> true
| [c] -> (
match c with
(* Atom *)
| Atom _ -> true
(* Xs[unsigned(r)], *)
| App (_, chunks) -> can_chunks_list_wrap chunks
(* unit => (), *)
| Delim _ | Opt_delim _ -> true
(* (v128 >> shift)[64..0] *)
| Binary (x, op, y) -> can_chunks_list_wrap [x] && can_chunks_list_wrap [y]
(* if (a > 1) then {1} else {2} *)
| If_then_else (_, i, t, e) ->
can_chunks_list_wrap [i] && can_chunks_list_wrap [t] && can_chunks_list_wrap [e]
(* {{{ Atom }}} *)
| Block (_, exps) -> can_chunks_list_wrap exps
(* case => (), // comment *)
(* then 2 // comment *)
| Comment (Comment_line, _, _, _, trailing) -> trailing
| _ -> false
)
| [c] ->
(* if deep > 0, then not wrap
* i.e.
* - foo(a, bar(a, b))
*)
let can_wrap_nested =
match c with
| Atom _ | String_literal _ | Delim _ | Opt_delim _ -> true
| Block (_, exps) -> can_chunks_list_wrap ~nest exps
| _ -> false
in
if not can_wrap_nested then count := !count + 1;
if nest > 0 && not can_wrap_nested then false
else (
match c with
(* Atom *)
| Atom _ | String_literal _ -> true
(* unit => (), *)
| Delim _ | Opt_delim _ -> true (* Xs[unsigned(r)], *)
| App (id, chunks) -> can_chunks_list_wrap ~nest:1 chunks
(* (v128 >> shift)[64..0] *)
| Binary (x, op, y) -> can_chunks_list_wrap ~nest [x] && can_chunks_list_wrap ~nest [y]
(* if regval[31..16] then *)
| Index (exp, ix) -> can_chunks_list_wrap ~nest [exp] && can_chunks_list_wrap ~nest [ix]
(* if (a > 1) then {1} else {2} *)
| If_then_else (_, i, t, e) ->
can_chunks_list_wrap ~nest [i] && can_chunks_list_wrap ~nest [t] && can_chunks_list_wrap ~nest [e]
(* {{{ Atom }}} *)
| Block (_, exps) -> can_chunks_list_wrap ~nest exps
(* case => (), // comment *)
(* then 2 // comment *)
| Comment (Comment_line, _, _, _, trailing) -> trailing
| _ -> false
)
| c :: cq -> (
match c with
| Comment (_, _, _, _, _) -> false
| _ ->
can_chunks_list_wrap [Queue.of_seq (List.to_seq [c])]
&& can_chunks_list_wrap [Queue.of_seq (List.to_seq cq)]
can_chunks_list_wrap ~nest [Queue.of_seq (List.to_seq [c])]
&& can_chunks_list_wrap ~nest [Queue.of_seq (List.to_seq cq)]
)
)
| cq :: cqs -> can_chunks_list_wrap [cq] && can_chunks_list_wrap cqs
| cq :: cqs -> can_chunks_list_wrap ~nest [cq] && can_chunks_list_wrap ~nest cqs

module Make (Config : CONFIG) = struct
let indent = Config.config.indent
Expand Down Expand Up @@ -601,7 +617,7 @@ module Make (Config : CONFIG) = struct
|> atomic_parens opts
| Index (exp, ix) ->
let exp_doc, exp_wrap = doc_exps (opts |> atomic |> expression_like) exp in
let exp_doc = group (empty ^^ nest 4 exp_doc ^^ empty) in
let exp_doc = group (empty ^^ nest indent exp_doc ^^ empty) in
let ix_doc, ix_wrap = doc_exps (opts |> nonatomic |> expression_like) ix in
let hl = if ix_wrap then empty else hardline in
exp_doc ^^ char '[' ^^ ix_doc ^^ hl ^^ char ']' |> subatomic_parens opts
Expand Down Expand Up @@ -725,7 +741,7 @@ module Make (Config : CONFIG) = struct
[string (binder_keyword binder); doc_chunks (atomic opts) x; char '='; doc_chunks (nonatomic opts) y]
else
separate space [string (binder_keyword binder); doc_chunks (atomic opts) x; char '=']
^^ nest 4 (hardline ^^ doc_chunks (nonatomic opts) y)
^^ nest indent (hardline ^^ doc_chunks (nonatomic opts) y)
| Binder (binder, x, y, z) ->
let x, _ = doc_exps (atomic opts) x in
let y, _ = doc_exps (atomic opts) y in
Expand Down Expand Up @@ -805,7 +821,7 @@ module Make (Config : CONFIG) = struct
- t has braces, use space
- e wraped, use space
*)
prefix_hardline false 4 0 res
prefix_hardline false indent 0 res
in
(doc, wrapped)

Expand Down

0 comments on commit ab0e0ec

Please sign in to comment.