Skip to content

Commit e85d1a8

Browse files
authored
[#35] Refactor file tree type (#52)
* Refactor * Refactor file tree type * Refactor variable names * Add motivation for storing file type and contents separately * Resolve merge conflict
1 parent a3a9213 commit e85d1a8

4 files changed

Lines changed: 42 additions & 23 deletions

File tree

lib/fs/fs.ml

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
module Filec = Filec
22

33
type tree =
4-
| File of string * Filec.t Lazy.t * Filec.file_type Lazy.t
5-
| Dir of string * tree array Lazy.t
4+
| File of {
5+
name : string;
6+
contents : Filec.t Lazy.t;
7+
file_type : Filec.file_type Lazy.t;
8+
}
9+
| Dir of {
10+
name : string;
11+
children : tree array Lazy.t;
12+
}
613

714
type dir_cursor = {
815
pos : int;
@@ -15,8 +22,8 @@ type cursor =
1522

1623
(* Extracts the file name from a tree node *)
1724
let file_name = function
18-
| File (name, _, _) -> name
19-
| Dir (name, _) -> name
25+
| File { name; _ } -> name
26+
| Dir { name; _ } -> name
2027

2128
(* A files comparison:
2229
@@ -30,10 +37,10 @@ let order_files t1 t2 =
3037
| _, _ -> String.compare (file_name t1) (file_name t2)
3138

3239
let rec sort_tree = function
33-
| File (name, contents, ft) -> File (name, contents, ft)
34-
| Dir (name, (lazy children)) ->
40+
| File _ as f -> f
41+
| Dir { name; children = (lazy children) } ->
3542
Array.sort order_files children;
36-
Dir (name, lazy (Array.map sort_tree children))
43+
Dir { name; children = lazy (Array.map sort_tree children) }
3744

3845
(* Recursively reads a directory tree *)
3946
let rec to_tree path =
@@ -44,13 +51,15 @@ let rec to_tree path =
4451
(fun child_name -> to_tree (Filename.concat path child_name))
4552
(Sys.readdir path))
4653
in
47-
let dirname = Filename.basename path in
48-
Dir (dirname, children)
54+
let name = Filename.basename path in
55+
Dir { name; children }
4956
else
5057
File
51-
( Filename.basename path,
52-
lazy (Filec.read path),
53-
lazy (Filec.type_of_path path) )
58+
{
59+
name = Filename.basename path;
60+
contents = lazy (Filec.read path);
61+
file_type = lazy (Filec.type_of_path path);
62+
}
5463

5564
let read_tree path = path |> to_tree |> sort_tree
5665
let file_at cursor = cursor.files.(cursor.pos)
@@ -100,12 +109,12 @@ let go_next zipper =
100109
| Dir_cursor cursor -> (
101110
let next = file_at cursor in
102111
match next with
103-
| File (_name, contents, _) ->
112+
| File { contents; _ } ->
104113
{
105114
parents = cursor :: zipper.parents;
106115
current = File_cursor (Lazy.force contents);
107116
}
108-
| Dir (_, (lazy next)) ->
117+
| Dir { children = (lazy next); _ } ->
109118
if Array.length next = 0 then zipper
110119
else
111120
{

lib/fs/fs.mli

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
and current offsets. *)
33
module Filec = Filec
44

5+
(* NOTE: contents and file_type are stored separately so we can know the file
6+
type and assign a proper icon without reading the contents *)
7+
58
(** A definition of a file tree. *)
69
type tree =
7-
| File of string * Filec.t Lazy.t * Filec.file_type Lazy.t
8-
| Dir of string * tree array Lazy.t
10+
| File of {
11+
name : string;
12+
contents : Filec.t Lazy.t;
13+
file_type : Filec.file_type Lazy.t;
14+
}
15+
| Dir of {
16+
name : string;
17+
children : tree array Lazy.t;
18+
}
919

1020
(** Return the name of a given tree node. *)
1121
val file_name : tree -> string

lib/tui/tui.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ let read_root_tree ~root_dir_path =
2929
let tree = Fs.read_tree root_dir_path in
3030
let files =
3131
match tree with
32-
| Fs.File (path, _, _) ->
33-
Printf.eprintf "Given path '%s' is not a directory!" path;
32+
| Fs.File { name; _ } ->
33+
Printf.eprintf "Given path '%s' is not a directory!" name;
3434
exit 1
35-
| Fs.Dir (_, files) -> files
35+
| Fs.Dir { children = files; _ } -> files
3636
in
3737
files
3838

lib/tui/widget/code.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ let max_file_name_len files =
5050
let fmt_file ~max_name_len (tree : Fs.tree) =
5151
let pad = Extra.String.fill_right max_name_len in
5252
match tree with
53-
| File (name, _, file_type) -> (
53+
| File { name; file_type; _ } -> (
5454
match Lazy.force file_type with
5555
| Fs.Filec.Text -> Pretty.Icon.file_char ^ " " ^ pad name
5656
| Fs.Filec.Binary -> Pretty.Icon.bin_char ^ " " ^ pad name)
57-
| Dir (name, (lazy children)) -> (
57+
| Dir { name; children = (lazy children) } -> (
5858
match children with
5959
| [||] -> Pretty.Icon.empty_dir_char ^ " " ^ pad name
6060
| _ -> Pretty.Icon.dir_char ^ " " ^ pad name)
@@ -189,8 +189,8 @@ let fs_to_view (fs : Fs.zipper) =
189189
| File_cursor contents, parent :: _ -> (parent, File_selected contents)
190190
| Dir_cursor cursor, _ -> (
191191
match Fs.file_at cursor with
192-
| File (_, contents, _) -> (cursor, File_selected (Lazy.force contents))
193-
| Dir (_, children) ->
192+
| File { contents; _ } -> (cursor, File_selected (Lazy.force contents))
193+
| Dir { children; _ } ->
194194
( cursor,
195195
Dir_selected
196196
{

0 commit comments

Comments
 (0)