gpui-kit-markdown-editor 0.1.1

A block-based WYSIWYG Markdown editor for GPUI, built on gpui-kit's editor host API.
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
use std::collections::HashMap;
use std::sync::Arc;

use super::{
    id::BlockId,
    inline::InlineText,
    kind::{BlockKind, BlockKindData},
    order::OrderIndex,
    position::BlockPosition,
    step::DetachedBlock,
};
use gpui_kit::base::text::{MarkdownExtensions, MarkdownNode};

/// Payload of an atomic block.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AtomData {
    None,
    Custom(MarkdownNode),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum BlockContent {
    Children(Vec<BlockId>),
    /// Paragraph, Heading, TableCell.
    Inline(InlineText),
    /// CodeBlock (its language lives in [`BlockKindData`]).
    Plain(String),
    /// Custom, Definition, Break, HorizontalRule, Unknown.
    Atom(AtomData),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct BlockEntry {
    pub(crate) parent: Option<BlockId>,
    pub(crate) kind: BlockKindData,
    pub(crate) content: BlockContent,
}

/// The editing model: block tree with stable ids (design ยง4.2, D4).
pub(crate) struct EditorDocument {
    pub(crate) revision: u64,
    pub(crate) root: BlockId,
    pub(crate) blocks: HashMap<BlockId, BlockEntry>,
    pub(crate) order: OrderIndex,
    /// Set by structural steps; the order index is rebuilt at commit.
    pub(crate) structure_dirty: bool,
    pub(crate) extensions: Arc<MarkdownExtensions>,
    /// Per-block content revision, bumped for a block and all its ancestors
    /// by every step that touches it; absent means never edited. The view
    /// re-projects a top-level block only when its revision changes.
    pub(crate) block_revisions: HashMap<BlockId, u64>,
    edit_counter: u64,
}

impl EditorDocument {
    /// An empty document: a root holding one empty paragraph.
    pub(crate) fn empty(extensions: Arc<MarkdownExtensions>) -> Self {
        let root = BlockId::next();
        let paragraph = BlockId::next();
        let mut blocks = HashMap::new();
        blocks.insert(
            root,
            BlockEntry {
                parent: None,
                kind: BlockKindData::Root,
                content: BlockContent::Children(vec![paragraph]),
            },
        );
        blocks.insert(
            paragraph,
            BlockEntry {
                parent: Some(root),
                kind: BlockKindData::Paragraph,
                content: BlockContent::Inline(InlineText::new()),
            },
        );
        let mut doc = Self {
            revision: 0,
            root,
            blocks,
            order: OrderIndex::default(),
            structure_dirty: true,
            extensions,
            block_revisions: HashMap::new(),
            edit_counter: 0,
        };
        doc.refresh_order();
        doc
    }

    /// Bump the content revision of `id` and its ancestors.
    pub(crate) fn touch(&mut self, id: BlockId) {
        self.edit_counter += 1;
        let mut next = Some(id);
        while let Some(id) = next {
            self.block_revisions.insert(id, self.edit_counter);
            next = self.parent(id);
        }
    }

    pub(crate) fn block_revision(&self, id: BlockId) -> u64 {
        self.block_revisions.get(&id).copied().unwrap_or(0)
    }

    /// Number of blocks, including the root.
    pub(crate) fn len(&self) -> usize {
        self.blocks.len()
    }

    pub(crate) fn get(&self, id: BlockId) -> Option<&BlockEntry> {
        self.blocks.get(&id)
    }

    pub(crate) fn get_mut(&mut self, id: BlockId) -> Option<&mut BlockEntry> {
        self.blocks.get_mut(&id)
    }

    pub(crate) fn children(&self, id: BlockId) -> &[BlockId] {
        match self.blocks.get(&id).map(|entry| &entry.content) {
            Some(BlockContent::Children(children)) => children,
            _ => &[],
        }
    }

    pub(crate) fn parent(&self, id: BlockId) -> Option<BlockId> {
        self.blocks.get(&id)?.parent
    }

    pub(crate) fn index_in_parent(&self, id: BlockId) -> Option<(BlockId, usize)> {
        let parent = self.parent(id)?;
        let index = self.children(parent).iter().position(|c| *c == id)?;
        Some((parent, index))
    }

    pub(crate) fn kind(&self, id: BlockId) -> Option<&BlockKindData> {
        self.blocks.get(&id).map(|entry| &entry.kind)
    }

    pub(crate) fn inline(&self, id: BlockId) -> Option<&InlineText> {
        match &self.blocks.get(&id)?.content {
            BlockContent::Inline(inline) => Some(inline),
            _ => None,
        }
    }

    /// Whether the block can hold a caret.
    pub(crate) fn is_caret_leaf(&self, id: BlockId) -> bool {
        self.blocks
            .get(&id)
            .is_some_and(|entry| !matches!(entry.content, BlockContent::Children(_)))
    }

    /// Length of a caret leaf in caret units: bytes of text, or 1 for an atom
    /// or code block (0 = before, 1 = after) in the spike.
    pub(crate) fn leaf_len(&self, id: BlockId) -> Option<usize> {
        match &self.blocks.get(&id)?.content {
            BlockContent::Inline(inline) => Some(inline.len()),
            BlockContent::Plain(code) => Some(code.len()),
            BlockContent::Atom(_) => Some(1),
            BlockContent::Children(_) => None,
        }
    }

    /// A code block's text.
    pub(crate) fn plain(&self, id: BlockId) -> Option<&str> {
        match &self.blocks.get(&id)?.content {
            BlockContent::Plain(code) => Some(code),
            _ => None,
        }
    }

    /// Caret-leaf text: inline text (atoms as U+FFFC) or code.
    pub(crate) fn leaf_text(&self, id: BlockId) -> Option<&str> {
        match &self.blocks.get(&id)?.content {
            BlockContent::Inline(inline) => Some(inline.text()),
            BlockContent::Plain(code) => Some(code),
            _ => None,
        }
    }

    /// Path from the root down to `id`, inclusive.
    pub(crate) fn path(&self, id: BlockId) -> Vec<BlockId> {
        let mut path = vec![id];
        let mut current = id;
        while let Some(parent) = self.parent(current) {
            path.push(parent);
            current = parent;
        }
        path.reverse();
        path
    }

    /// Caret leaves in document order, by walking the tree. Used inside
    /// transactions, where the order index may be stale.
    pub(crate) fn walk_leaves(&self) -> Vec<BlockId> {
        let mut out = Vec::new();
        let mut stack = vec![self.root];
        while let Some(id) = stack.pop() {
            match self.blocks.get(&id).map(|entry| &entry.content) {
                Some(BlockContent::Children(children)) => stack.extend(children.iter().rev()),
                Some(_) => out.push(id),
                None => {}
            }
        }
        out
    }

    pub(crate) fn refresh_order(&mut self) {
        if self.structure_dirty {
            self.order = OrderIndex::build(self);
            self.structure_dirty = false;
        }
    }

    /// Leaves in document order from the (fresh) order index.
    pub(crate) fn leaves(&self) -> &[BlockId] {
        debug_assert!(!self.structure_dirty, "order index read while stale");
        self.order.leaves()
    }

    pub(crate) fn first_caret(&self) -> BlockPosition {
        let leaf = self.walk_leaves().first().copied().unwrap_or(self.root);
        BlockPosition::new(leaf, 0)
    }

    pub(crate) fn last_caret(&self) -> BlockPosition {
        let leaf = self.walk_leaves().last().copied().unwrap_or(self.root);
        BlockPosition::new(leaf, self.leaf_len(leaf).unwrap_or(0))
    }

    /// Insert a detached subtree (keeping its ids) under `parent` at `index`.
    pub(crate) fn attach(&mut self, parent: BlockId, index: usize, block: DetachedBlock) -> bool {
        let Some(BlockEntry {
            content: BlockContent::Children(children),
            ..
        }) = self.blocks.get_mut(&parent)
        else {
            return false;
        };
        if index > children.len() {
            return false;
        }
        children.insert(index, block.id);
        self.attach_entry(Some(parent), block);
        self.structure_dirty = true;
        true
    }

    fn attach_entry(&mut self, parent: Option<BlockId>, block: DetachedBlock) {
        let content = match block.content {
            super::step::DetachedContent::Children(children) => {
                let ids = children.iter().map(|child| child.id).collect();
                for child in children {
                    self.attach_entry(Some(block.id), child);
                }
                BlockContent::Children(ids)
            }
            super::step::DetachedContent::Leaf(content) => content,
        };
        self.blocks.insert(
            block.id,
            BlockEntry {
                parent,
                kind: block.kind,
                content,
            },
        );
    }

    /// Remove the child at `index` of `parent`, with its subtree.
    pub(crate) fn detach(&mut self, parent: BlockId, index: usize) -> Option<DetachedBlock> {
        let Some(BlockEntry {
            content: BlockContent::Children(children),
            ..
        }) = self.blocks.get_mut(&parent)
        else {
            return None;
        };
        if index >= children.len() {
            return None;
        }
        let id = children.remove(index);
        self.structure_dirty = true;
        self.detach_entry(id)
    }

    fn detach_entry(&mut self, id: BlockId) -> Option<DetachedBlock> {
        let entry = self.blocks.remove(&id)?;
        let content = match entry.content {
            BlockContent::Children(children) => super::step::DetachedContent::Children(
                children
                    .into_iter()
                    .filter_map(|child| self.detach_entry(child))
                    .collect(),
            ),
            other => super::step::DetachedContent::Leaf(other),
        };
        Some(DetachedBlock {
            id,
            kind: entry.kind,
            content,
        })
    }

    /// Structural invariants (design ยง5.1) plus N1โ€“N3 for every inline leaf.
    pub(crate) fn check_invariants(&self) -> Result<(), String> {
        let mut seen = 0;
        let mut stack = vec![(self.root, None)];
        while let Some((id, parent)) = stack.pop() {
            seen += 1;
            let entry = self.blocks.get(&id).ok_or(format!("missing {id:?}"))?;
            if entry.parent != parent {
                return Err(format!("bad parent link on {id:?}"));
            }
            match &entry.content {
                BlockContent::Children(children) => {
                    if !entry.kind.is_container() {
                        return Err(format!("{:?} has children", entry.kind));
                    }
                    if children.is_empty() && entry.kind != BlockKindData::Root {
                        return Err(format!("empty container {:?}", entry.kind));
                    }
                    if let BlockKindData::List { .. } = entry.kind
                        && children
                            .iter()
                            .any(|c| !matches!(self.kind(*c), Some(BlockKindData::ListItem { .. })))
                    {
                        return Err("list holds a non-item".into());
                    }
                    if let BlockKindData::ListItem { .. } = entry.kind
                        && !children.first().is_some_and(|c| self.is_caret_leaf(*c))
                    {
                        return Err("list item does not start with a leaf".into());
                    }
                    stack.extend(children.iter().map(|c| (*c, Some(id))));
                }
                BlockContent::Inline(inline) if !inline.is_normalized() => {
                    return Err(format!("inline text of {id:?} not normalized"));
                }
                _ => {}
            }
        }
        if seen != self.blocks.len() {
            return Err("orphaned entries".into());
        }
        if self.walk_leaves().is_empty() {
            return Err("no caret leaf".into());
        }
        Ok(())
    }
}

/// Borrowed read projection of the document, like the dock's `PaneRef`.
#[derive(Clone, Copy)]
pub struct DocumentRef<'a> {
    pub(crate) doc: &'a EditorDocument,
}

impl<'a> DocumentRef<'a> {
    pub fn root(&self) -> BlockRef<'a> {
        BlockRef {
            doc: self.doc,
            id: self.doc.root,
        }
    }

    pub fn block(&self, id: BlockId) -> Option<BlockRef<'a>> {
        self.doc.get(id).map(|_| BlockRef { doc: self.doc, id })
    }

    pub fn first_caret(&self) -> BlockPosition {
        self.doc.first_caret()
    }

    pub fn last_caret(&self) -> BlockPosition {
        self.doc.last_caret()
    }

    /// Caret-bearing leaves in document order (O(1) per step, from the order
    /// index).
    pub fn leaves(&self) -> impl Iterator<Item = BlockRef<'a>> + 'a {
        let doc = self.doc;
        doc.leaves().iter().map(move |id| BlockRef { doc, id: *id })
    }
}

/// Borrowed read projection of one block.
#[derive(Clone, Copy)]
pub struct BlockRef<'a> {
    doc: &'a EditorDocument,
    id: BlockId,
}

impl<'a> BlockRef<'a> {
    pub fn id(&self) -> BlockId {
        self.id
    }

    pub fn kind(&self) -> BlockKind {
        self.doc
            .kind(self.id)
            .map(BlockKindData::public)
            .unwrap_or(BlockKind::Unknown)
    }

    /// The flat text of a text-bearing leaf (inline atoms show as U+FFFC).
    pub fn text(&self) -> Option<&'a str> {
        match &self.doc.get(self.id)?.content {
            BlockContent::Inline(inline) => Some(inline.text()),
            BlockContent::Plain(text) => Some(text),
            _ => None,
        }
    }

    pub fn parent(&self) -> Option<BlockRef<'a>> {
        self.doc
            .parent(self.id)
            .map(|id| BlockRef { doc: self.doc, id })
    }

    pub fn children(&self) -> impl Iterator<Item = BlockRef<'a>> + 'a {
        let doc = self.doc;
        doc.children(self.id)
            .iter()
            .map(move |id| BlockRef { doc, id: *id })
    }
}