Skip to content

Commit 7561197

Browse files
authored
Fix indentation after if .. then (* cmt *) begin (#2785)
This reduce the indentation of the `begin` keyword in this case: let () = if true then (* a *) begin () end else () and fixes an instability issue when the comment was big enough.
1 parent ae69c1c commit 7561197

47 files changed

Lines changed: 152 additions & 53 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/Fmt_ast.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2564,8 +2564,15 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
25642564
Some (Cmts.fmt_after c keyword_loc)
25652565
else None
25662566
in
2567+
let cmts_before_opt loc =
2568+
if Cmts.has_before c.cmts loc then
2569+
Some
2570+
(Cmts.fmt_before ~pro:noop ~epi:noop ~eol:noop
2571+
c loc )
2572+
else None
2573+
in
25672574
let p =
2568-
Params.get_if_then_else c.conf
2575+
Params.get_if_then_else c.conf ~cmts_before_opt
25692576
~pro:(fmt_if first pro_inner) ~first ~last
25702577
~parens_bch ~parens_prev_bch:!parens_prev_bch
25712578
~xcond ~xbch ~expr_loc:pexp_loc

lib/Params.ml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,8 @@ type if_then_else =
847847
; break_end_branch: Fmt.t
848848
; space_between_branches: Fmt.t }
849849

850-
let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
851-
~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs
850+
let get_if_then_else (c : Conf.t) ~cmts_before_opt ~pro ~first ~last
851+
~parens_bch ~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs
852852
~infix_ext_attrs ~fmt_cond ~cmts_before_kw ~cmts_after_kw =
853853
let imd = c.fmt_opts.indicate_multiline_delimiters.v in
854854
let beginend_loc, infix_ext_attrs_beginend, branch_expr =
@@ -889,10 +889,10 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
889889
| `Closing_on_separate_line ->
890890
wrap (brk (0, opn_hint_indent)) (brk ch_sl)
891891
in
892-
let cond () =
892+
let cond ?(box = hvbox 2) () =
893893
match xcond with
894894
| Some xcnd ->
895-
hvbox 2
895+
box
896896
( hvbox 0
897897
( hvbox 2
898898
( pro
@@ -902,12 +902,21 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
902902
$ space_break $ cmts_before_kw $ str "then" )
903903
$ opt cmts_after_kw Fn.id )
904904
| None ->
905-
cmts_before_kw $ hvbox 2 (pro $ str "else" $ opt cmts_after_kw Fn.id)
905+
cmts_before_kw $ box (pro $ str "else" $ opt cmts_after_kw Fn.id)
906906
in
907-
let branch_pro ?(indent = 2) () =
908-
if Option.is_some cmts_after_kw then break 1000 indent
909-
else if has_beginend || parens_bch then str " "
910-
else break 1 indent
907+
let has_cmts_after_kw = Option.is_some cmts_after_kw in
908+
let branch_pro ?(begin_end_offset = 2) ?(indent = 2)
909+
?(break_before_cmts = break 1000 indent) () =
910+
match beginend_loc with
911+
| Some loc -> (
912+
match cmts_before_opt loc with
913+
| Some cmts -> break_before_cmts $ cmts $ break 1000 ~-begin_end_offset
914+
| None ->
915+
if has_cmts_after_kw then break 1000 ~-begin_end_offset
916+
else str " " )
917+
| None when has_cmts_after_kw -> break 1000 indent
918+
| None when parens_bch -> str " "
919+
| None -> break 1 indent
911920
in
912921
match c.fmt_opts.if_then_else.v with
913922
| `Compact ->
@@ -931,7 +940,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
931940
{ box_branch= Fn.id
932941
; cond= cond ()
933942
; box_keyword_and_expr= Fn.id
934-
; branch_pro= branch_pro ()
943+
; branch_pro= branch_pro ~begin_end_offset:0 ()
935944
; wrap_parens= wrap_parens ~wrap_breaks:(wrap (break 1000 2) noop)
936945
; beginend_loc
937946
; box_expr= Some has_beginend
@@ -950,7 +959,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
950959
| _ -> 0 )
951960
; cond= cond ()
952961
; box_keyword_and_expr= Fn.id
953-
; branch_pro= branch_pro ()
962+
; branch_pro= branch_pro ~begin_end_offset:0 ()
954963
; wrap_parens=
955964
wrap_parens
956965
~wrap_breaks:
@@ -975,7 +984,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
975984
{ box_branch= Fn.id
976985
; cond= cond ()
977986
; box_keyword_and_expr= Fn.id
978-
; branch_pro= branch_pro ()
987+
; branch_pro= branch_pro ~begin_end_offset:0 ()
979988
; wrap_parens=
980989
wrap_parens
981990
~wrap_breaks:
@@ -1010,7 +1019,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
10101019
{ box_branch= Fn.id
10111020
; cond
10121021
; box_keyword_and_expr= (fun k -> hovbox 2 (keyword $ k))
1013-
; branch_pro= branch_pro ~indent:0 ()
1022+
; branch_pro= branch_pro ~break_before_cmts:(str " ") ~indent:0 ()
10141023
; wrap_parens=
10151024
wrap_parens
10161025
~wrap_breaks:

lib/Params.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ type if_then_else =
220220

221221
val get_if_then_else :
222222
Conf.t
223+
-> cmts_before_opt:(Location.t -> Fmt.t option)
223224
-> pro:Fmt.t
224225
-> first:bool
225226
-> last:bool
@@ -234,6 +235,7 @@ val get_if_then_else :
234235
-> cmts_before_kw:Fmt.t
235236
-> cmts_after_kw:Fmt.t option
236237
-> if_then_else
238+
(** [cmts_before_opt] return the comment before the given location with no breaks around it. *)
237239

238240
val match_indent : ?default:int -> Conf.t -> parens:bool -> ctx:Ast.t -> int
239241
(** [match_indent c ~ctx ~default] returns the indentation used for the

test/passing/refs.ahrefs/comment_breaking.ml.ref

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,20 @@ let () =
66
(* this comment should not change breaking of the following line *)
77
foo aaaaaaaaaa bbbbbbbbbb cccccccccc |> (ignore : t -> _);
88
bar dddddddddd eeeeeeeeee ffffffffff |> (ignore : t -> _)
9+
10+
type pp_token =
11+
| Pp_if_newline
12+
(* to do something only if this very
13+
line has been broken *)
14+
| Pp_open_tag of stag (* opening a tag name *)
15+
| Pp_close_tag (* closing the most recently open tag *)
16+
17+
let format_pp_token state size = function
18+
| Pp_begin (off, ty) ->
19+
let insertion_point = state.pp_margin - state.pp_space_left in
20+
if insertion_point > state.pp_max_indent then
21+
(* can not open a box right there. *)
22+
begin
23+
pp_force_break_line state
24+
end;
25+
Stack.push { box_type; width } state.pp_format_stack

test/passing/refs.ahrefs/ite-compact.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ let _ = if x then 42 (* dummy *) else if y then z else w
178178

179179
let () =
180180
if true then (* a *)
181-
begin
181+
begin
182182
()
183183
end
184184
else ()

test/passing/refs.ahrefs/ite-compact_closing.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ let _ = if x then 42 (* dummy *) else if y then z else w
195195

196196
let () =
197197
if true then (* a *)
198-
begin
198+
begin
199199
()
200200
end
201201
else ()

test/passing/refs.ahrefs/ite-fit_or_vertical.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ let _ = if x then 42 (* dummy *) else if y then z else w
213213

214214
let () =
215215
if true then (* a *)
216-
begin
216+
begin
217217
()
218218
end
219219
else

test/passing/refs.ahrefs/ite-fit_or_vertical_closing.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ let _ = if x then 42 (* dummy *) else if y then z else w
223223

224224
let () =
225225
if true then (* a *)
226-
begin
226+
begin
227227
()
228228
end else
229229
()

test/passing/refs.ahrefs/ite-fit_or_vertical_no_indicate.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ let _ = if x then 42 (* dummy *) else if y then z else w
213213

214214
let () =
215215
if true then (* a *)
216-
begin
216+
begin
217217
()
218218
end
219219
else

test/passing/refs.ahrefs/ite-kr.ml.ref

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ let _ =
252252

253253
let () =
254254
if true then (* a *)
255-
begin
255+
begin
256256
()
257257
end else
258258
()

0 commit comments

Comments
 (0)