Skip to content

Commit

Permalink
Add [basename] label to fold argument
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg committed Mar 16, 2020
1 parent cd2068a commit c840cbb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/dune/dir_contents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ end = struct
| Group_root _ ->
acc
and walk_children ft_dir ~dir ~local acc =
File_tree.Dir.fold_sub_dirs ft_dir ~init:acc ~f:(fun name ft_dir acc ->
let dir = Path.Build.relative dir name in
let local = name :: local in
File_tree.Dir.fold_sub_dirs ft_dir ~init:acc
~f:(fun ~basename ft_dir acc ->
let dir = Path.Build.relative dir basename in
let local = basename :: local in
walk ft_dir ~dir ~local acc)
in
walk_children ft_dir ~dir ~local:[] []
Expand Down
2 changes: 1 addition & 1 deletion src/dune/dune_load.ml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ let load ~ancestor_vcs () =
in
let dune_files =
File_tree.Dir.fold_dune_files (File_tree.root ()) ~init:[]
~f:(fun _name dir dune_file dune_files ->
~f:(fun ~basename:_ dir dune_file dune_files ->
let path = File_tree.Dir.path dir in
let project = File_tree.Dir.project dir in
let dune_file = interpret ~dir:path ~project ~dune_file in
Expand Down
17 changes: 9 additions & 8 deletions src/dune/file_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -592,36 +592,37 @@ module Dir = struct
(Memo.Cell.get_sync s.sub_dir_as_t |> Option.value_exn).dir

let fold_sub_dirs (t : t) ~init ~f =
String.Map.foldi t.contents.sub_dirs ~init ~f:(fun name s acc ->
f name (sub_dir_as_t s) acc)
String.Map.foldi t.contents.sub_dirs ~init ~f:(fun basename s acc ->
f ~basename (sub_dir_as_t s) acc)

let fold_dune_files (type acc) t ~(init : acc) ~f =
let rec loop name dir (acc : acc) : acc =
let rec loop ~basename dir (acc : acc) : acc =
if status dir = Data_only then
acc
else
let init =
match dune_file dir with
| None -> acc
| Some dune_file -> f name dir dune_file acc
| Some dune_file -> f ~basename dir dune_file acc
in
fold_sub_dirs dir ~init ~f:(fun name -> loop (Some name))
fold_sub_dirs dir ~init ~f:(fun ~basename ->
loop ~basename:(Some basename))
in
let name =
let basename =
if Path.Source.is_root t.path then
None
else
Some (Path.Source.basename t.path)
in
loop name t init
loop ~basename t init

let rec fold t ~traverse ~init:acc ~f =
let must_traverse = Sub_dirs.Status.Map.find traverse t.status in
match must_traverse with
| false -> acc
| true ->
let acc = f t acc in
fold_sub_dirs t ~init:acc ~f:(fun _name t acc ->
fold_sub_dirs t ~init:acc ~f:(fun ~basename:_ t acc ->
fold t ~traverse ~init:acc ~f)
end

Expand Down
4 changes: 2 additions & 2 deletions src/dune/file_tree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ module Dir : sig

val file_paths : t -> Path.Source.Set.t

val fold_sub_dirs : t -> init:'a -> f:(string -> t -> 'a -> 'a) -> 'a
val fold_sub_dirs : t -> init:'a -> f:(basename:string -> t -> 'a -> 'a) -> 'a

val fold_dune_files :
t
-> init:'acc
-> f:(string option -> t -> Dune_file.t -> 'acc -> 'acc)
-> f:(basename:string option -> t -> Dune_file.t -> 'acc -> 'acc)
-> 'acc

val sub_dir_paths : t -> Path.Source.Set.t
Expand Down
3 changes: 2 additions & 1 deletion src/dune/utop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ let libs_under_dir sctx ~db ~dir =
(let open Option.O in
let* dir = Path.drop_build_context dir in
let+ dir = File_tree.find_dir dir in
File_tree.Dir.fold_dune_files dir ~init:[] ~f:(fun _name dir _dune_file acc ->
File_tree.Dir.fold_dune_files dir ~init:[]
~f:(fun ~basename:_ dir _dune_file acc ->
let dir =
Path.Build.append_source
(Super_context.build_dir sctx)
Expand Down

0 comments on commit c840cbb

Please sign in to comment.