Skip to content

Commit

Permalink
Trim empty lines upfront
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow committed Aug 20, 2019
1 parent a21f0fa commit 95deb88
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
10 changes: 1 addition & 9 deletions bin/test/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,6 @@ let run_toplevel_tests ?root c ppf tests t =
) tests;
Block.pp_footer ppf ()

let trim l =
let rec trim_front = function
| ""::t -> trim_front t
| l -> l
in
let rev_trim l = trim_front (List.rev (trim_front l)) in
rev_trim (List.rev_map String.trim l)

type file = { first: Mdx_top.Part.file; current: Mdx_top.Part.file }

let files: (string, file) Hashtbl.t = Hashtbl.create 8
Expand Down Expand Up @@ -227,8 +219,8 @@ let update_block_with_file ppf t file part =
(match part with None -> "" | Some p -> p)
file
| Some lines ->
let lines = trim lines in
let contents = String.concat "\n" lines in
let contents = String.trim contents in
Output.pp ppf (`Output contents);
Block.pp_footer ppf ()

Expand Down
7 changes: 5 additions & 2 deletions lib/top/part.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ let rec remove_empty_heads = function
| "" :: tl -> remove_empty_heads tl
| l -> l

let trim_empty_rev l =
remove_empty_heads (List.rev (remove_empty_heads l))

module Parse_parts =
struct

Expand All @@ -47,8 +50,8 @@ struct
ws; str "]"; ws; opt (str ";;"); ws;
]

let next_part ~name ~sep_indent = fun lines ->
let body = String.concat "\n" (List.rev (remove_empty_heads lines)) in
let next_part ~name ~sep_indent = fun lines_rev ->
let body = String.concat "\n" (trim_empty_rev lines_rev) in
Part.v ~name ~sep_indent ~body

let next_part_of_groups groups =
Expand Down
1 change: 0 additions & 1 deletion test/sync_to_ml.ml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ let f = "hello world!"

[@@@part "42"] ;;


let () =
f x print_int

0 comments on commit 95deb88

Please sign in to comment.