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
  - NoWrapChecker: check if chunk[s] can be nowrap
- new function
  - doc_chunks_nowrap_or_surround: check nowrap, if not, then surround
  - doc_chunks_nowrap_or_prefix: check nowrap, if not then prefix hardline
- new feature
   - allow Field, Unary, Infix_sequence, Binary, Index, App, Struct_update, Vector_update, Tuple nowrap
  - allow nowrap on nested chunks
  - allow no prefix hardline before binary rhs of block chunk
  - allow toplevel block wrap
  • Loading branch information
trdthg committed Jul 30, 2024
1 parent 561df10 commit 2bb72e7
Show file tree
Hide file tree
Showing 13 changed files with 1,018 additions and 70 deletions.
24 changes: 22 additions & 2 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 Down Expand Up @@ -909,6 +920,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 +956,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 2bb72e7

Please sign in to comment.