Simplify file cursor type

This commit is contained in:
Dmitrii Kovanikov 2024-06-23 13:42:54 +01:00
parent 200cb4665b
commit 6cc0915c77
3 changed files with 9 additions and 13 deletions

View File

@ -64,7 +64,7 @@ type dir_cursor = {
type cursor =
| Dir_cursor of dir_cursor
| File_cursor of string * file_contents
| File_cursor of file_contents
let file_at cursor = cursor.files.(cursor.pos)
@ -89,11 +89,9 @@ let go_down zipper =
let new_pos = (cursor.pos + 1) mod len in
let new_cursor = Dir_cursor { cursor with pos = new_pos } in
{ zipper with current = new_cursor }
| File_cursor (name, cursor) ->
| File_cursor cursor ->
let new_offset = cursor.offset + 1 in
let new_cursor =
File_cursor (name, { cursor with offset = new_offset })
in
let new_cursor = File_cursor { cursor with offset = new_offset } in
let len = Array.length cursor.lines in
if new_offset + span > len then zipper
else { zipper with current = new_cursor }
@ -105,11 +103,9 @@ let go_up zipper =
let new_pos = (cursor.pos + len - 1) mod len in
let new_cursor = Dir_cursor { cursor with pos = new_pos } in
{ zipper with current = new_cursor }
| File_cursor (name, cursor) ->
| File_cursor cursor ->
let new_offset = max 0 (cursor.offset - 1) in
let new_cursor =
File_cursor (name, { cursor with offset = new_offset })
in
let new_cursor = File_cursor { cursor with offset = new_offset } in
{ zipper with current = new_cursor }
let go_next zipper =
@ -118,10 +114,10 @@ let go_next zipper =
| Dir_cursor cursor -> (
let next = file_at cursor in
match next with
| File (name, contents) ->
| File (_name, contents) ->
{
parents = cursor :: zipper.parents;
current = File_cursor (name, Lazy.force contents);
current = File_cursor (Lazy.force contents);
}
| Dir (_, next) ->
if Array.length next = 0 then zipper

View File

@ -23,7 +23,7 @@ type dir_cursor = {
type cursor =
| Dir_cursor of dir_cursor
| File_cursor of string * file_contents
| File_cursor of file_contents
(** Return the currently selected file in file cursor. *)
val file_at : dir_cursor -> tree

View File

@ -231,7 +231,7 @@ let fs_to_view (fs : Fs.zipper) =
| File_cursor _, [] ->
failwith
"Error during rendering! Impossible to have a file without a parent"
| File_cursor (_, contents), parent :: _ -> (parent, File_selected contents)
| File_cursor contents, parent :: _ -> (parent, File_selected contents)
| Dir_cursor cursor, _ -> (
match Fs.file_at cursor with
| File (_, contents) -> (cursor, File_selected (Lazy.force contents))