edamame 0.1.5

A fast TUI Markdown editor and viewer
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! List structure-editing primitives: [`ListInfo`] + source + cursor byte in, a
//! `ContinueResult` (delta and post-edit cursor) out.  Pure; callers feed the delta through
//! `apply_byte_delta`.

use crate::document::EditDelta;
use crate::editor::list_edit::parse::{
    cursor_item_idx, line_end_byte, line_start_byte, parse_line_start, ContinueResult, ListInfo,
    ListItemInfo, MarkerKind,
};
use crate::markdown::{is_closing_fence, parse_opening_fence};

/// Continue the list at `cursor_byte` with a new empty item, renumbering later ordered
/// items.  `None` (caller falls through to a plain newline) when the cursor is inside the
/// marker prefix, mid-continuation-line, or outside every item.
pub fn continue_item(info: &ListInfo, source: &str, cursor_byte: usize) -> Option<ContinueResult> {
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    let item = &info.items[item_idx];

    if cursor_byte < item.marker_end {
        return None;
    }

    // On a continuation line only the very end of the item continues; splitting a
    // continuation paragraph must not mint a new marker.
    if cursor_byte > item.line_end {
        let content_end = if item.end > item.start && source.as_bytes()[item.end - 1] == b'\n' {
            item.end - 1
        } else {
            item.end
        };
        if cursor_byte != content_end {
            return None;
        }
    }

    let new_number = match info.kind {
        MarkerKind::Ordered(_) => item.number.unwrap_or(0) + 1,
        MarkerKind::Bullet(_) => 0,
    };
    let marker_text = render_marker(&info.indent, info.kind, new_number);
    let task_prefix = if item.task.is_some() { "[ ] " } else { "" };
    let new_prefix = format!("{marker_text}{task_prefix}");

    let tail = &source[cursor_byte..item.end];

    let mut rebuilt_rest = String::new();
    rebuilt_rest.push('\n');
    rebuilt_rest.push_str(&new_prefix);
    rebuilt_rest.push_str(tail);

    let cursor_target = cursor_byte + 1 + new_prefix.len();

    if let MarkerKind::Ordered(delim) = info.kind {
        for (tail_num, item_next) in (new_number + 1..).zip(&info.items[item_idx + 1..]) {
            let renumbered_line = format!(
                "{}{}{delim} {}",
                info.indent,
                tail_num,
                trim_marker_prefix(source, item_next),
            );
            rebuilt_rest.push_str(&renumbered_line);
        }
    } else {
        let tail_start = info
            .items
            .get(item_idx + 1)
            .map(|it| it.start)
            .unwrap_or(info.end);
        rebuilt_rest.push_str(&source[tail_start..info.end]);
    }

    let removed = source[cursor_byte..info.end].to_owned();

    Some(ContinueResult {
        delta: EditDelta {
            offset: cursor_byte,
            removed,
            inserted: rebuilt_rest,
        },
        cursor_byte: cursor_target,
    })
}

/// Break the list at the cursor's empty item — the last step of the triple-`Enter`
/// sequence (`continue_item` → [`space_out_empty_item`] → `exit_list`).  With a blank line
/// already above, the marker is simply stripped; without one, a single newline is inserted
/// so the parser's blank-line split still separates head from tail.  Trailing ordered items
/// are renumbered from 1 since they render as a fresh list.
pub fn exit_list(info: &ListInfo, source: &str, cursor_byte: usize) -> Option<ContinueResult> {
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    let item = &info.items[item_idx];
    if !item.content_is_empty(source) {
        return None;
    }

    let trailing = &info.items[item_idx + 1..];
    let blank_above = is_blank_line_above(source, item.start);

    if trailing.is_empty() {
        let removed = source[item.start..item.end].to_owned();
        let (inserted, cursor_target) = if blank_above {
            (String::new(), item.start)
        } else {
            ("\n".to_owned(), item.start + 1)
        };
        return Some(ContinueResult {
            delta: EditDelta {
                offset: item.start,
                removed,
                inserted,
            },
            cursor_byte: cursor_target,
        });
    }

    let mut inserted = if blank_above {
        String::new()
    } else {
        String::from("\n")
    };
    match info.kind {
        MarkerKind::Ordered(delim) => {
            for (k, trailing_item) in trailing.iter().enumerate() {
                let new_num = (k as u64) + 1;
                inserted.push_str(&info.indent);
                inserted.push_str(&new_num.to_string());
                inserted.push(delim);
                inserted.push(' ');
                inserted.push_str(&source[trailing_item.marker_end..trailing_item.end]);
            }
        }
        MarkerKind::Bullet(_) => {
            let tail_start = trailing[0].start;
            inserted.push_str(&source[tail_start..info.end]);
        }
    }

    let removed = source[item.start..info.end].to_owned();
    // Cursor lands on the separating blank line.
    let cursor_target = if blank_above {
        item.start.saturating_sub(1)
    } else {
        item.start
    };

    Some(ContinueResult {
        delta: EditDelta {
            offset: item.start,
            removed,
            inserted,
        },
        cursor_byte: cursor_target,
    })
}

/// Insert one blank line above an already-empty item, keeping the item and cursor in place:
/// the second step of the triple-`Enter` sequence.  `None` for a non-empty item (callers
/// route those to [`continue_item`]).
pub fn space_out_empty_item(
    info: &ListInfo,
    source: &str,
    cursor_byte: usize,
) -> Option<ContinueResult> {
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    let item = &info.items[item_idx];
    if !item.content_is_empty(source) {
        return None;
    }
    Some(ContinueResult {
        delta: EditDelta {
            offset: item.start,
            removed: String::new(),
            inserted: "\n".to_owned(),
        },
        cursor_byte: cursor_byte + 1,
    })
}

/// True when the line above `item_start` is blank, or there is none — so an empty item on
/// the buffer's first line exits on one `Enter` instead of spending a keystroke on a gap.
pub fn is_blank_line_above(source: &str, item_start: usize) -> bool {
    if item_start == 0 {
        return true;
    }
    let bytes = source.as_bytes();
    if item_start > bytes.len() || bytes[item_start - 1] != b'\n' {
        return false;
    }
    let mut prev_line_start = item_start - 1;
    while prev_line_start > 0 && bytes[prev_line_start - 1] != b'\n' {
        prev_line_start -= 1;
    }
    let prev_line = &source[prev_line_start..item_start - 1];
    prev_line.chars().all(char::is_whitespace)
}

/// Toggle the checkbox of the task item at `cursor_byte`.
pub fn toggle_checkbox(
    info: &ListInfo,
    source: &str,
    cursor_byte: usize,
) -> Option<ContinueResult> {
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    let item = &info.items[item_idx];
    let (checked, box_off) = match (item.task, item.task_box) {
        (Some(c), Some(b)) => (c, b),
        _ => return None,
    };
    let new_char = if checked { ' ' } else { 'x' };
    let removed = source[box_off + 1..box_off + 2].to_owned();
    let inserted = new_char.to_string();
    Some(ContinueResult {
        delta: EditDelta {
            offset: box_off + 1,
            removed,
            inserted,
        },
        cursor_byte,
    })
}

/// Indent the item at `cursor_byte` by `indent_width` spaces into a nested list; an ordered
/// item restarts at 1 and the outer items renumber around it.  `None` on the list's first
/// item: with no sibling to nest under, the indent would degrade the marker into a lazy
/// continuation or an indented code block.
pub fn indent_item(
    info: &ListInfo,
    source: &str,
    cursor_byte: usize,
    indent_width: usize,
) -> Option<ContinueResult> {
    if indent_width == 0 {
        return None;
    }
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    if item_idx == 0 {
        return None;
    }
    let tab_str: String = " ".repeat(indent_width);

    if let MarkerKind::Bullet(_) = info.kind {
        let item = &info.items[item_idx];
        let text = &source[item.start..item.end];
        let mut out = String::new();
        // An empty indented `    - ` parses as a setext H2 underline of the previous item
        // (CommonMark 4.3); a blank line before it forces the nested-list reading.
        let mut shift_before_cursor = 0usize;
        if item.content_is_empty(source) {
            out.push('\n');
            shift_before_cursor += 1;
        }
        let mut pos = item.start;
        for line in text.split_inclusive('\n') {
            if !line.trim().is_empty() {
                out.push_str(&tab_str);
                if cursor_byte >= pos {
                    shift_before_cursor += indent_width;
                }
            }
            out.push_str(line);
            pos += line.len();
        }
        return Some(ContinueResult {
            delta: EditDelta {
                offset: item.start,
                removed: text.to_owned(),
                inserted: out,
            },
            cursor_byte: cursor_byte + shift_before_cursor,
        });
    }

    let MarkerKind::Ordered(delim) = info.kind else {
        unreachable!();
    };
    let base = info.items[0].number.unwrap_or(1);
    let mut out = String::new();
    let mut cursor_out: usize = 0;
    let mut outer_counter = base;
    let nested_indent = format!("{}{}", info.indent, tab_str);

    for (i, item) in info.items.iter().enumerate() {
        let rest = &source[item.marker_end..item.end];
        // Same setext guard as the bullet branch.
        if i == item_idx && item.content_is_empty(source) {
            out.push('\n');
        }
        let new_marker = if i == item_idx {
            format!("{nested_indent}1{delim} ")
        } else {
            let m = format!("{}{outer_counter}{delim} ", info.indent);
            outer_counter += 1;
            m
        };
        let marker_out_start = out.len();
        out.push_str(&new_marker);
        if i == item_idx {
            let in_item = cursor_byte.saturating_sub(item.marker_end).min(rest.len());
            let mut extra_before_cursor = 0usize;
            let mut pos = 0usize;
            for (li, line) in rest.split_inclusive('\n').enumerate() {
                if li > 0 && !line.trim().is_empty() {
                    out.push_str(&tab_str);
                    if in_item >= pos {
                        extra_before_cursor += indent_width;
                    }
                }
                out.push_str(line);
                pos += line.len();
            }
            cursor_out = marker_out_start + new_marker.len() + in_item + extra_before_cursor;
        } else {
            out.push_str(rest);
        }
    }

    let removed = source[info.start..info.end].to_owned();
    Some(ContinueResult {
        delta: EditDelta {
            offset: info.start,
            removed,
            inserted: out,
        },
        cursor_byte: info.start + cursor_out,
    })
}

/// Outdent the item at `cursor_byte` by up to `indent_width` leading spaces on each of its
/// non-blank lines; `None` at the outermost level.
pub fn outdent_item(
    info: &ListInfo,
    source: &str,
    cursor_byte: usize,
    indent_width: usize,
) -> Option<ContinueResult> {
    if indent_width == 0 {
        return None;
    }
    let item_idx = cursor_item_idx(info, cursor_byte)?;
    let item = &info.items[item_idx];
    let indent_len = info.indent.len();
    if indent_len == 0 {
        return None;
    }
    let strip = indent_width.min(indent_len);

    // A hand-written continuation line with less indent than `strip` loses only what it has.
    let text = &source[item.start..item.end];
    let mut out = String::new();
    let mut removed_before_cursor = 0usize;
    let mut pos = item.start;
    for line in text.split_inclusive('\n') {
        let lead = line.chars().take_while(|&c| c == ' ' || c == '\t').count();
        let s = if line.trim().is_empty() {
            0
        } else {
            strip.min(lead)
        };
        out.push_str(&line[s..]);
        if cursor_byte >= pos + s {
            removed_before_cursor += s;
        } else if cursor_byte > pos {
            removed_before_cursor += cursor_byte - pos;
        }
        pos += line.len();
    }
    Some(ContinueResult {
        delta: EditDelta {
            offset: item.start,
            removed: text.to_owned(),
            inserted: out,
        },
        cursor_byte: cursor_byte - removed_before_cursor,
    })
}

/// Renumber every ordered run in the list block around `cursor_byte`, nesting-aware.  Pure
/// (no parse), so cheap enough for the post-edit hook on every keystroke.
///
/// The block is the maximal run of list lines, crossing *loose-list blank gaps* (a blank
/// run whose next non-blank line is a list line) but bounded by non-list content, matching
/// pulldown-cmark's grouping so the numbers match the render.  Crossing into a bullet or
/// differently-delimited run is harmless: the counter restarts on a delimiter change.
///
/// `None` when the cursor is not on a list line (interior blanks included) or nothing
/// changes, so callers record no spare undo step.
pub fn renumber_list_block(source: &str, cursor_byte: usize) -> Option<EditDelta> {
    if source.is_empty() {
        return None;
    }
    let bytes = source.as_bytes();
    let clamped = cursor_byte.min(source.len());
    let cur_start = line_start_byte(bytes, clamped);
    let cur_content_end = line_end_byte(bytes, cur_start);
    let cur_line = &source[cur_start..cur_content_end];
    if parse_line_start(cur_line).is_none() && !is_block_continuation_line(cur_line) {
        return None;
    }

    let line_end_incl = |content_end: usize| {
        if content_end < source.len() && bytes[content_end] == b'\n' {
            content_end + 1
        } else {
            content_end
        }
    };

    // A list line commits the block start; a blank is absorbed only when a list line
    // further up commits past it.
    let mut block_start = cur_start;
    let mut probe = cur_start;
    while probe > 0 {
        let prev_nl = probe - 1;
        if bytes[prev_nl] != b'\n' {
            break;
        }
        let ps = line_start_byte(bytes, prev_nl);
        let prev = &source[ps..prev_nl];
        if parse_line_start(prev).is_some() || is_block_continuation_line(prev) {
            block_start = ps;
        } else if !prev.trim().is_empty() {
            break;
        }
        probe = ps;
    }

    let mut block_end = line_end_incl(cur_content_end);
    let mut probe = block_end;
    while probe < source.len() {
        let next_end = line_end_byte(bytes, probe);
        let next = &source[probe..next_end];
        let after = line_end_incl(next_end);
        if parse_line_start(next).is_some() || is_block_continuation_line(next) {
            block_end = after;
        } else if !next.trim().is_empty() {
            break;
        }
        probe = after;
    }

    renumber_ordered_runs_in_range(source, block_start, block_end)
}

/// Indented non-blank line, counted into the block by [`renumber_list_block`] without a
/// list-identity check — over-inclusion is harmless since only ordered markers get rewritten.
fn is_block_continuation_line(line: &str) -> bool {
    (line.starts_with(' ') || line.starts_with('\t')) && !line.trim().is_empty()
}

/// Renumber every ordered run inside `start..end`, nesting-aware: an outer list keeps
/// counting across a nested child, each nested list restarts under its parent, and each
/// run keeps its first number as the start (the renderer's `start.unwrap_or(1)`).  The
/// range must be a whole list block including loose-list blank gaps, as
/// [`renumber_list_block`] computes it; a blank-bounded range would diverge from the render.
/// `None` when nothing changes.
pub fn renumber_ordered_runs_in_range(source: &str, start: usize, end: usize) -> Option<EditDelta> {
    if start >= end || end > source.len() {
        return None;
    }
    let block = &source[start..end];
    let mut out = String::with_capacity(block.len());
    // `(indent_len, delimiter, next_number)`.
    let mut stack: Vec<(usize, char, u64)> = Vec::new();
    let mut changed = false;
    // Marker-shaped lines inside a fenced code block are literal; rewriting them would
    // corrupt the code.
    let mut fence: Option<(char, usize)> = None;
    let mut rest = block;
    while !rest.is_empty() {
        let (line, tail, had_nl) = match rest.find('\n') {
            Some(i) => (&rest[..i], &rest[i + 1..], true),
            None => (rest, "", false),
        };

        let in_fence = match fence {
            Some((c, count)) => {
                if is_closing_fence(line, c, count) {
                    fence = None;
                }
                true
            }
            None => match parse_opening_fence(line) {
                Some((c, count)) => {
                    fence = Some((c, count));
                    true
                }
                None => false,
            },
        };

        match (in_fence, parse_line_start(line)) {
            (false, Some((indent, MarkerKind::Ordered(delim), Some(num)))) => {
                let k = indent.len();
                while stack.last().is_some_and(|&(ki, _, _)| ki > k) {
                    stack.pop();
                }
                let new_num = match stack.last_mut() {
                    Some((ki, d, counter)) if *ki == k && *d == delim => {
                        let n = *counter;
                        *counter += 1;
                        n
                    }
                    _ => {
                        // A same-indent run with another delimiter is a different list.
                        if stack.last().is_some_and(|&(ki, _, _)| ki == k) {
                            stack.pop();
                        }
                        stack.push((k, delim, num + 1));
                        num
                    }
                };
                // Measure the digit run from the source: `01.` is wider than `num`.
                let digits = line[indent.len()..]
                    .bytes()
                    .take_while(u8::is_ascii_digit)
                    .count();
                let marker_len = indent.len() + digits + 2;
                out.push_str(&indent);
                out.push_str(&new_num.to_string());
                out.push(delim);
                out.push(' ');
                out.push_str(&line[marker_len..]);
                changed |= new_num != num;
            }
            (false, other) => {
                // A bullet sibling ends any ordered run at its indent or deeper.
                if let Some((indent, _, _)) = other {
                    let k = indent.len();
                    while stack.last().is_some_and(|&(ki, _, _)| ki >= k) {
                        stack.pop();
                    }
                }
                out.push_str(line);
            }
            (true, _) => {
                out.push_str(line);
            }
        }
        if had_nl {
            out.push('\n');
        }
        rest = tail;
    }

    if !changed {
        return None;
    }
    Some(EditDelta {
        offset: start,
        removed: block.to_owned(),
        inserted: out,
    })
}

fn render_marker(indent: &str, kind: MarkerKind, number: u64) -> String {
    match kind {
        MarkerKind::Bullet(c) => format!("{indent}{c} "),
        MarkerKind::Ordered(delim) => format!("{indent}{number}{delim} "),
    }
}

/// Everything in `item` after its marker prefix, through `item.end`.
fn trim_marker_prefix<'a>(source: &'a str, item: &ListItemInfo) -> &'a str {
    &source[item.marker_end..item.end]
}