Skip to content

Commit

Permalink
Fmt: optimize fmt for let_binder, exps, if_stmt ...
Browse files Browse the repository at this point in the history
- new module
  - WrapChecker: check if `chunk[s]` must wrap
- new function
  - doc_chunks_nowrap_or_surround: check nowrap, if not, then surround
  - doc_chunks_rhs: check wrap, if true then prefix hardline
- format rule updates
  - if_stmt will be prefix a hardline and nested if wrap
  - force foreach wrap
  • Loading branch information
trdthg committed Aug 12, 2024
1 parent 561df10 commit 9ac6d52
Show file tree
Hide file tree
Showing 17 changed files with 1,584 additions and 215 deletions.
8 changes: 8 additions & 0 deletions sail_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"fmt": {
"indent": 2,
"preserve_structure": false,
"line_width": 120,
"ribbon_width": 1.0
}
}
47 changes: 42 additions & 5 deletions src/lib/chunk_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,14 @@ let rec prerr_chunk indent = function
Queue.iter (prerr_chunk (indent ^ " ")) arg
)
[("vars", ex.vars); ("constr", ex.constr); ("typ", ex.typ)]
| Binder _ -> ()
| Binder (binder, x, y, z) ->
Printf.eprintf "%sBinder:%s\n" indent (binder_keyword binder);
List.iteri
(fun i arg ->
Printf.eprintf "%s %d:\n" indent i;
Queue.iter (prerr_chunk (indent ^ " ")) arg
)
[x; y; z]
| Block_binder (binder, binding, exp) ->
Printf.eprintf "%sBlock_binder:%s\n" indent (binder_keyword binder);
List.iter
Expand Down Expand Up @@ -387,7 +394,11 @@ let rec prerr_chunk indent = function
Queue.iter (prerr_chunk (indent ^ " ")) exp;
Printf.eprintf "%s with:" indent;
List.iter (fun exp -> Queue.iter (prerr_chunk (indent ^ " ")) exp) exps
| Vector_updates (_exp, _updates) -> Printf.eprintf "%sVector_updates:\n" indent
| Vector_updates (exp, updates) ->
Printf.eprintf "%sVector_updates:\n" indent;
Queue.iter (prerr_chunk (indent ^ " ")) exp;
Printf.eprintf "%s with:\n" indent;
List.iter (prerr_chunk (indent ^ " ")) updates
| Index (exp, ix) ->
Printf.eprintf "%sIndex:\n" indent;
List.iter
Expand All @@ -412,7 +423,14 @@ let rec pop_header_comments comments chunks l lnum =
| Some (s, _) when e.pos_cnum < s.pos_cnum && comment_s.pos_lnum = lnum ->
let _ = Stack.pop comments in
Queue.add
(Comment (comment_type, 0, comment_s.pos_cnum - comment_s.pos_bol, contents, e.pos_lnum == lnum))
(Comment
( comment_type,
0,
comment_s.pos_cnum - comment_s.pos_bol,
contents,
comment_type = Lexer.Comment_line && e.pos_lnum == lnum
)
)
chunks;
Queue.add (Spacer (true, 1)) chunks;
pop_header_comments comments chunks l (lnum + 1)
Expand All @@ -433,7 +451,12 @@ let rec pop_comments ?(spacer = true) comments chunks l =
let _ = Stack.pop comments in
Queue.add
(Comment
(comment_type, 0, comment_s.pos_cnum - comment_s.pos_bol, contents, comment_s.pos_lnum == e.pos_lnum)
( comment_type,
0,
comment_s.pos_cnum - comment_s.pos_bol,
contents,
comment_type = Lexer.Comment_line && comment_s.pos_lnum == e.pos_lnum
)
)
chunks;
if spacer && comment_e.pos_lnum < s.pos_lnum then Queue.add (Spacer (true, 1)) chunks;
Expand All @@ -450,7 +473,12 @@ let rec pop_comments_until_loc_end comments chunks l =
let _ = Stack.pop comments in
Queue.add
(Comment
(comment_type, 0, comment_s.pos_cnum - comment_s.pos_bol, contents, comment_s.pos_lnum == e.pos_lnum)
( comment_type,
0,
comment_s.pos_cnum - comment_s.pos_bol,
contents,
comment_type = Lexer.Comment_line && comment_s.pos_lnum == e.pos_lnum
)
)
chunks;
pop_comments_until_loc_end comments chunks l
Expand Down Expand Up @@ -909,6 +937,7 @@ let rec chunk_exp comments chunks (E_aux (aux, l)) =
in
Queue.add (Block (true, block_chunks)) chunks
| (E_let (LB_aux (LB_val (pat, exp), _), body) | E_internal_plet (pat, exp, body)) as binder ->
(* there need a way to find position of '=' *)
let binder =
match binder with
| E_let _ -> Let_binder
Expand Down Expand Up @@ -944,6 +973,14 @@ let rec chunk_exp comments chunks (E_aux (aux, l)) =
let i_chunks = rec_chunk_exp i in
pop_comments ~spacer:false comments i_chunks keywords.then_loc;
let t_chunks = rec_chunk_exp t in
if if_format.then_brace then ignore (pop_comments_until_loc_end comments t_chunks keywords.then_loc);
(*
no place to put comment between then_end and else_start
if foo
then {} /* comment */
else {}
*)
ignore (pop_trailing_comment comments t_chunks (ending_line_num keywords.then_loc));
(match keywords.else_loc with Some l -> pop_comments comments t_chunks l | None -> ());
let e_chunks = rec_chunk_exp e in
Expand Down
Loading

0 comments on commit 9ac6d52

Please sign in to comment.