-
-
Notifications
You must be signed in to change notification settings - Fork 15
Refactor file tree type #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6795c8b
b59c4fe
a083fea
45ca299
59f3d0e
daf978f
5d75755
18e735a
f9a6295
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,15 @@ | ||
| module Filec = Filec | ||
|
|
||
| type tree = | ||
| | File of string * Filec.t Lazy.t * Filec.file_type Lazy.t | ||
| | Dir of string * tree array Lazy.t | ||
| | File of { | ||
| name : string; | ||
| contents : Filec.t Lazy.t; | ||
| file_type : Filec.file_type Lazy.t; | ||
| } | ||
| | Dir of { | ||
| name : string; | ||
| children : tree array Lazy.t; | ||
| } | ||
|
|
||
| type dir_cursor = { | ||
| pos : int; | ||
|
|
@@ -15,8 +22,8 @@ type cursor = | |
|
|
||
| (* Extracts the file name from a tree node *) | ||
| let file_name = function | ||
| | File (name, _, _) -> name | ||
| | Dir (name, _) -> name | ||
| | File { name; _ } -> name | ||
| | Dir { name; _ } -> name | ||
|
|
||
| (* A files comparison: | ||
|
|
||
|
|
@@ -30,10 +37,10 @@ let order_files t1 t2 = | |
| | _, _ -> String.compare (file_name t1) (file_name t2) | ||
|
|
||
| let rec sort_tree = function | ||
| | File (name, contents, ft) -> File (name, contents, ft) | ||
| | Dir (name, (lazy children)) -> | ||
| | File _ as f -> f | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice usage of alias patterns 💯 |
||
| | Dir { name; children = (lazy children) } -> | ||
| Array.sort order_files children; | ||
| Dir (name, lazy (Array.map sort_tree children)) | ||
| Dir { name; children = lazy (Array.map sort_tree children) } | ||
|
|
||
| (* Recursively reads a directory tree *) | ||
| let rec to_tree path = | ||
|
|
@@ -44,13 +51,15 @@ let rec to_tree path = | |
| (fun child_name -> to_tree (Filename.concat path child_name)) | ||
| (Sys.readdir path)) | ||
| in | ||
| let dirname = Filename.basename path in | ||
| Dir (dirname, children) | ||
| let name = Filename.basename path in | ||
| Dir { name; children } | ||
| else | ||
| File | ||
| ( Filename.basename path, | ||
| lazy (Filec.read path), | ||
| lazy (Filec.type_of_path path) ) | ||
| { | ||
| name = Filename.basename path; | ||
| contents = lazy (Filec.read path); | ||
| file_type = lazy (Filec.type_of_path path); | ||
| } | ||
|
|
||
| let read_tree path = path |> to_tree |> sort_tree | ||
| let file_at cursor = cursor.files.(cursor.pos) | ||
|
|
@@ -100,12 +109,12 @@ let go_next zipper = | |
| | Dir_cursor cursor -> ( | ||
| let next = file_at cursor in | ||
| match next with | ||
| | File (_name, contents, _) -> | ||
| | File { contents; _ } -> | ||
| { | ||
| parents = cursor :: zipper.parents; | ||
| current = File_cursor (Lazy.force contents); | ||
| } | ||
| | Dir (_, (lazy next)) -> | ||
| | Dir { children = (lazy next); _ } -> | ||
| if Array.length next = 0 then zipper | ||
| else | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,20 @@ | |
| and current offsets. *) | ||
| module Filec = Filec | ||
|
|
||
| (* NOTE: contents and file_type are stored separately so we can know the file | ||
| type and assign a proper icon without reading the contents *) | ||
|
|
||
| (** A definition of a file tree. *) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One minor addition while we're on this: let's improve docs by adding a note saying something like that Just a note for future selves so we don't forget the motivation 😅 |
||
| type tree = | ||
| | File of string * Filec.t Lazy.t * Filec.file_type Lazy.t | ||
| | Dir of string * tree array Lazy.t | ||
| | File of { | ||
| name : string; | ||
| contents : Filec.t Lazy.t; | ||
| file_type : Filec.file_type Lazy.t; | ||
| } | ||
| | Dir of { | ||
| name : string; | ||
| children : tree array Lazy.t; | ||
| } | ||
|
|
||
| (** Return the name of a given tree node. *) | ||
| val file_name : tree -> string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually the main motivation behind doing this minor refactoring.
The internal contents of constructors like
FileandDirmight change by adding and removing some fields (I already expect a few possible changes). But these changes will be decoupled from functions that don't affect them, so it'll be easier to perform them.Even such minor things matter when a project grows.