clankerdiff-markdown 0.1.1

Portable semantic Markdown parsing and review state
Documentation
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
//! Selection, draft editing, and decisions for a Markdown review.

use crate::{
    MarkdownAnchor, MarkdownCommentContext, MarkdownDocument, MarkdownReview,
    MarkdownReviewDecision, MarkdownReviewError, MarkdownReviewEvent, MarkdownReviewSubmission,
    MarkdownTarget, MarkdownTargetId,
};
use std::sync::Arc;

/// An editable UTF-8-safe Markdown comment draft.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarkdownCommentDraft {
    target: MarkdownTargetId,
    anchor: MarkdownAnchor,
    context: MarkdownCommentContext,
    body: String,
    cursor: usize,
    editing: Option<u64>,
}

impl MarkdownCommentDraft {
    fn new(
        target: MarkdownTargetId,
        anchor: MarkdownAnchor,
        context: MarkdownCommentContext,
        body: String,
        editing: Option<u64>,
    ) -> Self {
        Self {
            target,
            anchor,
            context,
            cursor: body.len(),
            body,
            editing,
        }
    }

    #[must_use]
    pub const fn target(&self) -> MarkdownTargetId {
        self.target
    }

    #[must_use]
    pub const fn target_id(&self) -> MarkdownTargetId {
        self.target
    }

    #[must_use]
    pub const fn anchor(&self) -> &MarkdownAnchor {
        &self.anchor
    }

    #[must_use]
    pub const fn context(&self) -> &MarkdownCommentContext {
        &self.context
    }

    #[must_use]
    pub fn body(&self) -> &str {
        &self.body
    }

    #[must_use]
    pub const fn cursor(&self) -> usize {
        self.cursor
    }

    #[must_use]
    pub fn body_before_cursor(&self) -> &str {
        &self.body[..self.cursor]
    }

    #[must_use]
    pub const fn editing(&self) -> Option<u64> {
        self.editing
    }

    pub fn set_body(&mut self, body: impl Into<String>) {
        self.body = body.into();
        self.cursor = self.body.len();
    }

    pub fn insert(&mut self, text: &str) {
        self.body.insert_str(self.cursor, text);
        self.cursor += text.len();
    }

    pub fn delete_before_cursor(&mut self) {
        let previous = self.previous_boundary();
        self.body.replace_range(previous..self.cursor, "");
        self.cursor = previous;
    }

    pub fn delete_at_cursor(&mut self) {
        let next = self.next_boundary();
        self.body.replace_range(self.cursor..next, "");
    }

    pub fn move_cursor_left(&mut self) {
        self.cursor = self.previous_boundary();
    }

    pub fn move_cursor_right(&mut self) {
        self.cursor = self.next_boundary();
    }

    pub const fn move_cursor_to_start(&mut self) {
        self.cursor = 0;
    }

    pub fn move_cursor_to_end(&mut self) {
        self.cursor = self.body.len();
    }

    fn previous_boundary(&self) -> usize {
        self.body[..self.cursor]
            .char_indices()
            .next_back()
            .map_or(0, |(offset, _)| offset)
    }

    fn next_boundary(&self) -> usize {
        self.body[self.cursor..]
            .char_indices()
            .nth(1)
            .map_or(self.body.len(), |(offset, _)| self.cursor + offset)
    }
}

/// State for reviewing one parsed Markdown document.
#[derive(Debug, Clone)]
pub struct MarkdownReviewSession {
    document: Arc<MarkdownDocument>,
    review: MarkdownReview,
    selected_target: Option<MarkdownTargetId>,
    draft: Option<MarkdownCommentDraft>,
}

impl MarkdownReviewSession {
    #[must_use]
    pub fn new(document: Arc<MarkdownDocument>) -> Self {
        let selected_target = document.targets().first().map(|target| target.id);
        Self {
            document,
            review: MarkdownReview::default(),
            selected_target,
            draft: None,
        }
    }

    #[must_use]
    pub const fn document(&self) -> &Arc<MarkdownDocument> {
        &self.document
    }

    #[must_use]
    pub const fn review(&self) -> &MarkdownReview {
        &self.review
    }

    pub const fn review_mut(&mut self) -> &mut MarkdownReview {
        &mut self.review
    }

    #[must_use]
    pub const fn selected_target(&self) -> Option<MarkdownTargetId> {
        self.selected_target
    }

    #[must_use]
    pub fn selected_target_info(&self) -> Option<&MarkdownTarget> {
        self.selected_target.and_then(|id| self.document.target(id))
    }

    #[must_use]
    pub fn selected_anchor(&self) -> Option<MarkdownAnchor> {
        self.selected_target
            .and_then(|id| self.document.anchor_for_target(id))
    }

    #[must_use]
    pub const fn draft(&self) -> Option<&MarkdownCommentDraft> {
        self.draft.as_ref()
    }

    pub const fn draft_mut(&mut self) -> Option<&mut MarkdownCommentDraft> {
        self.draft.as_mut()
    }

    /// Selects a target from this document.
    pub fn select_target(&mut self, target: MarkdownTargetId) -> bool {
        if self.document.target(target).is_none() {
            return false;
        }
        self.selected_target = Some(target);
        self.draft = None;
        true
    }

    /// Alias for [`Self::select_target`] useful to frontend adapters.
    pub fn select(&mut self, target: MarkdownTargetId) -> bool {
        self.select_target(target)
    }

    /// Moves one target forward or backward.
    pub fn move_by(&mut self, delta: isize) -> Option<MarkdownTargetId> {
        self.move_target(delta)
    }

    /// Returns the most recently added comment at the current selection.
    #[must_use]
    pub fn last_comment_id(&self) -> Option<u64> {
        self.review.most_recent_comment_id()
    }

    /// Returns comments attached to the current selection.
    #[must_use]
    pub fn selected_comments(&self) -> std::vec::IntoIter<&crate::MarkdownReviewComment> {
        self.selected_target.map_or_else(
            || Vec::new().into_iter(),
            |target| self.review.comments_for_target(&self.document, target),
        )
    }

    /// Moves through commentable targets, clamping at either end.
    pub fn move_target(&mut self, delta: isize) -> Option<MarkdownTargetId> {
        let count = self.document.targets().len();
        if count == 0 {
            self.selected_target = None;
            return None;
        }
        let current = self
            .selected_target
            .map_or(0, MarkdownTargetId::index)
            .min(count - 1);
        let index = if delta.is_negative() {
            current.saturating_sub(delta.unsigned_abs())
        } else {
            current.saturating_add(delta.unsigned_abs()).min(count - 1)
        };
        self.selected_target = Some(self.document.targets()[index].id);
        self.draft = None;
        self.selected_target
    }

    /// Selects the first or last target.
    pub fn select_boundary(&mut self, last: bool) -> Option<MarkdownTargetId> {
        let target = if last {
            self.document.targets().last()
        } else {
            self.document.targets().first()
        }
        .map(|target| target.id);
        self.selected_target = target;
        self.draft = None;
        target
    }

    pub fn select_first_target(&mut self) -> Option<MarkdownTargetId> {
        self.select_boundary(false)
    }

    pub fn select_last_target(&mut self) -> Option<MarkdownTargetId> {
        self.select_boundary(true)
    }

    /// Selects the next heading after the current target.
    pub fn next_heading(&mut self) -> Option<MarkdownTargetId> {
        self.move_heading(false)
    }

    /// Selects the previous heading before the current target.
    pub fn previous_heading(&mut self) -> Option<MarkdownTargetId> {
        self.move_heading(true)
    }

    fn move_heading(&mut self, previous: bool) -> Option<MarkdownTargetId> {
        let current = self.selected_target.map(MarkdownTargetId::index);
        let headings = self.document.outline();
        let heading = if previous {
            headings
                .iter()
                .rev()
                .find(|heading| current.is_none_or(|index| heading.target_id.index() < index))
        } else {
            headings
                .iter()
                .find(|heading| current.is_none_or(|index| heading.target_id.index() > index))
        }?;
        self.selected_target = Some(heading.target_id);
        self.draft = None;
        self.selected_target
    }

    /// Begins a new draft on the current target, or edits an existing comment.
    pub fn begin_draft(&mut self, editing: Option<u64>) -> bool {
        let Some(target) = self.selected_target else {
            return false;
        };
        let Some(anchor) = self.document.anchor_for_target(target) else {
            return false;
        };
        let Some(context) = MarkdownCommentContext::from_target(&self.document, target, &anchor)
        else {
            return false;
        };
        let body = match editing {
            Some(id) => {
                let Some(comment) = self.review.comment(id) else {
                    return false;
                };
                // Editing is intentionally tied to the selected target. This prevents
                // an editor from silently changing an unrelated comment after a move.
                if comment.anchor != anchor {
                    return false;
                }
                comment.body.clone()
            }
            None => String::new(),
        };
        self.draft = Some(MarkdownCommentDraft::new(
            target, anchor, context, body, editing,
        ));
        true
    }

    pub fn cancel_draft(&mut self) {
        self.draft = None;
    }

    /// Saves the active draft, returning its comment ID. Blank drafts are discarded.
    pub fn submit_draft(&mut self) -> Option<u64> {
        let draft = self.draft.take()?;
        if draft.body.trim().is_empty() {
            return None;
        }
        Some(match draft.editing {
            Some(id) => {
                if !self.review.edit_comment(id, draft.body) {
                    return None;
                }
                id
            }
            None => self
                .review
                .add_comment_with_context(draft.anchor, draft.context, draft.body),
        })
    }

    #[must_use]
    pub fn comment_id_at_selection(&self) -> Option<u64> {
        let anchor = self.selected_anchor()?;
        self.review
            .comments_for_anchor(&anchor)
            .next_back()
            .map(|comment| comment.id)
    }

    pub fn edit_comment_at_selection(&mut self) -> bool {
        self.comment_id_at_selection()
            .is_some_and(|id| self.begin_draft(Some(id)))
    }

    pub fn delete_comment_at_selection(&mut self) -> bool {
        let Some(id) = self.comment_id_at_selection() else {
            return false;
        };
        self.review.remove_comment(id).is_some()
    }

    /// Removes the most recently added comment, if there is one.
    pub fn undo_last_comment(&mut self) -> bool {
        let Some(id) = self.review.most_recent_comment_id() else {
            return false;
        };
        self.review.remove_comment(id).is_some()
    }

    pub fn clear_review(&mut self) {
        self.review.clear();
        self.draft = None;
    }

    /// Replaces the document, reconciles comments, and preserves selection when possible.
    pub fn replace_document(&mut self, document: Arc<MarkdownDocument>) {
        let selected_anchor = self.selected_anchor();
        self.review.reconcile(&document);
        self.document = document;
        self.draft = None;
        self.selected_target = selected_anchor
            .and_then(|anchor| self.document.resolve_anchor(&anchor))
            .or_else(|| self.document.targets().first().map(|target| target.id));
    }

    /// Alias for [`Self::replace_document`] used by host adapters.
    pub fn set_document(&mut self, document: Arc<MarkdownDocument>) {
        self.replace_document(document);
    }

    /// Builds a submission for an explicit decision.
    #[must_use]
    pub fn submission(&self, decision: MarkdownReviewDecision) -> MarkdownReviewSubmission {
        self.review.submission(decision)
    }

    /// Builds an explicit submission event for either decision.
    ///
    /// # Errors
    ///
    /// Returns an error when a comment body is blank.
    pub fn submit(
        &self,
        decision: MarkdownReviewDecision,
    ) -> Result<MarkdownReviewEvent, MarkdownReviewError> {
        Ok(MarkdownReviewEvent::Submit(
            self.review.try_submission(decision)?,
        ))
    }

    /// Builds an explicit approval event.
    ///
    /// # Errors
    ///
    /// Returns an error when a comment body is blank.
    pub fn approve(&self) -> Result<MarkdownReviewEvent, MarkdownReviewError> {
        self.submit(MarkdownReviewDecision::Approved)
    }

    /// Builds an explicit request-changes event.
    ///
    /// # Errors
    ///
    /// Returns an error when a comment body is blank.
    pub fn request_changes(&self) -> Result<MarkdownReviewEvent, MarkdownReviewError> {
        self.submit(MarkdownReviewDecision::ChangesRequested)
    }

    /// Builds an explicit approval submission.
    ///
    /// # Errors
    ///
    /// Returns an error when a comment body is blank.
    pub fn approve_submission(&self) -> Result<MarkdownReviewSubmission, MarkdownReviewError> {
        self.review.try_submission(MarkdownReviewDecision::Approved)
    }

    /// Builds an explicit request-changes submission.
    ///
    /// # Errors
    ///
    /// Returns an error when a comment body is blank.
    pub fn request_changes_submission(
        &self,
    ) -> Result<MarkdownReviewSubmission, MarkdownReviewError> {
        self.review
            .try_submission(MarkdownReviewDecision::ChangesRequested)
    }

    #[must_use]
    pub fn cancel(&self) -> MarkdownReviewEvent {
        MarkdownReviewEvent::Cancel
    }

    #[must_use]
    pub fn copy_formatted(&self, decision: MarkdownReviewDecision) -> MarkdownReviewEvent {
        MarkdownReviewEvent::CopyFormatted(self.review.submission(decision).formatted)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{MarkdownDocument, MarkdownTargetKind};

    fn session(source: &str) -> MarkdownReviewSession {
        MarkdownReviewSession::new(Arc::new(MarkdownDocument::parse_with_metadata(
            Some("plan.md".into()),
            None,
            source,
        )))
    }

    #[test]
    fn navigates_targets_and_headings() {
        let mut session = session("# One\n\nfirst\n\n## Two\n\nsecond\n");
        assert_eq!(
            session.selected_target_info().unwrap().kind,
            MarkdownTargetKind::Heading
        );
        assert_eq!(session.next_heading().unwrap().index(), 2);
        assert_eq!(session.next_heading(), None);
        assert_eq!(session.previous_heading().unwrap().index(), 0);
        assert_eq!(session.select_last_target().unwrap().index(), 3);
        assert_eq!(session.select_first_target().unwrap().index(), 0);
    }

    #[test]
    fn drafts_are_utf8_safe_and_support_edit_delete_undo() {
        let mut session = session("paragraph\n");
        assert!(session.begin_draft(None));
        session.draft_mut().unwrap().insert("a界b");
        session.draft_mut().unwrap().move_cursor_left();
        session.draft_mut().unwrap().delete_before_cursor();
        assert_eq!(session.draft().unwrap().body(), "ab");
        let id = session.submit_draft().unwrap();
        assert_eq!(session.comment_id_at_selection(), Some(id));
        assert!(session.edit_comment_at_selection());
        session.draft_mut().unwrap().insert(" revised");
        assert_eq!(session.submit_draft(), Some(id));
        assert!(session.delete_comment_at_selection());
        assert!(session.begin_draft(None));
        session.draft_mut().unwrap().insert("undo");
        session.submit_draft();
        assert!(session.undo_last_comment());
    }

    #[test]
    fn replacement_preserves_selection_and_marks_removed_comments_outdated() {
        let mut session = session("# Plan\n\nKeep\n\nRemove\n");
        session.move_target(2);
        let selected = session.selected_target();
        session.begin_draft(None);
        session.draft_mut().unwrap().insert("note");
        session.submit_draft();
        session.replace_document(Arc::new(MarkdownDocument::parse_with_metadata(
            Some("new.md".into()),
            None,
            "# Plan\n\nInserted\n\nKeep\n",
        )));
        assert_ne!(session.selected_target(), None);
        assert_eq!(session.review().outdated_count(), 1);
        assert_eq!(session.draft(), None);
        assert_ne!(session.selected_target(), selected);
    }

    #[test]
    fn decision_events_are_distinct() {
        let session = session("text\n");
        let approved = session.approve().unwrap();
        let requested = session.request_changes().unwrap();
        assert!(
            matches!(approved, MarkdownReviewEvent::Submit(submission) if submission.decision == MarkdownReviewDecision::Approved)
        );
        assert!(
            matches!(requested, MarkdownReviewEvent::Submit(submission) if submission.decision == MarkdownReviewDecision::ChangesRequested)
        );
        assert!(matches!(session.cancel(), MarkdownReviewEvent::Cancel));
    }
}