Skip to main content

sui_widgets/
rich_document.rs

1use std::{
2    collections::{HashMap, HashSet},
3    fmt,
4    ops::Range,
5    sync::{Arc, Mutex, MutexGuard},
6};
7
8use pulldown_cmark::{
9    CodeBlockKind, Event as MarkdownEvent, HeadingLevel, Options, Parser, Tag, TagEnd,
10};
11use sui_reactive::{Observable, Observer, Signal, SourceId, Subscription};
12
13mod view;
14
15pub use view::{
16    RichDocumentRenderContext, RichDocumentRendererRegistry, RichDocumentView,
17    RichDocumentViewState,
18};
19
20/// Stable identity for a block in a [`RichDocumentModel`].
21#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct RichBlockId(u64);
23
24impl RichBlockId {
25    pub const fn new(value: u64) -> Self {
26        Self(value)
27    }
28
29    pub const fn get(self) -> u64 {
30        self.0
31    }
32}
33
34impl fmt::Display for RichBlockId {
35    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36        self.0.fmt(formatter)
37    }
38}
39
40#[derive(Clone, Debug, Default, PartialEq, Eq)]
41pub struct RichInlineStyle {
42    pub strong: bool,
43    pub emphasis: bool,
44    pub strikethrough: bool,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub struct RichLink {
49    pub destination: String,
50    pub title: Option<String>,
51}
52
53impl RichLink {
54    pub fn new(destination: impl Into<String>) -> Self {
55        Self {
56            destination: destination.into(),
57            title: None,
58        }
59    }
60
61    pub fn title(mut self, title: impl Into<String>) -> Self {
62        self.title = Some(title.into());
63        self
64    }
65}
66
67#[derive(Clone, Debug, PartialEq, Eq)]
68pub struct RichInlineImage {
69    pub source: String,
70    pub alt: String,
71    pub title: Option<String>,
72}
73
74impl RichInlineImage {
75    pub fn new(source: impl Into<String>, alt: impl Into<String>) -> Self {
76        Self {
77            source: source.into(),
78            alt: alt.into(),
79            title: None,
80        }
81    }
82
83    pub fn title(mut self, title: impl Into<String>) -> Self {
84        self.title = Some(title.into());
85        self
86    }
87}
88
89#[derive(Clone, Debug, PartialEq, Eq)]
90pub enum RichInlineKind {
91    Text,
92    Code,
93    Link(RichLink),
94    Image(RichInlineImage),
95}
96
97#[derive(Clone, Debug, PartialEq, Eq)]
98pub struct RichDocumentSpan {
99    pub text: String,
100    pub source_range: Range<usize>,
101    pub style: RichInlineStyle,
102    pub kind: RichInlineKind,
103}
104
105impl RichDocumentSpan {
106    pub fn text(text: impl Into<String>, source_range: Range<usize>) -> Self {
107        Self {
108            text: text.into(),
109            source_range,
110            style: RichInlineStyle::default(),
111            kind: RichInlineKind::Text,
112        }
113    }
114}
115
116#[derive(Clone, Debug, PartialEq, Eq)]
117pub struct RichListItem {
118    pub source_range: Range<usize>,
119    pub spans: Vec<RichDocumentSpan>,
120    pub checked: Option<bool>,
121}
122
123impl RichListItem {
124    pub fn plain_text(&self) -> String {
125        spans_plain_text(&self.spans)
126    }
127}
128
129#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
130pub enum RichDocumentStatus {
131    #[default]
132    Neutral,
133    Pending,
134    Running,
135    Success,
136    Warning,
137    Error,
138}
139
140#[derive(Clone, Debug, PartialEq, Eq)]
141pub struct RichAttachment {
142    pub name: String,
143    pub media_type: Option<String>,
144    pub source: Option<String>,
145    pub size_bytes: Option<u64>,
146    pub description: Option<String>,
147}
148
149impl RichAttachment {
150    pub fn new(name: impl Into<String>) -> Self {
151        Self {
152            name: name.into(),
153            media_type: None,
154            source: None,
155            size_bytes: None,
156            description: None,
157        }
158    }
159}
160
161/// Application-defined structured block rendered through a registry entry.
162///
163/// `renderer` is an architecture-neutral dispatch key such as `tool-call`,
164/// `diff`, or `operation-log`. The fallback renderer remains usable when an
165/// application does not register a specialized widget.
166#[derive(Clone, Debug, PartialEq, Eq)]
167pub struct RichExtensionBlock {
168    pub renderer: String,
169    pub title: String,
170    pub summary: Option<String>,
171    pub body: String,
172    pub status: RichDocumentStatus,
173    pub initially_expanded: bool,
174    pub metadata: Vec<(String, String)>,
175}
176
177impl RichExtensionBlock {
178    pub fn new(renderer: impl Into<String>, title: impl Into<String>) -> Self {
179        Self {
180            renderer: renderer.into(),
181            title: title.into(),
182            summary: None,
183            body: String::new(),
184            status: RichDocumentStatus::Neutral,
185            initially_expanded: false,
186            metadata: Vec::new(),
187        }
188    }
189}
190
191#[derive(Clone, Debug, PartialEq, Eq)]
192pub enum RichDocumentBlockKind {
193    Paragraph {
194        spans: Vec<RichDocumentSpan>,
195    },
196    Heading {
197        level: u8,
198        spans: Vec<RichDocumentSpan>,
199    },
200    BlockQuote {
201        label: Option<String>,
202        spans: Vec<RichDocumentSpan>,
203    },
204    List {
205        start: Option<u64>,
206        items: Vec<RichListItem>,
207    },
208    CodeBlock {
209        language: Option<String>,
210        code: String,
211    },
212    ThematicBreak,
213    Attachment(RichAttachment),
214    Extension(RichExtensionBlock),
215}
216
217impl RichDocumentBlockKind {
218    pub fn renderer_key(&self) -> String {
219        match self {
220            Self::Paragraph { .. } => "paragraph".to_string(),
221            Self::Heading { .. } => "heading".to_string(),
222            Self::BlockQuote { .. } => "block-quote".to_string(),
223            Self::List { .. } => "list".to_string(),
224            Self::CodeBlock { .. } => "code-block".to_string(),
225            Self::ThematicBreak => "thematic-break".to_string(),
226            Self::Attachment(_) => "attachment".to_string(),
227            Self::Extension(extension) => format!("extension:{}", extension.renderer),
228        }
229    }
230
231    pub fn plain_text(&self) -> String {
232        match self {
233            Self::Paragraph { spans }
234            | Self::Heading { spans, .. }
235            | Self::BlockQuote { spans, .. } => spans_plain_text(spans),
236            Self::List { start, items } => {
237                let mut next = start.unwrap_or(1);
238                items
239                    .iter()
240                    .map(|item| {
241                        let marker = if start.is_some() {
242                            let marker = format!("{next}. ");
243                            next = next.saturating_add(1);
244                            marker
245                        } else {
246                            "- ".to_string()
247                        };
248                        format!("{marker}{}", item.plain_text())
249                    })
250                    .collect::<Vec<_>>()
251                    .join("\n")
252            }
253            Self::CodeBlock { code, .. } => code.clone(),
254            Self::ThematicBreak => String::new(),
255            Self::Attachment(attachment) => attachment.name.clone(),
256            Self::Extension(extension) => {
257                if extension.body.is_empty() {
258                    extension.title.clone()
259                } else {
260                    format!("{}\n{}", extension.title, extension.body)
261                }
262            }
263        }
264    }
265}
266
267#[derive(Clone, Debug, PartialEq, Eq)]
268pub struct RichDocumentBlock {
269    pub id: RichBlockId,
270    pub source_range: Range<usize>,
271    pub kind: RichDocumentBlockKind,
272}
273
274impl RichDocumentBlock {
275    pub fn plain_text(&self) -> String {
276        self.kind.plain_text()
277    }
278
279    pub fn renderer_key(&self) -> String {
280        self.kind.renderer_key()
281    }
282}
283
284#[derive(Clone, Debug, Default, PartialEq, Eq)]
285pub struct RichDocumentUpdate {
286    pub revision: u64,
287    pub reparsed_source: Range<usize>,
288    pub reused_prefix_blocks: usize,
289    pub changed_block_ids: Vec<RichBlockId>,
290    pub append_only: bool,
291}
292
293#[derive(Clone, Debug, Default, PartialEq, Eq)]
294pub struct RichDocumentSnapshot {
295    pub revision: u64,
296    pub markdown: String,
297    pub blocks: Vec<RichDocumentBlock>,
298    pub last_update: RichDocumentUpdate,
299}
300
301#[derive(Clone)]
302pub struct RichDocumentModel {
303    inner: Arc<Mutex<RichDocumentModelInner>>,
304    revision: Signal<u64>,
305    structure_revision: Signal<u64>,
306}
307
308struct RichDocumentModelInner {
309    markdown: String,
310    segments: Vec<RichDocumentSegment>,
311    block_signals: HashMap<RichBlockId, Signal<RichDocumentBlock>>,
312    ids_by_origin: HashMap<(usize, &'static str), RichBlockId>,
313    next_id: u64,
314    revision: u64,
315    last_update: RichDocumentUpdate,
316}
317
318enum RichDocumentSegment {
319    Markdown {
320        source_range: Range<usize>,
321        blocks: Vec<RichDocumentBlock>,
322    },
323    Structured(RichDocumentBlock),
324}
325
326impl RichDocumentSegment {
327    fn block_count(&self) -> usize {
328        match self {
329            Self::Markdown { blocks, .. } => blocks.len(),
330            Self::Structured(_) => 1,
331        }
332    }
333}
334
335impl RichDocumentModel {
336    pub fn new() -> Self {
337        Self {
338            inner: Arc::new(Mutex::new(RichDocumentModelInner {
339                markdown: String::new(),
340                segments: Vec::new(),
341                block_signals: HashMap::new(),
342                ids_by_origin: HashMap::new(),
343                next_id: 1,
344                revision: 0,
345                last_update: RichDocumentUpdate::default(),
346            })),
347            revision: Signal::named("RichDocumentModel", 0),
348            structure_revision: Signal::named("RichDocumentModel::structure", 0),
349        }
350    }
351
352    pub fn from_markdown(markdown: impl Into<String>) -> Self {
353        let model = Self::new();
354        model.set_markdown(markdown);
355        model
356    }
357
358    fn lock_inner(&self) -> MutexGuard<'_, RichDocumentModelInner> {
359        self.inner
360            .lock()
361            .unwrap_or_else(|poisoned| poisoned.into_inner())
362    }
363
364    pub fn revision(&self) -> u64 {
365        self.lock_inner().revision
366    }
367
368    pub fn markdown(&self) -> String {
369        self.lock_inner().markdown.clone()
370    }
371
372    pub fn blocks(&self) -> Vec<RichDocumentBlock> {
373        flatten_blocks(&self.lock_inner())
374    }
375
376    pub fn snapshot(&self) -> RichDocumentSnapshot {
377        let inner = self.lock_inner();
378        RichDocumentSnapshot {
379            revision: inner.revision,
380            markdown: inner.markdown.clone(),
381            blocks: flatten_blocks(&inner),
382            last_update: inner.last_update.clone(),
383        }
384    }
385
386    pub fn last_update(&self) -> RichDocumentUpdate {
387        self.lock_inner().last_update.clone()
388    }
389
390    pub(crate) fn structure_observable(&self) -> &Signal<u64> {
391        &self.structure_revision
392    }
393
394    pub(crate) fn block_signal(&self, id: RichBlockId) -> Option<Signal<RichDocumentBlock>> {
395        self.lock_inner().block_signals.get(&id).cloned()
396    }
397
398    pub fn set_markdown(&self, markdown: impl Into<String>) -> bool {
399        let markdown = markdown.into();
400        let (changed, structure_changed) = {
401            let mut inner = self.lock_inner();
402            if inner.markdown == markdown {
403                return false;
404            }
405            let old = flatten_blocks(&inner);
406            inner.markdown = markdown;
407            let source_len = inner.markdown.len();
408            let parsed = parse_markdown_blocks(&inner.markdown, 0);
409            let markdown_blocks = assign_block_ids(&mut inner, parsed);
410            let structured = inner
411                .segments
412                .drain(..)
413                .filter_map(|segment| match segment {
414                    RichDocumentSegment::Structured(mut block) => {
415                        block.source_range = source_len..source_len;
416                        Some(block)
417                    }
418                    RichDocumentSegment::Markdown { .. } => None,
419                })
420                .map(RichDocumentSegment::Structured)
421                .collect::<Vec<_>>();
422            inner.segments = Vec::with_capacity(structured.len() + 1);
423            if source_len > 0 {
424                inner.segments.push(RichDocumentSegment::Markdown {
425                    source_range: 0..source_len,
426                    blocks: markdown_blocks,
427                });
428            }
429            inner.segments.extend(structured);
430            let structure_changed = commit_update(&mut inner, old, 0..source_len, 0, false);
431            (inner.revision, structure_changed)
432        };
433        let _ = self.revision.set(changed);
434        if structure_changed {
435            let _ = self.structure_revision.set(changed);
436        }
437        true
438    }
439
440    /// Append a streaming Markdown fragment and reparse only the mutable tail.
441    ///
442    /// All blocks before the final Markdown block are retained byte-for-byte,
443    /// including their stable IDs and any widget-local state keyed by them.
444    pub fn append_markdown(&self, fragment: &str) -> bool {
445        if fragment.is_empty() {
446            return false;
447        }
448        let (changed, structure_changed) = {
449            let mut inner = self.lock_inner();
450            let old = flatten_blocks(&inner);
451            let old_source_len = inner.markdown.len();
452            let last_markdown = inner.segments.last().and_then(|segment| match segment {
453                RichDocumentSegment::Markdown {
454                    source_range,
455                    blocks,
456                } => Some((source_range.clone(), blocks.clone())),
457                RichDocumentSegment::Structured(_) => None,
458            });
459            inner.markdown.push_str(fragment);
460            let source_len = inner.markdown.len();
461            let (reparse_start, reused_prefix_blocks) = if let Some((range, blocks)) = last_markdown
462            {
463                let reparse_start = blocks
464                    .last()
465                    .map(|block| block.source_range.start)
466                    .unwrap_or(range.start)
467                    .min(old_source_len);
468                let local_prefix = blocks
469                    .iter()
470                    .take_while(|block| block.source_range.end <= reparse_start)
471                    .count();
472                let segment_prefix = inner.segments[..inner.segments.len().saturating_sub(1)]
473                    .iter()
474                    .map(RichDocumentSegment::block_count)
475                    .sum::<usize>();
476                let parsed = parse_markdown_blocks(&inner.markdown[reparse_start..], reparse_start);
477                let mut next = blocks[..local_prefix].to_vec();
478                next.extend(assign_block_ids(&mut inner, parsed));
479                if let Some(RichDocumentSegment::Markdown {
480                    source_range,
481                    blocks,
482                }) = inner.segments.last_mut()
483                {
484                    source_range.end = source_len;
485                    *blocks = next;
486                }
487                (reparse_start, segment_prefix + local_prefix)
488            } else {
489                let reused = old.len();
490                let parsed = parse_markdown_blocks(fragment, old_source_len);
491                let blocks = assign_block_ids(&mut inner, parsed);
492                inner.segments.push(RichDocumentSegment::Markdown {
493                    source_range: old_source_len..source_len,
494                    blocks,
495                });
496                (old_source_len, reused)
497            };
498            let structure_changed = commit_update(
499                &mut inner,
500                old,
501                reparse_start..source_len,
502                reused_prefix_blocks,
503                true,
504            );
505            (inner.revision, structure_changed)
506        };
507        let _ = self.revision.set(changed);
508        if structure_changed {
509            let _ = self.structure_revision.set(changed);
510        }
511        true
512    }
513
514    pub fn append_attachment(&self, attachment: RichAttachment) -> RichBlockId {
515        self.append_structured(RichDocumentBlockKind::Attachment(attachment))
516    }
517
518    pub fn append_extension(&self, extension: RichExtensionBlock) -> RichBlockId {
519        self.append_structured(RichDocumentBlockKind::Extension(extension))
520    }
521
522    pub fn update_structured(&self, id: RichBlockId, kind: RichDocumentBlockKind) -> bool {
523        if !matches!(
524            kind,
525            RichDocumentBlockKind::Attachment(_) | RichDocumentBlockKind::Extension(_)
526        ) {
527            return false;
528        }
529        let (changed, structure_changed) = {
530            let mut inner = self.lock_inner();
531            let old = flatten_blocks(&inner);
532            let Some(segment_index) = inner.segments.iter().position(
533                |segment| matches!(segment, RichDocumentSegment::Structured(block) if block.id == id),
534            )
535            else {
536                return false;
537            };
538            let reused_prefix_blocks = inner.segments[..segment_index]
539                .iter()
540                .map(RichDocumentSegment::block_count)
541                .sum();
542            let RichDocumentSegment::Structured(block) = &mut inner.segments[segment_index] else {
543                unreachable!("structured segment index changed")
544            };
545            if block.kind == kind {
546                return false;
547            }
548            block.kind = kind;
549            inner.revision = inner.revision.wrapping_add(1);
550            inner.last_update = RichDocumentUpdate {
551                revision: inner.revision,
552                reparsed_source: inner.markdown.len()..inner.markdown.len(),
553                reused_prefix_blocks,
554                changed_block_ids: vec![id],
555                append_only: false,
556            };
557            let structure_changed = reconcile_block_signals(&mut inner, &old);
558            (inner.revision, structure_changed)
559        };
560        let _ = self.revision.set(changed);
561        if structure_changed {
562            let _ = self.structure_revision.set(changed);
563        }
564        true
565    }
566
567    fn append_structured(&self, kind: RichDocumentBlockKind) -> RichBlockId {
568        let (id, revision, structure_changed) = {
569            let mut inner = self.lock_inner();
570            let old = flatten_blocks(&inner);
571            let id = RichBlockId::new(inner.next_id);
572            inner.next_id = inner.next_id.wrapping_add(1).max(1);
573            let end = inner.markdown.len();
574            inner
575                .segments
576                .push(RichDocumentSegment::Structured(RichDocumentBlock {
577                    id,
578                    source_range: end..end,
579                    kind,
580                }));
581            inner.revision = inner.revision.wrapping_add(1);
582            inner.last_update = RichDocumentUpdate {
583                revision: inner.revision,
584                reparsed_source: end..end,
585                reused_prefix_blocks: inner
586                    .segments
587                    .iter()
588                    .take(inner.segments.len().saturating_sub(1))
589                    .map(RichDocumentSegment::block_count)
590                    .sum(),
591                changed_block_ids: vec![id],
592                append_only: true,
593            };
594            let structure_changed = reconcile_block_signals(&mut inner, &old);
595            (id, inner.revision, structure_changed)
596        };
597        let _ = self.revision.set(revision);
598        if structure_changed {
599            let _ = self.structure_revision.set(revision);
600        }
601        id
602    }
603}
604
605impl Default for RichDocumentModel {
606    fn default() -> Self {
607        Self::new()
608    }
609}
610
611impl Observable<u64> for RichDocumentModel {
612    fn source_id(&self) -> SourceId {
613        self.revision.source_id()
614    }
615
616    fn source_name(&self) -> Arc<str> {
617        self.revision.source_name()
618    }
619
620    fn get(&self) -> u64 {
621        self.revision.get()
622    }
623
624    fn subscribe(&self, observer: Observer) -> Subscription {
625        self.revision.subscribe(observer)
626    }
627}
628
629fn flatten_blocks(inner: &RichDocumentModelInner) -> Vec<RichDocumentBlock> {
630    let mut blocks = Vec::new();
631    for segment in &inner.segments {
632        match segment {
633            RichDocumentSegment::Markdown {
634                blocks: markdown, ..
635            } => blocks.extend(markdown.iter().cloned()),
636            RichDocumentSegment::Structured(block) => blocks.push(block.clone()),
637        }
638    }
639    blocks
640}
641
642fn assign_block_ids(
643    inner: &mut RichDocumentModelInner,
644    parsed: Vec<ParsedBlock>,
645) -> Vec<RichDocumentBlock> {
646    parsed
647        .into_iter()
648        .map(|parsed| {
649            let origin = (parsed.source_range.start, block_discriminant(&parsed.kind));
650            let id = *inner.ids_by_origin.entry(origin).or_insert_with(|| {
651                let id = RichBlockId::new(inner.next_id);
652                inner.next_id = inner.next_id.wrapping_add(1).max(1);
653                id
654            });
655            RichDocumentBlock {
656                id,
657                source_range: parsed.source_range,
658                kind: parsed.kind,
659            }
660        })
661        .collect()
662}
663
664fn commit_update(
665    inner: &mut RichDocumentModelInner,
666    old: Vec<RichDocumentBlock>,
667    reparsed_source: Range<usize>,
668    reused_prefix_blocks: usize,
669    append_only: bool,
670) -> bool {
671    inner.revision = inner.revision.wrapping_add(1);
672    let old_by_id = old
673        .iter()
674        .cloned()
675        .map(|block| (block.id, block))
676        .collect::<HashMap<_, _>>();
677    let next = flatten_blocks(inner);
678    let next_ids = next.iter().map(|block| block.id).collect::<HashSet<_>>();
679    let mut changed_block_ids = next
680        .iter()
681        .filter(|block| old_by_id.get(&block.id) != Some(block))
682        .map(|block| block.id)
683        .collect::<Vec<_>>();
684    changed_block_ids.extend(
685        old.iter()
686            .filter(|block| !next_ids.contains(&block.id))
687            .map(|block| block.id),
688    );
689    inner.last_update = RichDocumentUpdate {
690        revision: inner.revision,
691        reparsed_source,
692        reused_prefix_blocks,
693        changed_block_ids,
694        append_only,
695    };
696    reconcile_block_signals(inner, &old)
697}
698
699fn reconcile_block_signals(inner: &mut RichDocumentModelInner, old: &[RichDocumentBlock]) -> bool {
700    let next = flatten_blocks(inner);
701    let old_structure = old
702        .iter()
703        .map(|block| (block.id, block.renderer_key()))
704        .collect::<Vec<_>>();
705    let next_structure = next
706        .iter()
707        .map(|block| (block.id, block.renderer_key()))
708        .collect::<Vec<_>>();
709    let live = next.iter().map(|block| block.id).collect::<HashSet<_>>();
710    inner.block_signals.retain(|id, _| live.contains(id));
711    for block in next {
712        if let Some(signal) = inner.block_signals.get(&block.id) {
713            let _ = signal.set(block);
714        } else {
715            inner.block_signals.insert(
716                block.id,
717                Signal::named(format!("RichDocumentBlock({})", block.id), block),
718            );
719        }
720    }
721    old_structure != next_structure
722}
723
724fn block_discriminant(kind: &RichDocumentBlockKind) -> &'static str {
725    match kind {
726        RichDocumentBlockKind::Paragraph { .. } => "paragraph",
727        RichDocumentBlockKind::Heading { .. } => "heading",
728        RichDocumentBlockKind::BlockQuote { .. } => "quote",
729        RichDocumentBlockKind::List { .. } => "list",
730        RichDocumentBlockKind::CodeBlock { .. } => "code",
731        RichDocumentBlockKind::ThematicBreak => "rule",
732        RichDocumentBlockKind::Attachment(_) => "attachment",
733        RichDocumentBlockKind::Extension(_) => "extension",
734    }
735}
736
737struct ParsedBlock {
738    source_range: Range<usize>,
739    kind: RichDocumentBlockKind,
740}
741
742#[derive(Clone, Default)]
743struct InlineContext {
744    strong: usize,
745    emphasis: usize,
746    strikethrough: usize,
747    code: usize,
748    link: Option<RichLink>,
749    image: Option<RichInlineImage>,
750}
751
752enum ParseBlock {
753    Text {
754        start: usize,
755        end: usize,
756        kind: TextBlockKind,
757        spans: Vec<RichDocumentSpan>,
758    },
759    Code {
760        start: usize,
761        end: usize,
762        language: Option<String>,
763        code: String,
764    },
765    List {
766        start: usize,
767        end: usize,
768        ordered_start: Option<u64>,
769        items: Vec<RichListItem>,
770        current_item: Option<RichListItem>,
771    },
772}
773
774#[derive(Clone)]
775enum TextBlockKind {
776    Paragraph,
777    Heading(u8),
778    Quote(Option<String>),
779}
780
781fn parse_markdown_blocks(markdown: &str, base: usize) -> Vec<ParsedBlock> {
782    let mut options = Options::empty();
783    options.insert(Options::ENABLE_STRIKETHROUGH);
784    options.insert(Options::ENABLE_TASKLISTS);
785    options.insert(Options::ENABLE_TABLES);
786    options.insert(Options::ENABLE_GFM);
787    let mut blocks = Vec::new();
788    let mut current: Option<ParseBlock> = None;
789    let mut inline = InlineContext::default();
790    let mut quote_label: Option<String> = None;
791
792    for (event, local_range) in Parser::new_ext(markdown, options).into_offset_iter() {
793        let range = base + local_range.start..base + local_range.end;
794        match event {
795            MarkdownEvent::Start(Tag::Paragraph) => {
796                if current.is_none() {
797                    current = Some(ParseBlock::Text {
798                        start: range.start,
799                        end: range.end,
800                        kind: TextBlockKind::Paragraph,
801                        spans: Vec::new(),
802                    });
803                }
804            }
805            MarkdownEvent::End(TagEnd::Paragraph) => {
806                extend_end(&mut current, range.end);
807                if !matches!(current, Some(ParseBlock::List { .. })) {
808                    finish_block(&mut current, &mut blocks);
809                }
810            }
811            MarkdownEvent::Start(Tag::Heading { level, .. }) => {
812                finish_block(&mut current, &mut blocks);
813                current = Some(ParseBlock::Text {
814                    start: range.start,
815                    end: range.end,
816                    kind: TextBlockKind::Heading(heading_level(level)),
817                    spans: Vec::new(),
818                });
819            }
820            MarkdownEvent::End(TagEnd::Heading(_)) => {
821                extend_end(&mut current, range.end);
822                finish_block(&mut current, &mut blocks);
823            }
824            MarkdownEvent::Start(Tag::BlockQuote(kind)) => {
825                finish_block(&mut current, &mut blocks);
826                quote_label = kind.map(|kind| format!("{kind:?}"));
827                current = Some(ParseBlock::Text {
828                    start: range.start,
829                    end: range.end,
830                    kind: TextBlockKind::Quote(quote_label.clone()),
831                    spans: Vec::new(),
832                });
833            }
834            MarkdownEvent::End(TagEnd::BlockQuote(_)) => {
835                extend_end(&mut current, range.end);
836                finish_block(&mut current, &mut blocks);
837                quote_label = None;
838            }
839            MarkdownEvent::Start(Tag::CodeBlock(kind)) => {
840                finish_block(&mut current, &mut blocks);
841                current = Some(ParseBlock::Code {
842                    start: range.start,
843                    end: range.end,
844                    language: match kind {
845                        CodeBlockKind::Indented => None,
846                        CodeBlockKind::Fenced(language) => {
847                            let language = language.trim();
848                            (!language.is_empty()).then(|| language.to_string())
849                        }
850                    },
851                    code: String::new(),
852                });
853            }
854            MarkdownEvent::End(TagEnd::CodeBlock) => {
855                extend_end(&mut current, range.end);
856                finish_block(&mut current, &mut blocks);
857            }
858            MarkdownEvent::Start(Tag::List(start)) => {
859                if !matches!(current, Some(ParseBlock::List { .. })) {
860                    finish_block(&mut current, &mut blocks);
861                    current = Some(ParseBlock::List {
862                        start: range.start,
863                        end: range.end,
864                        ordered_start: start,
865                        items: Vec::new(),
866                        current_item: None,
867                    });
868                }
869            }
870            MarkdownEvent::End(TagEnd::List(_)) => {
871                finish_list_item(&mut current, range.end);
872                extend_end(&mut current, range.end);
873                finish_block(&mut current, &mut blocks);
874            }
875            MarkdownEvent::Start(Tag::Item) => {
876                finish_list_item(&mut current, range.start);
877                if let Some(ParseBlock::List { current_item, .. }) = &mut current {
878                    *current_item = Some(RichListItem {
879                        source_range: range.clone(),
880                        spans: Vec::new(),
881                        checked: None,
882                    });
883                }
884            }
885            MarkdownEvent::End(TagEnd::Item) => finish_list_item(&mut current, range.end),
886            MarkdownEvent::Start(Tag::Emphasis) => inline.emphasis += 1,
887            MarkdownEvent::End(TagEnd::Emphasis) => {
888                inline.emphasis = inline.emphasis.saturating_sub(1);
889            }
890            MarkdownEvent::Start(Tag::Strong) => inline.strong += 1,
891            MarkdownEvent::End(TagEnd::Strong) => {
892                inline.strong = inline.strong.saturating_sub(1);
893            }
894            MarkdownEvent::Start(Tag::Strikethrough) => inline.strikethrough += 1,
895            MarkdownEvent::End(TagEnd::Strikethrough) => {
896                inline.strikethrough = inline.strikethrough.saturating_sub(1);
897            }
898            MarkdownEvent::Start(Tag::Link {
899                dest_url, title, ..
900            }) => {
901                inline.link = Some(RichLink {
902                    destination: dest_url.to_string(),
903                    title: (!title.is_empty()).then(|| title.to_string()),
904                });
905            }
906            MarkdownEvent::End(TagEnd::Link) => inline.link = None,
907            MarkdownEvent::Start(Tag::Image {
908                dest_url, title, ..
909            }) => {
910                inline.image = Some(RichInlineImage {
911                    source: dest_url.to_string(),
912                    alt: String::new(),
913                    title: (!title.is_empty()).then(|| title.to_string()),
914                });
915            }
916            MarkdownEvent::End(TagEnd::Image) => {
917                if let Some(image) = inline.image.take() {
918                    let display = if image.alt.is_empty() {
919                        "Image".to_string()
920                    } else {
921                        image.alt.clone()
922                    };
923                    push_span(&mut current, display, range, &inline, Some(image));
924                }
925            }
926            MarkdownEvent::Text(text) => {
927                if let Some(image) = &mut inline.image {
928                    image.alt.push_str(&text);
929                } else if let Some(ParseBlock::Code { code, end, .. }) = &mut current {
930                    code.push_str(&text);
931                    *end = (*end).max(range.end);
932                } else {
933                    ensure_text_block(&mut current, range.start, quote_label.clone());
934                    push_span(&mut current, text.to_string(), range, &inline, None);
935                }
936            }
937            MarkdownEvent::Code(code) => {
938                ensure_text_block(&mut current, range.start, quote_label.clone());
939                inline.code += 1;
940                push_span(&mut current, code.to_string(), range, &inline, None);
941                inline.code = inline.code.saturating_sub(1);
942            }
943            MarkdownEvent::SoftBreak | MarkdownEvent::HardBreak => {
944                ensure_text_block(&mut current, range.start, quote_label.clone());
945                push_span(&mut current, "\n".to_string(), range, &inline, None);
946            }
947            MarkdownEvent::TaskListMarker(checked) => {
948                if let Some(ParseBlock::List {
949                    current_item: Some(item),
950                    ..
951                }) = &mut current
952                {
953                    item.checked = Some(checked);
954                }
955            }
956            MarkdownEvent::Rule => {
957                finish_block(&mut current, &mut blocks);
958                blocks.push(ParsedBlock {
959                    source_range: range,
960                    kind: RichDocumentBlockKind::ThematicBreak,
961                });
962            }
963            MarkdownEvent::Html(html) | MarkdownEvent::InlineHtml(html) => {
964                ensure_text_block(&mut current, range.start, quote_label.clone());
965                push_span(&mut current, html.to_string(), range, &inline, None);
966            }
967            MarkdownEvent::FootnoteReference(reference) => {
968                ensure_text_block(&mut current, range.start, quote_label.clone());
969                push_span(&mut current, format!("[{reference}]"), range, &inline, None);
970            }
971            _ => {}
972        }
973    }
974    finish_block(&mut current, &mut blocks);
975    blocks
976}
977
978fn heading_level(level: HeadingLevel) -> u8 {
979    match level {
980        HeadingLevel::H1 => 1,
981        HeadingLevel::H2 => 2,
982        HeadingLevel::H3 => 3,
983        HeadingLevel::H4 => 4,
984        HeadingLevel::H5 => 5,
985        HeadingLevel::H6 => 6,
986    }
987}
988
989fn ensure_text_block(current: &mut Option<ParseBlock>, start: usize, quote: Option<String>) {
990    if current.is_none() {
991        *current = Some(ParseBlock::Text {
992            start,
993            end: start,
994            kind: quote.map_or(TextBlockKind::Paragraph, |label| {
995                TextBlockKind::Quote(Some(label))
996            }),
997            spans: Vec::new(),
998        });
999    }
1000}
1001
1002fn push_span(
1003    current: &mut Option<ParseBlock>,
1004    text: String,
1005    source_range: Range<usize>,
1006    inline: &InlineContext,
1007    image: Option<RichInlineImage>,
1008) {
1009    let style = RichInlineStyle {
1010        strong: inline.strong > 0,
1011        emphasis: inline.emphasis > 0,
1012        strikethrough: inline.strikethrough > 0,
1013    };
1014    let kind = if let Some(image) = image {
1015        RichInlineKind::Image(image)
1016    } else if inline.code > 0 {
1017        RichInlineKind::Code
1018    } else if let Some(link) = &inline.link {
1019        RichInlineKind::Link(link.clone())
1020    } else {
1021        RichInlineKind::Text
1022    };
1023    let span = RichDocumentSpan {
1024        text,
1025        source_range: source_range.clone(),
1026        style,
1027        kind,
1028    };
1029    match current {
1030        Some(ParseBlock::Text { spans, end, .. }) => {
1031            spans.push(span);
1032            *end = (*end).max(source_range.end);
1033        }
1034        Some(ParseBlock::List {
1035            current_item, end, ..
1036        }) => {
1037            if current_item.is_none() {
1038                *current_item = Some(RichListItem {
1039                    source_range: source_range.clone(),
1040                    spans: Vec::new(),
1041                    checked: None,
1042                });
1043            }
1044            if let Some(item) = current_item {
1045                item.source_range.end = item.source_range.end.max(source_range.end);
1046                item.spans.push(span);
1047            }
1048            *end = (*end).max(source_range.end);
1049        }
1050        Some(ParseBlock::Code { .. }) | None => {}
1051    }
1052}
1053
1054fn finish_list_item(current: &mut Option<ParseBlock>, end: usize) {
1055    if let Some(ParseBlock::List {
1056        items,
1057        current_item,
1058        ..
1059    }) = current
1060        && let Some(mut item) = current_item.take()
1061    {
1062        item.source_range.end = item.source_range.end.max(end);
1063        items.push(item);
1064    }
1065}
1066
1067fn extend_end(current: &mut Option<ParseBlock>, end: usize) {
1068    match current {
1069        Some(ParseBlock::Text { end: current, .. })
1070        | Some(ParseBlock::Code { end: current, .. })
1071        | Some(ParseBlock::List { end: current, .. }) => *current = (*current).max(end),
1072        None => {}
1073    }
1074}
1075
1076fn finish_block(current: &mut Option<ParseBlock>, blocks: &mut Vec<ParsedBlock>) {
1077    let Some(block) = current.take() else {
1078        return;
1079    };
1080    let parsed = match block {
1081        ParseBlock::Text {
1082            start,
1083            end,
1084            kind,
1085            spans,
1086        } => {
1087            if spans.is_empty() {
1088                return;
1089            }
1090            let kind = match kind {
1091                TextBlockKind::Paragraph => RichDocumentBlockKind::Paragraph { spans },
1092                TextBlockKind::Heading(level) => RichDocumentBlockKind::Heading { level, spans },
1093                TextBlockKind::Quote(label) => RichDocumentBlockKind::BlockQuote { label, spans },
1094            };
1095            ParsedBlock {
1096                source_range: start..end.max(start),
1097                kind,
1098            }
1099        }
1100        ParseBlock::Code {
1101            start,
1102            end,
1103            language,
1104            code,
1105        } => ParsedBlock {
1106            source_range: start..end.max(start),
1107            kind: RichDocumentBlockKind::CodeBlock { language, code },
1108        },
1109        ParseBlock::List {
1110            start,
1111            end,
1112            ordered_start,
1113            mut items,
1114            current_item,
1115        } => {
1116            if let Some(item) = current_item {
1117                items.push(item);
1118            }
1119            ParsedBlock {
1120                source_range: start..end.max(start),
1121                kind: RichDocumentBlockKind::List {
1122                    start: ordered_start,
1123                    items,
1124                },
1125            }
1126        }
1127    };
1128    blocks.push(parsed);
1129}
1130
1131fn spans_plain_text(spans: &[RichDocumentSpan]) -> String {
1132    spans.iter().map(|span| span.text.as_str()).collect()
1133}
1134
1135#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1136pub enum RichSyntaxTokenKind {
1137    Keyword,
1138    String,
1139    Number,
1140    Comment,
1141    Type,
1142    Function,
1143    Property,
1144    Added,
1145    Removed,
1146    Header,
1147}
1148
1149#[derive(Clone, Debug, PartialEq, Eq)]
1150pub struct RichSyntaxSpan {
1151    pub range: Range<usize>,
1152    pub kind: RichSyntaxTokenKind,
1153}
1154
1155pub trait RichSyntaxHighlighter {
1156    fn highlight(&self, language: Option<&str>, source: &str) -> Vec<RichSyntaxSpan>;
1157}
1158
1159impl<F> RichSyntaxHighlighter for F
1160where
1161    F: Fn(Option<&str>, &str) -> Vec<RichSyntaxSpan>,
1162{
1163    fn highlight(&self, language: Option<&str>, source: &str) -> Vec<RichSyntaxSpan> {
1164        self(language, source)
1165    }
1166}
1167
1168/// Lightweight dependency-free lexer used by the default code-block renderer.
1169///
1170/// It deliberately recognizes broad token classes rather than implementing a
1171/// language grammar. Applications can replace it with tree-sitter, syntect, or
1172/// an LSP-backed highlighter through [`RichSyntaxHighlighter`].
1173#[derive(Clone, Copy, Debug, Default)]
1174pub struct BasicSyntaxHighlighter;
1175
1176impl RichSyntaxHighlighter for BasicSyntaxHighlighter {
1177    fn highlight(&self, language: Option<&str>, source: &str) -> Vec<RichSyntaxSpan> {
1178        let language = language.unwrap_or_default().to_ascii_lowercase();
1179        if matches!(language.as_str(), "diff" | "patch") {
1180            return highlight_diff(source);
1181        }
1182        highlight_code(&language, source)
1183    }
1184}
1185
1186fn highlight_diff(source: &str) -> Vec<RichSyntaxSpan> {
1187    let mut spans = Vec::new();
1188    let mut offset = 0;
1189    for line in source.split_inclusive('\n') {
1190        let kind = if line.starts_with("@@") || line.starts_with("diff ") {
1191            Some(RichSyntaxTokenKind::Header)
1192        } else if line.starts_with('+') && !line.starts_with("+++") {
1193            Some(RichSyntaxTokenKind::Added)
1194        } else if line.starts_with('-') && !line.starts_with("---") {
1195            Some(RichSyntaxTokenKind::Removed)
1196        } else {
1197            None
1198        };
1199        if let Some(kind) = kind {
1200            spans.push(RichSyntaxSpan {
1201                range: offset..offset + line.len(),
1202                kind,
1203            });
1204        }
1205        offset += line.len();
1206    }
1207    spans
1208}
1209
1210fn highlight_code(language: &str, source: &str) -> Vec<RichSyntaxSpan> {
1211    let mut spans = Vec::new();
1212    let bytes = source.as_bytes();
1213    let mut index = 0;
1214    while index < bytes.len() {
1215        if starts_line_comment(language, bytes, index) {
1216            let end = source[index..]
1217                .find('\n')
1218                .map_or(source.len(), |relative| index + relative);
1219            spans.push(RichSyntaxSpan {
1220                range: index..end,
1221                kind: RichSyntaxTokenKind::Comment,
1222            });
1223            index = end;
1224            continue;
1225        }
1226        if matches!(bytes[index], b'\'' | b'"' | b'`') {
1227            let quote = bytes[index];
1228            let start = index;
1229            index += 1;
1230            while index < bytes.len() {
1231                if bytes[index] == b'\\' {
1232                    index = (index + 2).min(bytes.len());
1233                } else if bytes[index] == quote {
1234                    index += 1;
1235                    break;
1236                } else {
1237                    index += 1;
1238                }
1239            }
1240            spans.push(RichSyntaxSpan {
1241                range: start..index,
1242                kind: RichSyntaxTokenKind::String,
1243            });
1244            continue;
1245        }
1246        if bytes[index].is_ascii_digit() {
1247            let start = index;
1248            index += 1;
1249            while index < bytes.len()
1250                && (bytes[index].is_ascii_alphanumeric()
1251                    || matches!(bytes[index], b'.' | b'_' | b'x' | b'o' | b'b'))
1252            {
1253                index += 1;
1254            }
1255            spans.push(RichSyntaxSpan {
1256                range: start..index,
1257                kind: RichSyntaxTokenKind::Number,
1258            });
1259            continue;
1260        }
1261        if bytes[index].is_ascii_alphabetic() || bytes[index] == b'_' {
1262            let start = index;
1263            index += 1;
1264            while index < bytes.len()
1265                && (bytes[index].is_ascii_alphanumeric() || bytes[index] == b'_')
1266            {
1267                index += 1;
1268            }
1269            let word = &source[start..index];
1270            if is_keyword(language, word) {
1271                spans.push(RichSyntaxSpan {
1272                    range: start..index,
1273                    kind: RichSyntaxTokenKind::Keyword,
1274                });
1275            } else if word.chars().next().is_some_and(char::is_uppercase) {
1276                spans.push(RichSyntaxSpan {
1277                    range: start..index,
1278                    kind: RichSyntaxTokenKind::Type,
1279                });
1280            }
1281            continue;
1282        }
1283        index += source[index..].chars().next().map_or(1, char::len_utf8);
1284    }
1285    spans
1286}
1287
1288fn starts_line_comment(language: &str, bytes: &[u8], index: usize) -> bool {
1289    if bytes.get(index..index + 2) == Some(b"//") {
1290        return true;
1291    }
1292    bytes[index] == b'#'
1293        && matches!(
1294            language,
1295            "python" | "py" | "shell" | "sh" | "bash" | "zsh" | "toml" | "yaml" | "yml"
1296        )
1297}
1298
1299fn is_keyword(language: &str, word: &str) -> bool {
1300    const COMMON: &[&str] = &[
1301        "as",
1302        "async",
1303        "await",
1304        "break",
1305        "case",
1306        "class",
1307        "const",
1308        "continue",
1309        "default",
1310        "do",
1311        "else",
1312        "enum",
1313        "export",
1314        "extends",
1315        "false",
1316        "fn",
1317        "for",
1318        "from",
1319        "function",
1320        "if",
1321        "impl",
1322        "import",
1323        "in",
1324        "interface",
1325        "let",
1326        "loop",
1327        "match",
1328        "mod",
1329        "move",
1330        "mut",
1331        "new",
1332        "none",
1333        "null",
1334        "pub",
1335        "return",
1336        "self",
1337        "static",
1338        "struct",
1339        "super",
1340        "switch",
1341        "trait",
1342        "true",
1343        "try",
1344        "type",
1345        "use",
1346        "var",
1347        "where",
1348        "while",
1349        "yield",
1350    ];
1351    COMMON.contains(&word)
1352        || (matches!(language, "python" | "py")
1353            && matches!(
1354                word,
1355                "def" | "elif" | "except" | "finally" | "lambda" | "with"
1356            ))
1357}
1358
1359#[cfg(test)]
1360mod model_tests {
1361    use super::*;
1362
1363    #[test]
1364    fn markdown_parser_preserves_structure_and_inline_actions() {
1365        let model = RichDocumentModel::from_markdown(
1366            "# Report\n\nRead [docs](https://example.test) and `code`.\n\n- one\n- [x] two\n\n```rust\nlet n = 42;\n```",
1367        );
1368        let blocks = model.blocks();
1369        assert!(matches!(
1370            blocks[0].kind,
1371            RichDocumentBlockKind::Heading { level: 1, .. }
1372        ));
1373        assert!(matches!(
1374            blocks[1].kind,
1375            RichDocumentBlockKind::Paragraph { ref spans }
1376                if spans.iter().any(|span| matches!(span.kind, RichInlineKind::Link(_)))
1377                    && spans.iter().any(|span| matches!(span.kind, RichInlineKind::Code))
1378        ));
1379        assert!(matches!(
1380            blocks[2].kind,
1381            RichDocumentBlockKind::List { ref items, .. }
1382                if items.len() == 2 && items[1].checked == Some(true)
1383        ));
1384        assert!(matches!(
1385            blocks[3].kind,
1386            RichDocumentBlockKind::CodeBlock { ref language, ref code }
1387                if language.as_deref() == Some("rust") && code.contains("let n")
1388        ));
1389    }
1390
1391    #[test]
1392    fn streaming_append_reparses_only_tail_and_preserves_prefix_ids() {
1393        let model = RichDocumentModel::from_markdown("# Stable\n\nStreaming");
1394        let before = model.blocks();
1395        model.append_markdown(" text\n\n- next");
1396        let after = model.blocks();
1397        assert_eq!(before[0].id, after[0].id);
1398        assert_eq!(before[1].id, after[1].id);
1399        assert_eq!(after[1].plain_text(), "Streaming text");
1400        let update = model.last_update();
1401        assert!(update.append_only);
1402        assert_eq!(update.reused_prefix_blocks, 1);
1403        assert_eq!(update.reparsed_source.start, before[1].source_range.start);
1404    }
1405
1406    #[test]
1407    fn structured_blocks_update_in_place() {
1408        let model = RichDocumentModel::new();
1409        let id = model.append_extension(RichExtensionBlock::new("tool-call", "Search"));
1410        let mut changed = RichExtensionBlock::new("tool-call", "Search");
1411        changed.status = RichDocumentStatus::Success;
1412        changed.body = "3 results".to_string();
1413        assert!(model.update_structured(id, RichDocumentBlockKind::Extension(changed)));
1414        assert_eq!(model.blocks()[0].id, id);
1415        assert_eq!(model.last_update().changed_block_ids, vec![id]);
1416    }
1417
1418    #[test]
1419    fn markdown_segments_remain_ordered_around_structured_blocks() {
1420        let model = RichDocumentModel::from_markdown("Before");
1421        let before_id = model.blocks()[0].id;
1422        let extension_id =
1423            model.append_extension(RichExtensionBlock::new("tool-call", "Operation"));
1424
1425        model.append_markdown("\n\nAfter");
1426        let first = model.blocks();
1427        assert_eq!(first.len(), 3);
1428        assert_eq!(first[0].id, before_id);
1429        assert_eq!(first[1].id, extension_id);
1430        assert_eq!(first[2].plain_text(), "After");
1431        let after_id = first[2].id;
1432
1433        model.append_markdown(" continued");
1434        let second = model.blocks();
1435        assert_eq!(second[0].id, before_id);
1436        assert_eq!(second[1].id, extension_id);
1437        assert_eq!(second[2].id, after_id);
1438        assert_eq!(second[2].plain_text(), "After continued");
1439        assert_eq!(model.last_update().reused_prefix_blocks, 2);
1440    }
1441
1442    #[test]
1443    fn replacement_diagnostics_include_removed_block_ids() {
1444        let model = RichDocumentModel::from_markdown("# Heading\n\nParagraph");
1445        let removed = model.blocks()[1].id;
1446        model.set_markdown("# Heading");
1447        assert!(model.last_update().changed_block_ids.contains(&removed));
1448    }
1449
1450    #[test]
1451    fn basic_highlighter_handles_code_and_diff_tokens() {
1452        let rust = BasicSyntaxHighlighter.highlight(Some("rust"), "let value = 42; // count");
1453        assert!(
1454            rust.iter()
1455                .any(|span| span.kind == RichSyntaxTokenKind::Keyword)
1456        );
1457        assert!(
1458            rust.iter()
1459                .any(|span| span.kind == RichSyntaxTokenKind::Number)
1460        );
1461        assert!(
1462            rust.iter()
1463                .any(|span| span.kind == RichSyntaxTokenKind::Comment)
1464        );
1465
1466        let diff = BasicSyntaxHighlighter.highlight(Some("diff"), "@@ -1 +1 @@\n-old\n+new\n");
1467        assert!(
1468            diff.iter()
1469                .any(|span| span.kind == RichSyntaxTokenKind::Header)
1470        );
1471        assert!(
1472            diff.iter()
1473                .any(|span| span.kind == RichSyntaxTokenKind::Removed)
1474        );
1475        assert!(
1476            diff.iter()
1477                .any(|span| span.kind == RichSyntaxTokenKind::Added)
1478        );
1479    }
1480}