plannotator-tui 0.9.1

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! Annotations for one document, saved automatically on every change.
//!
//! The record is `annotations.json` under the Plannotator data directory, keyed the way
//! Plannotator keys files (`plannotator_tui_schema::annotations_dir`). It holds
//! `plannotator_tui_schema::Annotation` values — the Workspaces wire shape — so a local record
//! and a server row are interchangeable. Resolution against the current source happens on
//! load; an annotation whose text is gone is kept as an orphan.
//!
//! A phase-2 sidecar (`<file>.annotations.json` next to the document) is imported once and
//! left alone; nothing is written next to the document any more.

use std::ops::Range;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result};
use plannotator_tui_schema::{Anchor, Annotation, Kind, Resolution, State, resolve};
use serde::{Deserialize, Serialize};

use crate::doc::Document;

mod review;
use review::timestamp;

#[derive(Debug, Clone)]
pub(crate) struct Store {
    /// `None` for transient documents: nothing is ever written.
    path: Option<PathBuf>,
    /// The document this store's record belongs to (absolute), for the record's `path` field.
    document: Option<PathBuf>,
    annotations: Vec<Annotation>,
    /// Parallel to `annotations`.
    resolved: Vec<Resolution>,
    deliveries: Vec<Delivered>,
    archived: Vec<Annotation>,
}

#[derive(Default, Serialize, Deserialize)]
struct Record {
    /// The absolute document path this record belongs to. Written since 0.5.0 so folder
    /// sends can enumerate annotated files without walking the tree; absent in older
    /// records, which are then only found through listed tree rows.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    path: Option<PathBuf>,
    annotations: Vec<Annotation>,
    /// Every send, newest last. Lets the UI say "sent" across restarts.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    deliveries: Vec<Delivered>,
    /// Finished annotations, kept whole so restoring preserves ids, anchors and metadata.
    /// Additive: consumers of the existing annotations array can ignore this field.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    archived: Vec<Annotation>,
}

/// One send of the feedback: when, where, and which annotations it covered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct Delivered {
    pub(crate) at: String,
    pub(crate) target: String,
    pub(crate) annotation_ids: Vec<String>,
}

/// A resolved annotation: the record plus where it currently sits in the source.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Placed<'a> {
    pub(crate) annotation: &'a Annotation,
    pub(crate) range: &'a Range<usize>,
}

impl Placed<'_> {
    pub(crate) fn kind(&self) -> Kind {
        self.annotation.anchor.kind()
    }
}

/// Where a document's annotations live, and its legacy sidecar if one exists.
#[derive(Debug, Clone)]
pub(crate) struct Location {
    pub(crate) record: PathBuf,
    pub(crate) legacy_sidecar: Option<PathBuf>,
    /// The document the record is for; written into the record so folder sends can find
    /// annotated files without walking the tree.
    pub(crate) document: Option<PathBuf>,
}

impl Location {
    /// `<data-dir>/clients/plannotator-tui/annotations/<project>/<slug>/annotations.json`, plus
    /// the phase-2 sidecar path when a file exists there.
    pub(crate) fn for_file(data_dir: &Path, project: &str, doc_path: &Path) -> Self {
        let resolved = doc_path.to_string_lossy();
        let record =
            plannotator_tui_schema::annotations_dir(data_dir, project, &resolved).join("annotations.json");
        let mut name = doc_path.file_name().map(std::ffi::OsStr::to_os_string).unwrap_or_default();
        name.push(".annotations.json");
        let sidecar = doc_path.with_file_name(name);
        Self {
            record,
            legacy_sidecar: sidecar.is_file().then_some(sidecar),
            document: Some(doc_path.to_path_buf()),
        }
    }
}

fn read_record(path: &Path) -> Result<Option<Record>> {
    match std::fs::read_to_string(path) {
        Ok(json) => {
            let record: Record =
                serde_json::from_str(&json).with_context(|| format!("parsing {}", path.display()))?;
            Ok(Some(record))
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
    }
}

impl Store {
    /// Load the record at `location`; import the legacy sidecar when the record is absent.
    pub(crate) fn load(location: &Location, doc: &Document) -> Result<Self> {
        let (record, imported) = match read_record(&location.record)? {
            Some(found) => (found, false),
            None => match &location.legacy_sidecar {
                Some(sidecar) => (read_record(sidecar)?.unwrap_or_default(), true),
                None => (Record::default(), false),
            },
        };
        let needs_path = record.path.is_none();
        let mut store = Self {
            path: Some(location.record.clone()),
            document: location.document.clone(),
            annotations: record.annotations,
            resolved: Vec::new(),
            deliveries: record.deliveries,
            archived: record.archived,
        };
        store.resolve_all(doc);
        if imported && !store.annotations.is_empty() {
            store.save()?; // the import is now the record; the sidecar is left alone
        } else if needs_path && !store.annotations.is_empty() && store.document.is_some() {
            // A pre-0.5.0 record: write the document path in so folder sends can find it.
            store.save()?;
        }
        Ok(store)
    }

    /// A store that never touches disk, for transient documents.
    pub(crate) fn transient() -> Self {
        Self {
            path: None,
            document: None,
            annotations: Vec::new(),
            resolved: Vec::new(),
            deliveries: Vec::new(),
            archived: Vec::new(),
        }
    }

    /// True when nothing about this store ever reaches disk.
    #[cfg(test)]
    pub(crate) fn is_transient(&self) -> bool {
        self.path.is_none()
    }

    /// Every document with active or archived annotations recorded for `project`, from records carrying their
    /// path (written since 0.5.0). Older records surface through listed tree rows instead.
    pub(crate) fn annotated_documents(data_dir: &Path, project: &str) -> Vec<PathBuf> {
        let dir = plannotator_tui_schema::annotations_dir(data_dir, project, "x");
        let Some(project_dir) = dir.parent() else { return Vec::new() };
        let Ok(entries) = std::fs::read_dir(project_dir) else { return Vec::new() };
        let mut found: Vec<PathBuf> = entries
            .filter_map(Result::ok)
            .map(|e| e.path().join("annotations.json"))
            .filter_map(|record| read_record(&record).ok().flatten())
            .filter(|record| !record.annotations.is_empty() || !record.archived.is_empty())
            .filter_map(|record| record.path)
            .collect();
        found.sort();
        found.dedup();
        found
    }

    /// Count annotations recorded for a file without loading a document.
    pub(crate) fn count_at(location: &Location) -> usize {
        read_record(&location.record)
            .ok()
            .flatten()
            .or_else(|| location.legacy_sidecar.as_deref().and_then(|p| read_record(p).ok().flatten()))
            .map_or(0, |r| r.annotations.len())
    }

    fn save(&self) -> Result<()> {
        let Some(path) = &self.path else { return Ok(()) };
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
        }
        let record = Record {
            path: self.document.clone(),
            annotations: self.annotations.clone(),
            deliveries: self.deliveries.clone(),
            archived: self.archived.clone(),
        };
        let json = serde_json::to_string_pretty(&record)?;
        let tmp = path.with_extension("json.tmp");
        std::fs::write(&tmp, json).with_context(|| format!("writing {}", tmp.display()))?;
        std::fs::rename(&tmp, path).with_context(|| format!("replacing {}", path.display()))?;
        Ok(())
    }

    /// Annotate `range` of the source. `rendered` is the selection's rendered text.
    pub(crate) fn add(
        &mut self,
        doc: &Document,
        range: Range<usize>,
        rendered: String,
        kind: Kind,
        body: String,
    ) -> Result<()> {
        let block = doc.block_containing(range.start);
        let source_range = plannotator_tui_schema::SourceRange {
            start: range.start,
            end: range.end,
            version: plannotator_tui_schema::blob_sha(doc.source.as_bytes()),
        };
        let anchor = Anchor::new(rendered, &doc.source, source_range, kind, block);
        let now = timestamp()?;
        self.annotations.push(Annotation {
            id: local_id(),
            document_id: String::new(),
            anchor,
            body,
            author: None,
            author_name: None,
            state: State::Open,
            attachments: Vec::new(),
            created_at: now.clone(),
            updated_at: now,
            replies: Vec::new(),
            other: std::collections::BTreeMap::default(),
        });
        self.resolved.push(Resolution::Range(range));
        self.save()
    }

    /// Remove every annotation resolved into `block`. Returns how many were removed.
    pub(crate) fn remove_in_block(&mut self, doc: &Document, block: usize) -> Result<usize> {
        let ids: Vec<String> = self
            .annotations
            .iter()
            .zip(&self.resolved)
            .filter(|(_, r)| matches!(r, Resolution::Range(range) if doc.block_containing(range.start) == Some(block)))
            .map(|(a, _)| a.id.clone())
            .collect();
        for id in &ids {
            self.remove_unsaved(id);
        }
        self.save()?;
        Ok(ids.len())
    }

    pub(crate) fn remove(&mut self, id: &str) -> Result<bool> {
        let removed = self.remove_unsaved(id);
        self.save()?;
        Ok(removed)
    }

    fn remove_unsaved(&mut self, id: &str) -> bool {
        let Some(index) = self.annotations.iter().position(|a| a.id == id) else { return false };
        self.annotations.remove(index);
        self.resolved.remove(index);
        true
    }

    pub(crate) fn resolve_all(&mut self, doc: &Document) {
        self.resolved = self
            .annotations
            .iter()
            .map(|a| resolve(&a.anchor, &doc.source, |o| doc.block_containing(o)))
            .collect();
    }

    /// Every resolved annotation, in source order.
    pub(crate) fn placed(&self) -> Vec<Placed<'_>> {
        let mut out: Vec<Placed<'_>> = self
            .annotations
            .iter()
            .zip(&self.resolved)
            .filter_map(|(annotation, r)| match r {
                Resolution::Range(range) => Some(Placed { annotation, range }),
                Resolution::Orphan => None,
            })
            .collect();
        out.sort_by_key(|p| p.range.start);
        out
    }

    pub(crate) fn len(&self) -> usize {
        self.annotations.len()
    }

    pub(crate) fn orphans(&self) -> usize {
        self.resolved.iter().filter(|r| **r == Resolution::Orphan).count()
    }
}

/// Howard Hinnant's days-to-civil, for a dependency-free UTC date.
pub(crate) fn civil_from_days(days: u64) -> (u64, u64, u64) {
    let z = days + 719_468;
    let era = z / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    (if m <= 2 { y + 1 } else { y }, m, d)
}

/// A local id in the server's style. Replaced by the server id once synced.
fn local_id() -> String {
    let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_nanos());
    format!("local_{nanos:x}")
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::indexing_slicing, reason = "tests assert by panicking")]
mod tests {
    use super::*;

    fn temp_root(tag: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("plannotator-tui-store-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("temp dir");
        dir
    }

    #[test]
    fn civil_dates_are_correct() {
        assert_eq!(civil_from_days(0), (1970, 1, 1));
        assert_eq!(civil_from_days(19_723), (2024, 1, 1));
        assert_eq!(civil_from_days(20_694), (2026, 8, 29));
    }

    #[test]
    fn saves_on_every_change_and_re_resolves_after_an_edit() {
        let root = temp_root("save");
        let data_dir = root.join("data");
        let doc_path = root.join("doc.md");
        let location = Location::for_file(&data_dir, "proj", &doc_path);
        let source = "# A\n\nfirst\n\nsecond thing\n".to_owned();
        let doc = Document::parse(source.clone());
        let mut store = Store::load(&location, &doc).expect("empty store");
        let start = source.find("second").expect("present");
        store.add(&doc, start..start + 6, "second".into(), Kind::LooksGood, String::new()).expect("saved");
        assert!(location.record.is_file(), "record written under the data dir on add");
        assert!(location.record.starts_with(data_dir.join("clients/plannotator-tui/annotations/proj")));
        assert!(!doc_path.with_file_name("doc.md.annotations.json").exists(), "no sidecar");

        let edited = Document::parse("# A\n\ninserted\n\nfirst\n\nsecond thing\n".to_owned());
        let reloaded = Store::load(&location, &edited).expect("reloads");
        let placed = reloaded.placed();
        let expected = edited.source.find("second").expect("present");
        assert_eq!(placed.len(), 1);
        assert_eq!(placed[0].range.clone(), expected..expected + 6);
        assert_eq!(placed[0].kind(), Kind::LooksGood);
        assert_eq!(Store::count_at(&location), 1);
        std::fs::remove_dir_all(&root).expect("cleanup");
    }

    #[test]
    fn legacy_sidecar_is_imported_once_and_left_alone() {
        let root = temp_root("import");
        let doc_path = root.join("doc.md");
        std::fs::write(&doc_path, "hello world\n").expect("doc");
        let sidecar = root.join("doc.md.annotations.json");
        let doc = Document::parse("hello world\n".to_owned());
        let mut seed = Store {
            document: None,
            path: Some(sidecar.clone()),
            annotations: Vec::new(),
            resolved: Vec::new(),
            deliveries: Vec::new(),
            archived: Vec::new(),
        };
        seed.add(&doc, 0..5, "hello".into(), Kind::Comment, "hi".into()).expect("seed sidecar");
        let before = std::fs::read_to_string(&sidecar).expect("sidecar");

        let location = Location::for_file(&root.join("data"), "proj", &doc_path);
        let store = Store::load(&location, &doc).expect("imports");
        assert_eq!(store.len(), 1);
        assert!(location.record.is_file(), "imported into the data dir");
        assert_eq!(std::fs::read_to_string(&sidecar).expect("sidecar"), before, "sidecar untouched");
        std::fs::remove_dir_all(&root).expect("cleanup");
    }

    #[test]
    fn a_record_without_deliveries_still_loads() {
        let root = temp_root("old-shape");
        let doc_path = root.join("doc.md");
        let location = Location::for_file(&root.join("data"), "proj", &doc_path);
        std::fs::create_dir_all(location.record.parent().expect("parent")).expect("dir");
        std::fs::write(&location.record, r#"{"annotations":[]}"#).expect("old record");
        let store = Store::load(&location, &Document::parse("x\n".to_owned())).expect("loads");
        assert!(!store.all_delivered());
        std::fs::remove_dir_all(&root).expect("cleanup");
    }

    #[test]
    fn all_delivered_tracks_the_last_send_across_reloads() {
        let root = temp_root("deliver");
        let doc_path = root.join("doc.md");
        let location = Location::for_file(&root.join("data"), "proj", &doc_path);
        let doc = Document::parse("one two three\n".to_owned());
        let mut store = Store::load(&location, &doc).expect("empty");
        assert!(!store.all_delivered(), "nothing to send yet");
        store.add(&doc, 0..3, "one".into(), Kind::Comment, "a".into()).expect("add");
        assert!(!store.all_delivered());
        let ids = store.placed().iter().map(|p| p.annotation.id.clone()).collect::<Vec<_>>();
        store.record_delivery("claude in w1:p1", &ids).expect("record");
        assert!(store.all_delivered());

        let mut reloaded = Store::load(&location, &doc).expect("reload");
        assert!(reloaded.all_delivered(), "the send survives a restart");
        reloaded.add(&doc, 4..7, "two".into(), Kind::LooksGood, String::new()).expect("add");
        assert!(!reloaded.all_delivered(), "a new annotation is unsent");
        let newest = reloaded.placed().last().map(|p| p.annotation.id.clone()).expect("two");
        reloaded.remove(&newest).expect("remove");
        assert!(reloaded.all_delivered(), "back to exactly the delivered set");
        std::fs::remove_dir_all(&root).expect("cleanup");
    }
}