Skip to content

Commit

Permalink
Allow nowrap on nested chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jul 27, 2024
1 parent bf03554 commit cfaca50
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
72 changes: 36 additions & 36 deletions src/lib/format_sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,10 @@ module type CONFIG = sig
end

type chunk_wrap_opt_state = { non_delim_count : int; non_wrap_count : int }
type chunk_wrap_opt = { strict : bool; app_nest : int; in_braces : bool; state : chunk_wrap_opt_state }
let app_nest_add opt = { opt with app_nest = opt.app_nest + 1 }
type chunk_wrap_opt = { strict : bool; in_braces : bool; state : chunk_wrap_opt_state }
let check_nowrap_strict opt = { opt with strict = true }
let in_braces opt = { opt with in_braces = true }
let default_opt =
{ strict = false; app_nest = 0; in_braces = false; state = { non_delim_count = 0; non_wrap_count = 0 } }
let default_opt = { strict = false; in_braces = false; state = { non_delim_count = 0; non_wrap_count = 0 } }

let rec check_chunks_nowrap ?(opt = default_opt) cqs =
match cqs with
Expand All @@ -487,30 +485,26 @@ let rec check_chunks_nowrap ?(opt = default_opt) cqs =
else (
non_delim_count := !non_delim_count + 1;
(* strict rule, for nested chunks *)
let check_chunk_nowrap_strict =
match c with
| Atom _ | String_literal _ -> true
| Field (cq, id) -> check_chunks_nowrap ~opt [cq]
| Block (_, cqs) -> check_chunks_nowrap ~opt cqs
| Index (cq, ix) -> check_chunks_nowrap ~opt:(opt |> in_braces) [cq] && check_chunks_nowrap ~opt [ix]
| App (id, cqs) -> List.length cqs = 1 && check_chunks_nowrap ~opt:(opt |> app_nest_add) cqs
| _ -> false
in
if opt.strict then check_chunk_nowrap_strict
else (
if check_chunk_nowrap_strict then nest_nowrap_count := !nest_nowrap_count + 1;
(* normal rule *)
let check_no_wrap c =
match c with
(* Atom *)
| Atom _ | String_literal _ -> true
(* Xs[unsigned(r)], *)
| App (id, cqs) -> check_chunks_nowrap ~opt:(opt |> app_nest_add) cqs
(* zero_extend(fcsr.bits) *)
| Field (cq, id) -> check_chunks_nowrap ~opt [cq]
| Block (_, cqs) -> check_chunks_nowrap ~opt cqs
(* (v128 >> shift)[64..0] *)
| Binary (x, op, y) -> check_chunks_nowrap ~opt [x] && check_chunks_nowrap ~opt [y]
(* if regval[31..16] then *)
| Index (cq, ix) -> check_chunks_nowrap ~opt:(opt |> in_braces) [cq] && check_chunks_nowrap ~opt [ix]
| Index (cq, ix) ->
let opt = opt |> in_braces in
check_chunks_nowrap ~opt [cq] && check_chunks_nowrap ~opt [ix]
(* Xs[unsigned(r)], *)
| App (id, cqs) -> check_chunks_nowrap ~opt cqs
| _ -> false
in
let check_nowrap_extra c =
match c with
| App (id, cqs) -> check_chunks_nowrap ~opt cqs
(* if (a > 1) then {1} else {2} *)
| If_then_else (_, i, t, e) ->
check_chunks_nowrap ~opt [i] && check_chunks_nowrap ~opt [t] && check_chunks_nowrap ~opt [e]
Expand All @@ -527,6 +521,12 @@ let rec check_chunks_nowrap ?(opt = default_opt) cqs =
| _ -> false
)
| _ -> false
in
let res = check_no_wrap c in
if opt.strict then res
else (
if res then nest_nowrap_count := !nest_nowrap_count + 1;
res || check_nowrap_extra c
)
)
in
Expand All @@ -541,7 +541,7 @@ module Make (Config : CONFIG) = struct
let indent = Config.config.indent
let preserve_structure = Config.config.preserve_structure

let rec doc_chunk ?(ungroup_tuple = false) ?(toplevel = false) ?(wrap = true)
let rec doc_chunk ?(ungroup_tuple = false) ?(toplevel = false) ?(nowrap = true)
?(force_block_comment_end_with_hardline = false) opts = function
| Atom s -> string s
| Chunks chunks -> doc_chunks opts chunks
Expand Down Expand Up @@ -604,10 +604,10 @@ module Make (Config : CONFIG) = struct
if outer_prec > opts.precedence then parens doc else doc
| If_then_else (bracing, i, t, e) ->
let insert_braces = opts.statement || bracing.then_brace || bracing.else_brace in
let i_doc, i_wrap = doc_chunks_exps (opts |> nonatomic |> expression_like) i in
let i_doc, i_nowrap = doc_chunks_exps (opts |> nonatomic |> expression_like) i in
(* check_nowrap with strict mode here *)
let t_wrap = check_chunks_nowrap ~opt:(default_opt |> check_nowrap_strict) [t] in
let e_wrap = check_chunks_nowrap ~opt:(default_opt |> check_nowrap_strict) [e] in
let t_nowrap = check_chunks_nowrap ~opt:(default_opt |> check_nowrap_strict) [t] in
let e_nowrap = check_chunks_nowrap ~opt:(default_opt |> check_nowrap_strict) [e] in
(* if any one can't wrap, then all should expand
i.e.
Expand All @@ -619,16 +619,16 @@ module Make (Config : CONFIG) = struct
3
}
*)
let wrap = i_wrap && t_wrap && e_wrap in
let nowrap = i_nowrap && t_nowrap && e_nowrap in
let t_doc, t_wrap =
if insert_braces && (not preserve_structure) && not bracing.then_brace then
(doc_chunk opts (Block (true, [t])), false)
else doc_chunks_exps ~wrap (opts |> nonatomic |> expression_like) t
else doc_chunks_exps ~nowrap ~strict:true (opts |> nonatomic |> expression_like) t
in
let e_doc, e_wrap =
if insert_braces && (not preserve_structure) && not bracing.else_brace then
(doc_chunk opts (Block (true, [e])), false)
else doc_chunks_exps ~wrap (opts |> nonatomic |> expression_like) e
else doc_chunks_exps ~nowrap ~strict:true (opts |> nonatomic |> expression_like) e
in
let doc_i = separate space [string "if"; i_doc] in
let doc_t = separate space [string "then"; t_doc] in
Expand All @@ -650,8 +650,8 @@ module Make (Config : CONFIG) = struct
- t has braces, use space
- e force to expand
*)
if wrap then doc_i ^^ sep i_wrap ^^ doc_t ^^ space ^^ doc_e |> atomic_parens opts
else doc_i ^^ sep i_wrap ^^ doc_t ^^ sep t_wrap ^^ doc_e |> atomic_parens opts
if nowrap then doc_i ^^ sep i_nowrap ^^ doc_t ^^ space ^^ doc_e |> atomic_parens opts
else doc_i ^^ sep i_nowrap ^^ doc_t ^^ sep t_wrap ^^ doc_e |> atomic_parens opts
| If_then (bracing, i, t) ->
let i = doc_chunks (opts |> nonatomic |> expression_like) i in
let t =
Expand Down Expand Up @@ -777,7 +777,7 @@ module Make (Config : CONFIG) = struct
| Block (always_hardline, exps) ->
let always_hardline =
match exps with
| [x] -> if not wrap then true else if check_chunks_nowrap exps then false else always_hardline
| [x] -> if not nowrap then true else if check_chunks_nowrap exps then false else always_hardline
| _ -> always_hardline
in
let exps =
Expand Down Expand Up @@ -861,15 +861,15 @@ module Make (Config : CONFIG) = struct
a
]
*)
and doc_chunks_wrapped ?(ungroup_tuple = false) ?(force_hardline = false) opts chunks =
and doc_chunks_wrapped ?(ungroup_tuple = false) opts chunks =
let doc = Queue.fold (fun doc chunk -> doc ^^ doc_chunk ~ungroup_tuple opts chunk) empty chunks in
let wrapped = check_chunks_nowrap [chunks] in
if wrapped then doc else surround_hardline true indent 1 empty doc empty

(* Format chunks, add prefix if can't wrap *)
and doc_chunks_exps ?(ungroup_tuple = false) ?(wrap = true) opts chunks =
let doc = Queue.fold (fun doc chunk -> doc ^^ doc_chunk ~wrap ~ungroup_tuple opts chunk) empty chunks in
let wrapped = check_chunks_nowrap [chunks] in
(* Format chunks, add prefix if wrap *)
and doc_chunks_exps ?(ungroup_tuple = false) ?(nowrap = true) ?(strict = false) opts chunks =
let doc = Queue.fold (fun doc chunk -> doc ^^ doc_chunk ~nowrap ~ungroup_tuple opts chunk) empty chunks in
let wrapped = check_chunks_nowrap ~opt:{ default_opt with strict } [chunks] in

let rec count_chunks_block_like chunks =
count_chunks_by_rule chunks (fun chunk -> is_block chunk || is_match chunk)
Expand All @@ -887,7 +887,7 @@ module Make (Config : CONFIG) = struct
*)
let no_prefix = count_chunks_block_like (List.of_seq (Queue.to_seq chunks)) = (1, 0) in
let doc =
(* add indent for multi line exps which can't be wrapped *)
(* add prefix for multi line exps which are wrapped *)
if wrapped || no_prefix then doc else prefix_hardline true indent 0 doc
in
(doc, wrapped)
Expand Down
6 changes: 1 addition & 5 deletions test/format/default/wrap_into_oneline.expect
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ function f b = if b == bitone then {
}

function valid_hex_bits_signed(n, str) = {
if string_take(str, 1) == "-" then {
valid_hex_bits(n, string_drop(str, 1))
} else {
valid_hex_bits(n, str)
}
if string_take(str, 1) == "-" then { valid_hex_bits(n, string_drop(str, 1)) } else { valid_hex_bits(n, str) }
}

function if_stmt () =
Expand Down

0 comments on commit cfaca50

Please sign in to comment.