From f5c33507a74d83692c028d1e1659d3506399138e Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 28 Jan 2024 23:42:12 +0100 Subject: feat: add non-linear change history with undo/redo --- lib/editorBuffer.ml | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'lib/editorBuffer.ml') diff --git a/lib/editorBuffer.ml b/lib/editorBuffer.ml index 959c04a..8aacd71 100644 --- a/lib/editorBuffer.ml +++ b/lib/editorBuffer.ml @@ -10,15 +10,17 @@ type buffer = { kind : kind; content : (char zipper zipper, error) Result.t; rendered : char Sequence.t zipper; + last_modified : float; } type t = buffer -let empty = +let empty () = { kind = No_name; content = empty |> push empty |> Result.return; rendered = push Sequence.empty empty; + last_modified = Unix.gettimeofday (); } let kind b = b.kind @@ -106,13 +108,14 @@ module Action = struct match b.content with | Error _ -> ((), b) | Ok c -> - let step = if before then left else right in + let rstep = if before then left else right ~by_one:false in + let cstep = if before then left else right ~by_one:false in let rec aux i r c = if i = 0 then r else let default = Sequence.empty in let l = apply_focus_or ~default (to_seq &> render) c in - let c' = step c and r' = swap_focus l r |> step in + let c' = cstep c and r' = swap_focus l r |> rstep in aux (i - 1) r' c' in ((), { b with rendered = aux n b.rendered c |> goto (left_length c) }) @@ -444,9 +447,11 @@ let from_file f = Sequence.(of_list lines |> map ~f:line_to_seq) with Unix.Unix_error (ENOENT, _, _) -> Sequence.empty in - let rendered = Sequence.map ~f:render lines |> of_seq in - let content = Sequence.map ~f:of_seq lines |> of_seq in - { kind = File f; content = Ok content; rendered } + let kind = File f + and content = Sequence.map ~f:of_seq lines |> of_seq |> Result.return + and rendered = Sequence.map ~f:render lines |> of_seq + and last_modified = Unix.gettimeofday () in + { kind; content; rendered; last_modified } let unrendered_view x y h w b = match b.content with -- cgit v1.2.3