inkhaven 1.2.14

Inkhaven — TUI literary work editor for Typst books
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Snapshot picker (F6), snapshot annotation (F5), snapshot
//! diff (V from picker), and split-snapshot accept/restore —
//! every App method that mutates the snapshot store or its
//! picker modal. Extracted from `tui::app` in the 1.2.7
//! refactor, Phase 3 batch 4.

use ratatui::style::{Color, Modifier, Style};
use tui_textarea::TextArea;
use uuid::Uuid;

use super::super::diff_utils::compute_line_diff;
use super::super::focus::Focus;
use super::super::input::TextInput;
use super::super::modal::Modal;
use super::super::session::ParagraphCursor;
use super::super::text_utils::body_to_lines;

impl super::App {

    /// Copy the currently-open paragraph's cursor + scroll into the
    /// in-memory `paragraph_cursors` map. Called on focus loss, on
    /// paragraph switch, and right before `save_session` writes to disk.
    /// No-op when no paragraph is open.
    pub(super) fn snapshot_open_paragraph_cursor(&mut self) {
        let Some(doc) = self.opened.as_ref() else {
            return;
        };
        let (row, col) = doc.textarea.cursor();
        self.paragraph_cursors.insert(
            doc.id,
            ParagraphCursor {
                cursor_row: row,
                cursor_col: col,
                scroll_row: doc.scroll_row,
                scroll_col: doc.scroll_col,
            },
        );
    }

    /// Replace the live buffer with the split snapshot and exit split mode.
    /// Used to "roll back" to the captured version after experimenting.
    pub(super) fn accept_split_snapshot(&mut self) {
        let Some(doc) = self.opened.as_mut() else {
            return;
        };
        let Some(split) = doc.split.take() else {
            self.status = "split is not open".into();
            return;
        };
        let lines = if split.snapshot_lines.is_empty() {
            vec![String::new()]
        } else {
            split.snapshot_lines
        };
        let mut new_ta = TextArea::new(lines);
        new_ta.set_cursor_line_style(Style::default().add_modifier(Modifier::REVERSED));
        new_ta.set_line_number_style(Style::default().fg(Color::DarkGray));
        doc.textarea = new_ta;
        doc.dirty = true;
        doc.scroll_row = 0;
        doc.scroll_col = 0;
        doc.last_activity = std::time::Instant::now();
        self.status =
            "split snapshot accepted — buffer replaced; Ctrl+S to commit · bold shows the diff"
                .into();
    }

    pub(super) fn create_snapshot_of_current(&mut self) {
        let Some(doc) = self.opened.as_ref() else {
            self.status = "no paragraph open".into();
            return;
        };
        let body = doc.textarea.lines().join("\n").into_bytes();
        let id = doc.id;
        let Some(node) = self.hierarchy.get(id).cloned() else {
            self.status = "node missing from hierarchy".into();
            return;
        };
        // 1.2.7+ — dedupe. If the latest existing snapshot for
        // this paragraph has identical content, skip the
        // annotation prompt entirely and stamp a "no changes"
        // status line. Stops F5 mashing from littering history
        // with duplicates AND keeps the annotation prompt
        // honest (no point annotating a no-op).
        if let Ok(snaps) = self.store.list_snapshots(id) {
            if let Some(latest) = snaps.first() {
                if let Ok(Some(prev)) =
                    self.store.snapshot_content(latest.id)
                {
                    if prev == body {
                        self.status = format!(
                            "snapshot: `{}` unchanged since the last snapshot — no new snapshot taken",
                            node.title
                        );
                        return;
                    }
                }
            }
        }
        // 1.2.6+ — pop an annotation prompt so the user can
        // jot a one-line note ("first complete draft", "before
        // the lighthouse rewrite"). Enter on empty input still
        // commits — keeps the F5 → Enter flow as fast as the
        // old one-keystroke path. Esc cancels.
        self.modal = Modal::SnapshotAnnotation {
            input: TextInput::new(),
            parent_id: id,
            parent_title: node.title,
            body,
        };
        self.status =
            "snapshot annotation: type a note (or Enter for no note) · Esc cancels".into();
    }

    /// Commit step for `Modal::SnapshotAnnotation` — invoked by
    /// the modal's Enter handler. Calls
    /// `Store::create_snapshot_annotated`, stamps the result on
    /// the status bar, and closes the modal.
    pub(super) fn commit_snapshot_annotation(
        &mut self,
        parent_id: Uuid,
        parent_title: &str,
        body: &[u8],
        annotation: &str,
    ) {
        let Some(node) = self.hierarchy.get(parent_id).cloned() else {
            self.status = "snapshot: paragraph vanished".into();
            return;
        };
        match self
            .store
            .create_snapshot_annotated(&node, body, annotation)
        {
            Ok(snap_id) => {
                let n_snaps = self
                    .store
                    .list_snapshots(parent_id)
                    .map(|v| v.len())
                    .unwrap_or(0);
                let note = if annotation.trim().is_empty() {
                    String::new()
                } else {
                    format!(" · `{annotation}`")
                };
                self.status = format!(
                    "snapshot {} of `{parent_title}` created ({} total){note} — F6 to view",
                    snap_id.simple(),
                    n_snaps,
                );
            }
            Err(e) => {
                self.status = format!("snapshot failed: {e}");
            }
        }
    }

    /// 1.2.11+ — snapshot the currently open
    /// paragraph with `annotation`, picking up the
    /// live editor body (not the on-disk file).
    /// Used by flows that need to label the
    /// pre-mutation state before they apply an
    /// AI rewrite — most notably the Ctrl+B Shift+M
    /// sentence-rhythm rewrite, which snapshots
    /// the unrewritten body annotated `Sentence
    /// rhythm rewrite` immediately before the
    /// rewrite lands.  No-op when no paragraph is
    /// open; failures land on the status bar.
    pub(super) fn snapshot_open_paragraph_with_annotation(
        &mut self,
        annotation: &str,
    ) {
        let Some(doc) = self.opened.as_ref() else {
            self.status =
                "snapshot: no paragraph open".into();
            return;
        };
        let parent_id = doc.id;
        let parent_title = doc.title.clone();
        let body = doc.textarea.lines().join("\n").into_bytes();
        self.commit_snapshot_annotation(
            parent_id,
            &parent_title,
            &body,
            annotation,
        );
    }

    pub(super) fn open_snapshot_picker(&mut self) {
        let Some(doc) = self.opened.as_ref() else {
            self.status = "no paragraph open".into();
            return;
        };
        let id = doc.id;
        let title = doc.title.clone();
        match self.store.list_snapshots(id) {
            Ok(snapshots) => {
                if snapshots.is_empty() {
                    self.status =
                        format!("no snapshots yet for `{title}` — press F5 to create one");
                    return;
                }
                // 1.2.8+ — reset the annotation filter; previous
                // session's filter shouldn't haunt a fresh picker.
                self.snapshot_filter.clear();
                self.snapshot_filter_focused = false;
                self.modal = Modal::SnapshotPicker {
                    paragraph_id: id,
                    paragraph_title: title,
                    snapshots,
                    cursor: 0,
                };
            }
            Err(e) => {
                self.status = format!("snapshot list failed: {e}");
            }
        }
    }

    /// 1.2.8+ — return indices into `snaps` that match the
    /// current annotation filter (case-insensitive substring
    /// against annotation text). Empty filter returns every
    /// index in order. The picker uses these via
    /// `visible[cursor]` to look up the absolute snapshot the
    /// cursor refers to.
    pub(super) fn visible_snapshot_indices(
        &self,
        snaps: &[crate::store::Snapshot],
    ) -> Vec<usize> {
        if self.snapshot_filter.is_empty() {
            return (0..snaps.len()).collect();
        }
        let needle = self.snapshot_filter.to_lowercase();
        snaps
            .iter()
            .enumerate()
            .filter(|(_, s)| s.annotation.to_lowercase().contains(&needle))
            .map(|(i, _)| i)
            .collect()
    }

    /// 1.2.12+ — Shift+Enter on a snapshot picker
    /// row: load the snapshot's body into the
    /// split-view `secondary` slot as a *read-only*
    /// historical view of the paragraph.  Unlocks
    /// the draft-vs-current comparison workflow:
    /// snapshot in the right pane, live buffer in
    /// the left, `Shift+F4` to view side-by-side,
    /// `F12` to fire `critique-compare`.
    ///
    /// The pinned secondary's `read_only` flag is
    /// `true` — the user can scroll and select but
    /// can't edit.  The title is prefixed with the
    /// snapshot's timestamp so the historical-vs-
    /// live distinction is visible at a glance in
    /// the split-view title bar.
    ///
    /// Closes the picker modal on success.  On
    /// error, leaves the modal up so the user can
    /// retry or pick a different row.
    pub(super) fn pin_snapshot_to_secondary(&mut self) {
        let (snap_id, when) = match &self.modal {
            Modal::SnapshotPicker {
                snapshots, cursor, ..
            } => {
                let visible = self.visible_snapshot_indices(snapshots);
                let Some(abs_idx) = visible.get(*cursor) else {
                    return;
                };
                let Some(snap) = snapshots.get(*abs_idx) else {
                    return;
                };
                (snap.id, snap.created_at)
            }
            _ => return,
        };
        let content = match self.store.snapshot_content(snap_id) {
            Ok(Some(bytes)) => bytes,
            Ok(None) => {
                self.status = "snapshot has no body — nothing to pin".into();
                return;
            }
            Err(e) => {
                self.status = format!("snapshot pin failed: {e}");
                return;
            }
        };
        let Some(primary) = self.opened.as_ref() else {
            self.status =
                "snapshot pin: no paragraph open to anchor against".into();
            return;
        };
        let primary_id = primary.id;
        let primary_title = primary.title.clone();
        let primary_rel = primary.rel_path.clone();
        let primary_content_type = primary.content_type.clone();
        let body = String::from_utf8_lossy(&content).into_owned();
        let lines = body_to_lines(&body);
        let saved_lines = lines.clone();
        let mut textarea = TextArea::new(lines);
        textarea.set_cursor_line_style(
            Style::default().add_modifier(Modifier::REVERSED),
        );
        textarea.set_line_number_style(Style::default().fg(Color::DarkGray));
        // Format the snapshot age compactly for the
        // title so the user sees "(snapshot · 2h
        // ago)" in the split-view chrome.
        let age = {
            let now = chrono::Utc::now();
            let delta = now.signed_duration_since(when);
            let secs = delta.num_seconds().max(0) as u64;
            super::super::text_utils::format_age_humantime(
                std::time::Duration::from_secs(secs),
            )
        };
        let snap_title = format!("{primary_title} (snapshot · {age})");
        self.secondary = Some(super::super::state::OpenedDoc {
            id: primary_id,
            title: snap_title,
            rel_path: primary_rel,
            textarea,
            dirty: false,
            scroll_row: 0,
            scroll_col: 0,
            block_anchor: None,
            last_activity: std::time::Instant::now(),
            saved_lines,
            // 1.2.14+ Phase C.1 — snapshots are
            // read-only; comments don't render or
            // mutate from this slot.
            comments: super::super::comments::CommentsFile::new(),
            loaded_mtime: None,
            split: None,
            search: None,
            // Read-only — historical snapshot is for
            // comparison, not editing.
            read_only: true,
            correction_baseline: None,
            content_type: primary_content_type,
            typst_diagnostics: Vec::new(),
            typst_diagnostics_checked_at: std::time::Instant::now(),
            typst_diag_last_fired: None,
            detected_language: None,
            detected_language_length: 0,
        });
        self.secondary_focused = false;
        self.modal = Modal::None;
        self.status = format!(
            "snapshot pinned to secondary (read-only · {age}) — Shift+F4 to view split",
        );
    }

    pub(super) fn commit_snapshot_load(&mut self) {
        // 1.2.8+ — when the annotation filter is non-empty,
        // `cursor` indexes the FILTERED visible list, not the
        // absolute snapshots Vec.  Translate before reading.
        let (snap_id, when) = match &self.modal {
            Modal::SnapshotPicker {
                snapshots, cursor, ..
            } => {
                let visible = self.visible_snapshot_indices(snapshots);
                let Some(abs_idx) = visible.get(*cursor) else {
                    self.modal = Modal::None;
                    return;
                };
                let Some(snap) = snapshots.get(*abs_idx) else {
                    self.modal = Modal::None;
                    return;
                };
                (snap.id, snap.created_at)
            }
            _ => return,
        };
        let content = match self.store.snapshot_content(snap_id) {
            Ok(Some(bytes)) => bytes,
            Ok(None) => {
                self.status = "snapshot has no body".into();
                self.modal = Modal::None;
                return;
            }
            Err(e) => {
                self.status = format!("snapshot load failed: {e}");
                self.modal = Modal::None;
                return;
            }
        };

        // Safety net (1.2.4+): before we replace the editor buffer,
        // snapshot whatever is currently in it. Without this, hitting
        // Enter on an old snapshot would silently discard any
        // unsaved typing — "oops! a day of work gone". Now the
        // recoverable history grows by one row instead.
        //
        // The pre-restore snapshot fires `hook.on_snapshot` like any
        // other snapshot. If snapshot creation itself fails, we
        // abort the load: the whole point is data safety, so doing
        // the replace without the safety net would defeat the
        // change. The user can fix the underlying error (disk full,
        // store offline) and retry.
        let pre_restore_id = if let Some(doc) = self.opened.as_ref() {
            let body_now = doc.textarea.lines().join("\n");
            let node = self.hierarchy.get(doc.id).cloned();
            match node {
                Some(n) => match self.store.create_snapshot(&n, body_now.as_bytes()) {
                    Ok(id) => Some(id),
                    Err(e) => {
                        self.status = format!(
                            "snapshot load aborted: safety snapshot failed ({e}) — retry once the store is healthy"
                        );
                        self.modal = Modal::None;
                        return;
                    }
                },
                None => None,
            }
        } else {
            None
        };

        let body = String::from_utf8_lossy(&content).into_owned();
        let Some(doc) = self.opened.as_mut() else {
            self.modal = Modal::None;
            return;
        };
        let mut new_textarea = TextArea::new(body_to_lines(&body));
        new_textarea.set_cursor_line_style(Style::default().add_modifier(Modifier::REVERSED));
        new_textarea.set_line_number_style(Style::default().fg(Color::DarkGray));
        doc.textarea = new_textarea;
        doc.dirty = true;
        doc.scroll_row = 0;
        doc.scroll_col = 0;
        doc.last_activity = std::time::Instant::now();
        // saved_lines stays at the previously-saved on-disk version, so the
        // snapshot text shows as "added" (bold) until the user accepts it
        // by hitting Ctrl+S.
        self.modal = Modal::None;
        self.change_focus(Focus::Editor);
        let safety_msg = match pre_restore_id {
            Some(id) => format!(" · safety snapshot {} created", id.simple()),
            None => String::new(),
        };
        self.status = format!(
            "loaded snapshot from {} — bold marks the change vs saved{}",
            when.with_timezone(&chrono::Local).format("%Y-%m-%d %H:%M:%S %z"),
            safety_msg,
        );
    }

    /// Open the snapshot-diff modal against the cursor's snapshot.
    /// Stashes the current `SnapshotPicker` modal inside the new
    /// variant so `Esc` returns to the picker rather than closing
    /// both layers.
    pub(super) fn open_snapshot_diff(&mut self) {
        let (snap_id, when, paragraph_title) = match &self.modal {
            Modal::SnapshotPicker {
                snapshots,
                cursor,
                paragraph_title,
                ..
            } => {
                // 1.2.8+ — translate visible cursor → absolute index.
                let visible = self.visible_snapshot_indices(snapshots);
                let Some(abs_idx) = visible.get(*cursor) else {
                    return;
                };
                let Some(snap) = snapshots.get(*abs_idx) else {
                    return;
                };
                (snap.id, snap.created_at, paragraph_title.clone())
            }
            _ => return,
        };
        let snapshot_bytes = match self.store.snapshot_content(snap_id) {
            Ok(Some(b)) => b,
            Ok(None) => {
                self.status = "snapshot has no body".into();
                return;
            }
            Err(e) => {
                self.status = format!("snapshot load failed: {e}");
                return;
            }
        };
        let snapshot_text = String::from_utf8_lossy(&snapshot_bytes).into_owned();
        let current_text = self
            .opened
            .as_ref()
            .map(|d| d.textarea.lines().join("\n"))
            .unwrap_or_default();
        let rows = compute_line_diff(&snapshot_text, &current_text);
        let when_str = when
            .with_timezone(&chrono::Local)
            .format("%Y-%m-%d %H:%M:%S %z")
            .to_string();
        let return_to = Box::new(std::mem::replace(&mut self.modal, Modal::None));
        self.modal = Modal::SnapshotDiff {
            paragraph_title,
            when: when_str,
            rows,
            scroll: 0,
            return_to,
        };
        self.status = "diff: snapshot ← left · current → right · ↑↓ scroll · Esc back".into();
    }

    pub(super) fn delete_current_snapshot(&mut self) {
        // 1.2.8+ — translate visible cursor → absolute index
        // via the annotation-filter helper, same as the load
        // path.
        let (snap_id, when, paragraph_id, paragraph_title) = match &self.modal {
            Modal::SnapshotPicker {
                snapshots,
                cursor,
                paragraph_id,
                paragraph_title,
            } => {
                let visible = self.visible_snapshot_indices(snapshots);
                let Some(abs_idx) = visible.get(*cursor) else {
                    return;
                };
                let Some(snap) = snapshots.get(*abs_idx).cloned() else {
                    return;
                };
                (snap.id, snap.created_at, *paragraph_id, paragraph_title.clone())
            }
            _ => return,
        };

        if let Err(e) = self.store.delete_snapshot(snap_id) {
            self.status = format!("delete snapshot failed: {e}");
            return;
        }

        let when_local = when
            .with_timezone(&chrono::Local)
            .format("%Y-%m-%d %H:%M:%S %z");

        match self.store.list_snapshots(paragraph_id) {
            Ok(snapshots) => {
                if snapshots.is_empty() {
                    self.modal = Modal::None;
                    self.status = format!(
                        "deleted snapshot {when_local} — no snapshots left for `{paragraph_title}`"
                    );
                } else {
                    // Keep the cursor on the same row index, clamped
                    // to the new (shorter) list — feels like "the row
                    // below the deleted one slid up".
                    let new_cursor = match &self.modal {
                        Modal::SnapshotPicker { cursor, .. } => {
                            (*cursor).min(snapshots.len() - 1)
                        }
                        _ => 0,
                    };
                    self.modal = Modal::SnapshotPicker {
                        paragraph_id,
                        paragraph_title,
                        snapshots,
                        cursor: new_cursor,
                    };
                    self.status = format!("deleted snapshot {when_local}");
                }
            }
            Err(e) => {
                self.modal = Modal::None;
                self.status =
                    format!("deleted snapshot, but couldn't refresh list: {e}");
            }
        }
    }

}