caretline 0.3.0

A replayable, drivable text-editing engine: Helix's editing model in a pure Elm architecture. The whole editor is one serializable state; every input is a message.
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
//! Markdown in and out of an outline: pasted Markdown read into blocks, a selection written as
//! Markdown for the clipboard, and a whole outline document read from and written to a
//! Markdown file.

use crate::outline::{default_gap, derive, numbered_marker, parse_str, BlockInfo, Hang, Kind, NewBlock, Outline, OutlineConfig};
use crate::state::{State, Viewport};

/// Markdown read as blocks: paragraphs (their lines joined), `-` `*` `+` bullets nested by
/// their first indent, numbered items, tagged bullets `- [c] ` when `c` is one of the config's
/// tags (a bare `[c] ` line too), headings, quotes
/// and rules as one-line paragraphs, and fences, tables and front matter as one paragraph
/// with its line breaks. Images are left out and counted. `plain`: a paragraph per block of
/// lines, line breaks kept, nothing read as a list.

/// A bullet's tag at the start of `rest` (`[c] `, with `c` in `cfg.tags`; GFM's `[X]` reads as
/// `[x]` when only `x` is a tag): the tag and the chars it takes.
fn tag_at(rest: &str, cfg: &OutlineConfig) -> Option<char> {
    let mut cs = rest.chars();
    if cs.next() != Some('[') {
        return None;
    }
    let c = cs.next()?;
    if cs.next() != Some(']') || cs.next() != Some(' ') {
        return None;
    }
    if cfg.is_tag(c) {
        Some(c)
    } else if c == 'X' && cfg.is_tag('x') {
        Some('x')
    } else {
        None
    }
}

pub fn parse_markdown(input: &str, plain: bool, cfg: &OutlineConfig) -> (Vec<NewBlock>, usize) {
    let text = input.replace("\r\n", "\n").replace('\r', "\n");
    let mut raw: Vec<String> = text.split('\n').map(|l| l.replace('\t', "    ")).collect();
    while raw.last().is_some_and(|l| l.trim().is_empty()) {
        raw.pop();
    }
    while raw.first().is_some_and(|l| l.trim().is_empty()) {
        raw.remove(0);
    }
    let mut out: Vec<NewBlock> = Vec::new();
    if plain {
        let mut block: Vec<String> = Vec::new();
        for l in raw.into_iter().chain(std::iter::once(String::new())) {
            if l.trim().is_empty() {
                if !block.is_empty() {
                    out.push(NewBlock::para(&block.join("\n")));
                    block.clear();
                }
            } else {
                block.push(l);
            }
        }
        return (out, 0);
    }
    let mut images = 0;
    for l in raw.iter_mut() {
        while let (Some(a), true) = (l.find("!["), l.contains("](")) {
            let Some(close) = l[a..].find(')').map(|k| a + k) else { break };
            l.replace_range(a..=close, "");
            images += 1;
        }
        let trimmed = l.trim_end().len();
        l.truncate(trimmed);
    }
    // (indent, marker, body) of a list line.
    let item = |l: &str| -> Option<(usize, String, String)> {
        let indent = l.len() - l.trim_start().len();
        let rest = l.trim_start();
        if let Some(b) = rest.strip_prefix("- ").or_else(|| rest.strip_prefix("* ")).or_else(|| rest.strip_prefix("+ ")) {
            return Some((indent, "-".into(), b.to_string()));
        }
        if tag_at(rest, cfg).is_some() {
            return Some((indent, "-".into(), rest.to_string()));
        }
        let n = numbered_marker(rest)?;
        Some((indent, rest[..n - 1].to_string(), rest[n..].to_string()))
    };
    let unit = raw.iter().filter_map(|l| item(l)).map(|(i, _, _)| i).find(|i| *i > 0).unwrap_or(2);
    let mut para: Vec<String> = Vec::new();
    // The indentation of the paragraph being read: an indented paragraph is nested.
    let mut para_indent = 0usize;
    let flush = |para: &mut Vec<String>, out: &mut Vec<NewBlock>, indent: usize| {
        if !para.is_empty() {
            out.push(NewBlock { depth: (indent / unit) as u16, ..NewBlock::para(&para.join(" ")) });
            para.clear();
        }
    };
    let mut i = 0;
    if raw.first().map(String::as_str) == Some("---") {
        if let Some(close) = raw.iter().skip(1).position(|l| l == "---").map(|k| k + 1) {
            let mut body = vec!["```".to_string()];
            body.extend(raw[1..close].iter().cloned());
            body.push("```".into());
            out.push(NewBlock::para(&body.join("\n")));
            i = close + 1;
        }
    }
    let mut last_indent: Option<usize> = None;
    while i < raw.len() {
        let l = raw[i].clone();
        let t = l.trim();
        if t.is_empty() {
            flush(&mut para, &mut out, para_indent);
            last_indent = None;
            i += 1;
            continue;
        }
        if t.starts_with("```") {
            flush(&mut para, &mut out, para_indent);
            let mut body = vec![t.to_string()];
            i += 1;
            while i < raw.len() {
                body.push(raw[i].clone());
                i += 1;
                if raw[i - 1].trim().starts_with("```") {
                    break;
                }
            }
            out.push(NewBlock::para(&body.join("\n")));
            last_indent = None;
            continue;
        }
        if t.starts_with('|') {
            flush(&mut para, &mut out, para_indent);
            let mut rows = Vec::new();
            while i < raw.len() && raw[i].trim().starts_with('|') {
                rows.push(raw[i].trim().to_string());
                i += 1;
            }
            out.push(NewBlock::para(&rows.join("\n")));
            last_indent = None;
            continue;
        }
        if let Some((indent, marker, body)) = item(&l) {
            flush(&mut para, &mut out, para_indent);
            let mut nb = NewBlock { depth: (indent / unit) as u16, kind: Kind::Bullet, tag: None, text: body, gap: None, mark: None };
            if let Some(c) = tag_at(&nb.text, cfg) {
                nb.tag = Some(c);
                nb.text = nb.text.chars().skip(4).collect();
            }
            if marker != "-" {
                nb.text = format!("{marker} {}", nb.text);
            }
            out.push(nb);
            last_indent = Some(indent);
            i += 1;
            continue;
        }
        if let (Some(li), Some(last)) = (last_indent, out.last_mut()) {
            if l.len() - l.trim_start().len() > li {
                last.text.push(' ');
                last.text.push_str(t);
                i += 1;
                continue;
            }
        }
        last_indent = None;
        if t.starts_with('#') || t.starts_with('>') || t == "---" || t == "***" {
            flush(&mut para, &mut out, para_indent);
            out.push(NewBlock::para(t));
        } else {
            if para.is_empty() {
                para_indent = l.len() - l.trim_start().len();
            }
            para.push(t.to_string());
        }
        i += 1;
    }
    flush(&mut para, &mut out, para_indent);
    // Depth never jumps more than one below the item above.
    // A paragraph is at most one below the block above too (it nests like any block).
    let mut prev: isize = -1;
    for b in out.iter_mut() {
        b.depth = b.depth.min((prev + 1) as u16);
        prev = b.depth as isize;
    }
    (out, images)
}

/// The selection `[from, to)` as Markdown (a copy). Inside one block: its plain text. Across
/// blocks: the first block's text from `from` (with its marker only when `from` is its
/// content start), then every later block with its marker and indentation, a blank line
/// around paragraphs and none between list items. Soft breaks are line breaks.
pub fn to_markdown(state: &State, o: &Outline, from: usize, to: usize) -> String {
    to_markdown_with(state, o, from, to, &|_| None)
}

/// [`to_markdown`] with a host's text after each block's first line (across blocks): what a
/// host keeps per block outside the text, written back into the copy.
pub fn to_markdown_with(state: &State, o: &Outline, from: usize, to: usize, suffix: &dyn Fn(crate::marks::MarkId) -> Option<String>) -> String {
    let text = state.doc.text.slice(..);
    let piece = |a: usize, b: usize| text.slice(a..b.max(a)).to_string().replace("\r\n", "\n");
    let parts: Vec<(&BlockInfo, String)> = o
        .indices_between(text, from, to)
        .map(|i| {
            let b = &o.blocks[i];
            (b, piece(from.max(b.content_start()), to.min(b.end)))
        })
        .collect();
    if parts.len() == 1 {
        return parts[0].1.clone();
    }
    // Depth relative to the shallowest block copied (a paragraph's children are indented under it).
    let base = parts.iter().map(|(b, _)| b.depth).min().unwrap_or(0);
    let first = parts[0].0;
    let from_start = from <= first.content_start();
    let mut out = String::new();
    let mut prev_para: Option<bool> = None;
    for (k, (b, part)) in parts.iter().enumerate() {
        let para = b.kind == Kind::Para;
        if prev_para.is_some_and(|p| para || p) {
            out.push('\n');
        }
        let with_marker = k > 0 || from_start;
        let pad = "  ".repeat(b.depth.saturating_sub(base) as usize);
        if para {
            let mut rows = part.split('\n');
            if with_marker {
                out.push_str(&pad);
                if matches!(b.hang, Hang::Heading(_) | Hang::Quote) {
                    out.push_str(&piece(b.start + b.indent, b.content_start()));
                }
            }
            out.push_str(rows.next().unwrap_or(""));
            out.push_str(&suffix(b.id).unwrap_or_default());
            for r in rows {
                out.push('\n');
                out.push_str(&pad);
                out.push_str(r);
            }
        } else {
            let marker = match (b.hang, b.tag) {
                (_, Some(c)) => format!("- [{c}] "),
                (Hang::Number(_), _) => piece(b.start + b.indent, b.content_start()),
                _ => "- ".into(),
            };
            let mut rows = part.split('\n');
            if with_marker {
                out.push_str(&pad);
                out.push_str(&marker);
            }
            out.push_str(rows.next().unwrap_or(""));
            out.push_str(&suffix(b.id).unwrap_or_default());
            for r in rows {
                out.push('\n');
                out.push_str(&pad);
                out.push_str("  ");
                out.push_str(r);
            }
        }
        out.push('\n');
        prev_para = Some(para);
    }
    out.strip_suffix('\n').unwrap_or(&out).to_string()
}

/// The continuation indent of a block in a Markdown file: past an item's marker (for a tagged
/// bullet, past its `- `), or the paragraph's indentation.
fn continuation_indent(b: &BlockInfo) -> usize {
    match b.kind {
        Kind::Para => b.indent,
        Kind::Bullet if b.tag.is_some() => b.indent + 2,
        Kind::Bullet => b.prefix_len,
    }
}

/// The whole outline document as a Markdown file: a blank line where a block has a blank row
/// before it, continuation lines indented under their item, and a final line break. Empty
/// paragraphs (a fresh line to type on) are left out.
pub fn to_file(state: &State) -> String {
    let Some(o) = state.blocks() else { return state.doc.text.to_string() };
    let text = state.doc.text.slice(..);
    let mut lines: Vec<String> = Vec::new();
    for b in &o.blocks {
        if b.kind == Kind::Para && b.hang == Hang::None && b.is_empty() && b.line_count == 1 {
            continue;
        }
        if b.gap && !lines.is_empty() {
            lines.push(String::new());
        }
        let pad = " ".repeat(continuation_indent(b));
        for (k, line) in (b.first_line..=b.last_line()).enumerate() {
            let s = text.line(line).to_string();
            let s = s.trim_end_matches(['\n', '\r']);
            if k == 0 || b.fence || s.is_empty() {
                lines.push(s.to_string());
            } else {
                lines.push(format!("{pad}{s}"));
            }
        }
    }
    let mut out = lines.join("\n");
    out.push('\n');
    out
}

/// Reads a Markdown file into outline buffer text and its block starts: `(text, starts)`,
/// each start a line index and whether a blank line came before it. Blank lines separate
/// blocks (outside fences), a marker line starts a block, and any other line continues the
/// block above (its continuation indent removed). Line breaks become `\n`.
pub fn from_file(md: &str, cfg: &OutlineConfig) -> (String, Vec<(usize, bool)>) {
    let md = md.replace("\r\n", "\n");
    let mut out: Vec<String> = Vec::new();
    let mut starts: Vec<(usize, bool)> = Vec::new();
    let mut blank = false;
    let mut in_fence = false;
    let mut cont = 0usize;
    for raw in md.split('\n') {
        if in_fence {
            out.push(raw.to_string());
            if raw.trim_start().starts_with("```") {
                in_fence = false;
            }
            continue;
        }
        if raw.trim().is_empty() {
            blank = true;
            continue;
        }
        let p = parse_str(raw, cfg);
        if out.is_empty() || blank || p.marker {
            starts.push((out.len(), blank && !out.is_empty()));
            out.push(raw.to_string());
            cont = match p.kind {
                Kind::Para => p.indent,
                Kind::Bullet if p.tag.is_some() => p.indent + 2,
                Kind::Bullet => p.len,
            };
            in_fence = p.fence;
        } else {
            let strip = raw.chars().take(cont).take_while(|c| *c == ' ').count();
            out.push(raw.chars().skip(strip).collect());
        }
        blank = false;
    }
    (out.join("\n"), starts)
}

/// An outline document from a Markdown file's text: blocks, marks and blank rows as the file
/// has them, a fresh history, and the document counted as saved.
pub fn load(md: &str, path: Option<String>, viewport: Viewport, cfg: OutlineConfig) -> State {
    let (text, starts) = from_file(md, &cfg);
    let mut state = State::new(&text, path, viewport);
    for &(line, _) in &starts {
        let pos = state.doc.text.line_to_char(line);
        state.doc.marks.mint(pos);
    }
    let o = derive(state.doc.text.slice(..), &state.doc.marks, &cfg);
    let blank: std::collections::HashMap<usize, bool> = starts.into_iter().collect();
    for (i, b) in o.blocks.iter().enumerate() {
        if i == 0 {
            continue;
        }
        let want = blank.get(&b.first_line).copied().unwrap_or(false);
        let default = default_gap(o.blocks.get(i - 1), b);
        if want != default {
            state.doc.marks.set_gap(b.id, Some(want));
        }
    }
    state.enable_outline(cfg);
    state
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tagged() -> OutlineConfig {
        OutlineConfig { tags: " x".into(), ..OutlineConfig::default() }
    }

    #[test]
    fn paste_reads_an_outline() {
        let md = "Intro line\ncontinued\n\n- one\n  - [ ] two\n- [x] three\n1. first\n![x](y.png)\n";
        let (blocks, images) = parse_markdown(md, false, &tagged());
        assert_eq!(images, 1);
        let shape: Vec<(u16, Kind, Option<char>, &str)> = blocks.iter().map(|b| (b.depth, b.kind, b.tag, b.text.as_str())).collect();
        assert_eq!(
            shape,
            [
                (0, Kind::Para, None, "Intro line continued"),
                (0, Kind::Bullet, None, "one"),
                (1, Kind::Bullet, Some(' '), "two"),
                (0, Kind::Bullet, Some('x'), "three"),
                (0, Kind::Bullet, None, "1. first"),
            ]
        );
        // Without tags, a bracket is text.
        let (blocks, _) = parse_markdown(md, false, &OutlineConfig::default());
        assert_eq!((blocks[2].tag, blocks[2].text.as_str()), (None, "[ ] two"));
    }

    #[test]
    fn a_file_round_trips() {
        let md = "# Trip\n\nBooked the flat.\nIt faces the river.\n\n- [ ] Pay the deposit\n  - ask about the desk\n    on two lines\n- [x] Book flights\n\n```\n- not a list\n\n```\n";
        for cfg in [tagged(), OutlineConfig::default()] {
            let s = load(md, None, Viewport { width: 80, height: 24 }, cfg.clone());
            assert_eq!(to_file(&s), md);
            let o = s.blocks().unwrap();
            let kinds: Vec<Kind> = o.blocks.iter().map(|b| b.kind).collect();
            assert_eq!(kinds, [Kind::Para, Kind::Para, Kind::Bullet, Kind::Bullet, Kind::Bullet, Kind::Para]);
            assert_eq!(o.blocks[3].line_count, 2, "the continuation line belongs to its item");
            assert_eq!(o.blocks[2].tag.is_some(), !cfg.tags.is_empty());
        }
    }
}