Skip to content

Commit

Permalink
Improve a bit the break rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Jan 22, 2024
1 parent 9fa3287 commit 09513e0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
4 changes: 1 addition & 3 deletions lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1521,9 +1521,7 @@ end = struct
(* String literals using the heavy syntax are not simple. *)
| Pexp_constant {pconst_desc= Pconst_string (_, _, Some _); _} -> false
(* Only strings fitting on the line are simple. *)
| Pexp_constant {pconst_desc= Pconst_string (_, loc, None); _} ->
Exp.is_trivial exp
|| (Location.height loc = 1 && fit_margin c (width xexp))
| Pexp_constant {pconst_desc= Pconst_string (_, _, None); _} -> true
| Pexp_constant _ -> true
| Pexp_field _ | Pexp_ident _ | Pexp_send _
|Pexp_construct (_, None)
Expand Down
4 changes: 2 additions & 2 deletions lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ let rec place t loc_tree ?prev_loc ?deep_loc locs cmts =
(** Relocate comments, for Ast transformations such as sugaring. *)
let relocate (t : t) ~src ~before ~after =
if t.debug then
Format.eprintf "relocate %a to %a and %a@\n%!" Location.fmt src
Location.fmt before Location.fmt after ;
Format.eprintf "relocate %a to %a and %a@\n%!"
Location.fmt src Location.fmt before Location.fmt after ;
let merge_and_sort x y =
List.rev_append x y
|> List.sort ~compare:(Comparable.lift Location.compare_start ~f:Cmt.loc)
Expand Down
4 changes: 2 additions & 2 deletions lib/Conf_decl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ let rec pp_from fs = function
| `Profile (s, p) -> Format.fprintf fs " (profile %s%a)" s pp_from_src p
| `Updated (x, None) -> pp_from_src fs x
| `Updated (x, Some r) ->
Format.fprintf fs "%a -- Warning (redundant): %a" pp_from_src x pp_from
r
Format.fprintf fs "%a -- Warning (redundant): %a"
pp_from_src x pp_from r

let loc_udapted_from = function
| `Commandline -> Location.in_file "<command-line>"
Expand Down
7 changes: 4 additions & 3 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1520,13 +1520,14 @@ and fmt_args_grouped ?epi:(global_epi = noop) c ctx args =
let xexp = sub_exp ~ctx x in
is_simple c.conf (expression_width c) xexp
in
let should_break_after x = not (is_simple x)
and should_break_before ((_lbl, exp) as y) =
let should_break_before x = not (is_simple x)
and should_break_after ((_lbl, exp) as y) =
match exp.pexp_desc with
(* Heavy syntax strings are not grouped. *)
| Pexp_constant {pconst_desc= Pconst_string (_, _, Some _); _} -> true
(* Non-simple strings are grouped but end a group. *)
| Pexp_constant {pconst_desc= Pconst_string (_, _, None); _} -> false
| Pexp_constant {pconst_desc= Pconst_string (str, _, None); _} ->
String.length str * 3 > c.conf.fmt_opts.margin.v
| _ -> not (is_simple y)
in
let break x y =
Expand Down
4 changes: 2 additions & 2 deletions test/passing/tests/infix_arg_grouping.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ vbox 1
;;

user_error
( "version mismatch: .ocamlformat requested "
^ value ^ " but version is " ^ Version.version )
( "version mismatch: .ocamlformat requested " ^ value ^ " but version is "
^ Version.version )
;;

hvbox 1
Expand Down

0 comments on commit 09513e0

Please sign in to comment.