Source file session.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(*---------------------------------------------------------------------------
  Copyright (c) 2026 The Raven authors. All rights reserved.
  SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

module Id_map = Map.Make (String)

(* ───── Types ───── *)

type cell_status = Idle | Queued | Running

(* ───── History ───── *)

type history = {
  past : Doc.t list;
  future : Doc.t list;
  count : int;
  capacity : int;
}

let empty_history capacity = { past = []; future = []; count = 0; capacity }

let take n xs =
  let rec loop acc n = function
    | [] -> List.rev acc
    | _ when n <= 0 -> List.rev acc
    | x :: rest -> loop (x :: acc) (n - 1) rest
  in
  loop [] n xs

let push_history doc h =
  let past = doc :: h.past in
  if h.count >= h.capacity then
    { h with past = take h.capacity past; future = []; count = h.capacity }
  else { h with past; future = []; count = h.count + 1 }

(* ───── Session ───── *)

type t = {
  doc : Doc.t;
  last_checkpoint : Doc.t;
  statuses : cell_status Id_map.t;
  history : history;
}

let create ?(history_capacity = 100) doc =
  {
    doc;
    last_checkpoint = doc;
    statuses = Id_map.empty;
    history = empty_history history_capacity;
  }

let doc s = s.doc

let cell_status id s =
  match Id_map.find_opt id s.statuses with Some st -> st | None -> Idle

let can_undo s = s.history.past <> []
let can_redo s = s.history.future <> []

(* ───── Document operations ───── *)

let update_source cell_id source s =
  match Doc.find cell_id s.doc with
  | Some c ->
      let doc = Doc.replace cell_id (Cell.set_source source c) s.doc in
      { s with doc }
  | None -> s

let checkpoint s =
  if s.doc == s.last_checkpoint then s
  else
    let history = push_history s.last_checkpoint s.history in
    { s with last_checkpoint = s.doc; history }

let with_history_push f s =
  let s = checkpoint s in
  let history = push_history s.doc s.history in
  let doc = f s.doc in
  { s with doc; last_checkpoint = doc; history }

let insert_cell ~pos cell s = with_history_push (Doc.insert ~pos cell) s
let remove_cell cell_id s = with_history_push (Doc.remove cell_id) s
let move_cell cell_id ~pos s = with_history_push (Doc.move cell_id ~pos) s

let clear_outputs cell_id s =
  let doc = Doc.update cell_id Cell.clear_outputs s.doc in
  { s with doc }

let clear_all_outputs s =
  let doc = Doc.clear_all_outputs s.doc in
  { s with doc }

let set_cell_kind cell_id kind s =
  match Doc.find cell_id s.doc with
  | Some c ->
      with_history_push
        (fun doc ->
          let src = Cell.source c in
          let id = Cell.id c in
          let attrs = Cell.attrs c in
          let c' =
            match kind with
            | `Code -> Cell.code ~id ~attrs src
            | `Text -> Cell.text ~id ~attrs src
          in
          Doc.replace cell_id c' doc)
        s
  | None -> s

let set_cell_attrs cell_id attrs s =
  match Doc.find cell_id s.doc with
  | Some c ->
      with_history_push
        (fun doc -> Doc.replace cell_id (Cell.set_attrs attrs c) doc)
        s
  | None -> s

(* ───── Execution state ───── *)

let mark_running cell_id s =
  { s with statuses = Id_map.add cell_id Running s.statuses }

let mark_queued cell_id s =
  { s with statuses = Id_map.add cell_id Queued s.statuses }

let mark_idle cell_id s =
  { s with statuses = Id_map.add cell_id Idle s.statuses }

let apply_output cell_id output s =
  let doc = Doc.update cell_id (Cell.append_output output) s.doc in
  { s with doc }

let finish_execution cell_id ~success:_ s =
  let doc = Doc.update cell_id Cell.increment_execution_count s.doc in
  { s with doc; statuses = Id_map.add cell_id Idle s.statuses }

(* ───── History ───── *)

let undo s =
  match s.history.past with
  | prev :: rest ->
      let history =
        {
          s.history with
          past = rest;
          future = s.doc :: s.history.future;
          count = s.history.count - 1;
        }
      in
      { s with doc = prev; last_checkpoint = prev; history }
  | [] -> s

let redo s =
  match s.history.future with
  | next :: rest ->
      let history =
        {
          s.history with
          past = s.doc :: s.history.past;
          future = rest;
          count = s.history.count + 1;
        }
      in
      { s with doc = next; last_checkpoint = next; history }
  | [] -> s

(* ───── Reload ───── *)

let reload doc s =
  {
    doc;
    last_checkpoint = doc;
    statuses = Id_map.empty;
    history = empty_history s.history.capacity;
  }