ochrepad-core 0.3.0

Text model and editing engine of the ochrepad text editor (internal; no stable 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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Resolving an [`Action`] against a document and selection set: motions, word
//! boundaries and the edits each action produces. Pure functions; `Editor`
//! turns the result into a transaction.

use unicode_segmentation::UnicodeSegmentation;

use crate::action::{Action, ActionError, Direction, Motion};
use crate::changeset::{Bias, ChangeSet, Edit};
use crate::history::EditKind;
use crate::position::{
    ByteOffset, VisualCol, byte_to_visual_col, next_grapheme_boundary, prev_grapheme_boundary,
    visual_col_to_byte, visual_col_to_byte_ceil,
};
use std::ops::Range;

use crate::buffer::Indent;
use crate::display::DisplayMap;
use crate::search::{SearchQuery, Searcher};
use crate::selection::{Rectangle, Selection, SelectionSet};
use crate::text::Document;
use crate::transform;

pub(crate) enum Resolved {
    /// Only the selections change.
    Selections(SelectionSet),
    Edit {
        changes: ChangeSet,
        after: SelectionSet,
        kind: EditKind,
    },
}

/// Resolves every action except `Undo`/`Redo`, which `Editor` handles.
/// Vertical motions move by the view's display `rows`.
pub(crate) fn resolve(
    action: &Action,
    doc: &Document,
    sels: &SelectionSet,
    indent: Indent,
    rows: &DisplayMap,
) -> Resolved {
    let tab_width = indent.width;
    match action {
        Action::Indent => transform::indent(doc, sels, &indent_unit(indent)),
        // Tab with a selection over several lines indents them.
        Action::InsertTab if transform::spans_lines(doc, sels) => {
            transform::indent(doc, sels, &indent_unit(indent))
        }
        Action::Unindent => transform::unindent(doc, sels, tab_width),
        Action::ConvertIndentation { to_spaces } => {
            crate::indent::convert(doc, sels, *to_spaces, tab_width)
        }
        Action::GoToLine { line, column } => {
            let line = line.saturating_sub(1).min(doc.line_count() - 1);
            let col = VisualCol(column.saturating_sub(1));
            Resolved::Selections(SelectionSet::caret(visual_col_to_byte(
                doc, line, col, tab_width,
            )))
        }
        Action::GoToMatchingBracket | Action::SelectToMatchingBracket => {
            let select = matches!(action, Action::SelectToMatchingBracket);
            let pair = crate::brackets::bracket_at(doc, sels.primary().head);
            match pair.and_then(|p| p.matching.map(|m| (p, m))) {
                None => Resolved::Selections(sels.clone()),
                Some((p, m)) if select => {
                    let (a, b) = (p.at.min(m), p.at.max(m));
                    Resolved::Selections(SelectionSet::single(Selection::new(
                        a,
                        ByteOffset(b.0 + 1),
                    )))
                }
                // The caret keeps its side: before the bracket stays before.
                Some((p, m)) => Resolved::Selections(SelectionSet::caret(if p.after_caret {
                    m
                } else {
                    ByteOffset(m.0 + 1)
                })),
            }
        }
        Action::InsertTab => {
            // A tab character, or spaces up to the next tab stop.
            edit_each(doc, sels, EditKind::Other, |_, s| {
                let text = if indent.use_spaces {
                    let tw = tab_width.max(1);
                    let col = byte_to_visual_col(doc, s.start(), tab_width).0;
                    " ".repeat(tw - col % tw)
                } else {
                    "\t".to_owned()
                };
                Edit::new(s.start(), s.end(), text)
            })
        }
        Action::InsertText(text) => {
            let kind = if sels.ranges().iter().all(Selection::is_empty)
                && text != "\n"
                && text.graphemes(true).count() == 1
            {
                EditKind::Insert
            } else {
                EditKind::Other
            };
            edit_each(doc, sels, kind, |_, s| {
                Edit::new(s.start(), s.end(), text.as_str())
            })
        }
        Action::Paste(text) => {
            // One line per selection when the counts match (e.g. a multi-caret
            // copy pasted back), otherwise the whole text at every selection.
            let body = text.strip_suffix('\n').unwrap_or(text);
            let lines: Vec<&str> = body.split('\n').collect();
            if sels.len() > 1 && lines.len() == sels.len() {
                edit_each(doc, sels, EditKind::Other, |i, s| {
                    Edit::new(s.start(), s.end(), lines[i])
                })
            } else {
                edit_each(doc, sels, EditKind::Other, |_, s| {
                    Edit::new(s.start(), s.end(), text.as_str())
                })
            }
        }
        Action::ColumnSelect {
            anchor_line,
            anchor_col,
            head_line,
            head_col,
        } => {
            let last = doc.line_count() - 1;
            Resolved::Selections(column_selection(
                doc,
                Rectangle {
                    start_line: (*anchor_line).min(last),
                    end_line: (*head_line).min(last),
                    start_col: *anchor_col,
                    end_col: *head_col,
                },
                tab_width,
            ))
        }
        Action::ColumnExtend(dir) => {
            let mut r = sels.rect().cloned().unwrap_or_else(|| {
                let head = sels.primary().head;
                let line = doc.line_of(head);
                let col = byte_to_visual_col(doc, head, tab_width);
                Rectangle {
                    start_line: line,
                    end_line: line,
                    start_col: col,
                    end_col: col,
                }
            });
            match dir {
                Direction::Up => r.end_line = r.end_line.saturating_sub(1),
                Direction::Down => r.end_line = (r.end_line + 1).min(doc.line_count() - 1),
                Direction::Left => {
                    r.end_col = step_col(doc, r.end_line, r.end_col, false, tab_width)
                }
                Direction::Right => {
                    r.end_col = step_col(doc, r.end_line, r.end_col, true, tab_width)
                }
            }
            Resolved::Selections(column_selection(doc, r, tab_width))
        }
        Action::ConvertCase(mode) => transform::case(doc, sels, *mode),
        Action::SortLines(options) => transform::sort(doc, sels, *options, tab_width),
        Action::ShuffleLines { seed } => transform::shuffle(doc, sels, *seed, tab_width),
        Action::TrimTrailingWhitespace => transform::trim_trailing(doc, sels),
        Action::DuplicateLine => transform::duplicate(doc, sels),
        Action::MoveLinesUp => transform::move_lines(doc, sels, true),
        Action::MoveLinesDown => transform::move_lines(doc, sels, false),
        Action::JoinLines => transform::join(doc, sels),
        Action::SplitLines { width } => transform::split_lines(doc, sels, *width, tab_width),
        Action::RemoveEmptyLines { include_whitespace } => {
            transform::remove_empty(doc, sels, *include_whitespace)
        }
        Action::RemoveDuplicateLines { consecutive_only } => {
            transform::remove_duplicates(doc, sels, *consecutive_only)
        }
        Action::DeleteLine => transform::delete_lines(doc, sels),
        Action::TransposeLines => transform::transpose(doc, sels),
        Action::InsertLineAbove => transform::insert_line(doc, sels, true),
        Action::InsertLineBelow => transform::insert_line(doc, sels, false),
        Action::ReverseLines => transform::reverse(doc, sels),
        Action::TrimLeadingWhitespace => transform::trim_lines(doc, sels, false),
        Action::TrimLeadingAndTrailingWhitespace => transform::trim_lines(doc, sels, true),
        Action::EolToSpace => transform::eol_to_space(doc, sels),
        Action::TabsToSpaces => transform::tabs_to_spaces(doc, sels, tab_width),
        Action::SpacesToTabs => transform::spaces_to_tabs(doc, sels, tab_width),
        Action::ColumnInsert(what) => crate::column_edit::insert(doc, sels, what, tab_width),
        Action::PasteBlock(text) => {
            if sels.len() > 1 || !sels.primary().is_empty() {
                return resolve(&Action::Paste(text.clone()), doc, sels, indent, rows);
            }
            paste_block(doc, sels.primary().head, text, tab_width)
        }
        Action::SetSelections(set) => {
            let clamp = |p: ByteOffset| {
                let mut p = p.min(doc.len());
                while !doc.is_char_boundary(p) {
                    p.0 -= 1;
                }
                p
            };
            let ranges = set
                .ranges()
                .iter()
                .map(|s| Selection {
                    anchor: clamp(s.anchor),
                    head: clamp(s.head),
                    goal: s.goal,
                })
                .collect();
            Resolved::Selections(
                SelectionSet::new(ranges, set.primary_index()).with_rect(set.rect().cloned()),
            )
        }
        Action::InsertNewline => edit_each(doc, sels, EditKind::Other, |_, s| {
            let line = doc.line_of(s.start());
            let prefix = doc.slice(doc.line_start(line)..s.start());
            let indent: String = prefix
                .chars()
                .take_while(|c| *c == ' ' || *c == '\t')
                .collect();
            Edit::new(s.start(), s.end(), format!("\n{indent}"))
        }),
        Action::DeleteBackward => delete(doc, sels, EditKind::DeleteBackward, |p| {
            (prev_grapheme_boundary(doc, p), p)
        }),
        Action::DeleteForward => delete(doc, sels, EditKind::DeleteForward, |p| {
            (p, next_grapheme_boundary(doc, p))
        }),
        Action::DeleteWordBackward => {
            delete(doc, sels, EditKind::Other, |p| (word_left(doc, p), p))
        }
        Action::DeleteWordForward => {
            delete(doc, sels, EditKind::Other, |p| (p, word_right(doc, p)))
        }
        Action::MoveCursor { motion, extend } => {
            Resolved::Selections(sels.map(|s| move_selection(doc, rows, s, *motion, *extend)))
        }
        Action::SelectAll => Resolved::Selections(SelectionSet::single(Selection::new(
            ByteOffset(0),
            doc.len(),
        ))),
        Action::SelectWord => Resolved::Selections(sels.map(|s| {
            let (start, end) = word_at(doc, s.head);
            Selection::new(start, end)
        })),
        Action::SelectLine => Resolved::Selections(sels.map(|s| {
            let first = doc.line_of(s.start());
            let last = doc.line_of(s.end());
            let end = if last + 1 < doc.line_count() {
                doc.line_start(last + 1)
            } else {
                doc.len()
            };
            Selection::new(doc.line_start(first), end)
        })),
        Action::AddCursorAbove => add_cursor(rows, sels, -1),
        Action::AddCursorBelow => add_cursor(rows, sels, 1),
        Action::CollapseSelections => {
            Resolved::Selections(SelectionSet::caret(sels.primary().head))
        }
        // Handled by `resolve_search` / `Editor`.
        Action::Undo
        | Action::Redo
        | Action::Fold
        | Action::Unfold
        | Action::ToggleFold
        | Action::FoldAll
        | Action::UnfoldAll
        | Action::FoldLevel(_)
        | Action::UnfoldLevel(_)
        | Action::ToggleLineComment
        | Action::CommentLines
        | Action::UncommentLines
        | Action::ToggleBlockComment
        | Action::FormatJson { .. }
        | Action::MinifyJson
        | Action::Encode(_)
        | Action::Decode(_)
        | Action::Find { .. }
        | Action::Replace { .. }
        | Action::ReplaceAll { .. }
        | Action::SelectAllMatches(_)
        | Action::MultiSelectNext { .. }
        | Action::MultiSelectAll { .. }
        | Action::ToggleBookmark
        | Action::NextBookmark
        | Action::PrevBookmark
        | Action::ClearBookmarks
        | Action::InverseBookmarks
        | Action::BookmarkMatches(_)
        | Action::RemoveBookmarkedLines
        | Action::RemoveUnbookmarkedLines
        | Action::ReplaceBookmarkedLines(_) => Resolved::Selections(sels.clone()),
    }
}

fn searcher(query: &SearchQuery) -> Result<Searcher, ActionError> {
    Searcher::new(query).map_err(|e| ActionError::InvalidPattern(e.0))
}

/// Resolves the search actions, which can fail.
pub(crate) fn resolve_search(
    action: &Action,
    doc: &Document,
    sels: &SelectionSet,
) -> Result<Resolved, ActionError> {
    match action {
        Action::Find { query, backward } => {
            let m = searcher(query)?
                .next_from(doc, sels, *backward)
                .ok_or(ActionError::NotFound)?;
            Ok(Resolved::Selections(SelectionSet::single(Selection::new(
                m.start, m.end,
            ))))
        }
        Action::SelectAllMatches(query) => {
            let matches = searcher(query)?.find_in(doc, sels);
            if matches.is_empty() {
                return Err(ActionError::NotFound);
            }
            let ranges = matches
                .into_iter()
                .map(|m| Selection::new(m.start, m.end))
                .collect();
            Ok(Resolved::Selections(SelectionSet::new(ranges, 0)))
        }
        Action::ReplaceAll { query, replacement } => {
            let s = searcher(query)?;
            let edits: Vec<Edit> = s
                .find_in(doc, sels)
                .into_iter()
                .map(|m| {
                    let text = s.expand(doc, m.clone(), replacement);
                    Edit::new(m.start, m.end, text)
                })
                .collect();
            let changes = ChangeSet::new(doc.len(), edits);
            if changes.is_empty() {
                return Ok(Resolved::Selections(sels.clone()));
            }
            // Selections keep covering the (replaced) text they covered.
            let after = SelectionSet::new(
                sels.ranges()
                    .iter()
                    .map(|sel| {
                        let start = changes.map_position(sel.start(), Bias::Before);
                        let end = changes.map_position(sel.end(), Bias::After);
                        if sel.head < sel.anchor {
                            Selection::new(end, start)
                        } else {
                            Selection::new(start, end)
                        }
                    })
                    .collect(),
                sels.primary_index(),
            );
            Ok(Resolved::Edit {
                changes,
                after,
                kind: EditKind::Other,
            })
        }
        Action::Replace { query, replacement } => {
            let s = searcher(query)?;
            let select =
                |m: Range<ByteOffset>| SelectionSet::single(Selection::new(m.start, m.end));
            let Some((m, text)) = s.replacement_for_selection(doc, sels, replacement) else {
                // Not on a match: just find the next one.
                let m = s.next_from(doc, sels, false).ok_or(ActionError::NotFound)?;
                return Ok(Resolved::Selections(select(m)));
            };
            let changes = ChangeSet::new(doc.len(), vec![Edit::new(m.start, m.end, text)]);
            // One step: the selection afterwards is the next match, found in
            // the edited text (a rope clone is cheap).
            let caret = SelectionSet::caret(changes.map_position(m.end, Bias::After));
            let mut edited = doc.clone();
            changes.apply(&mut edited);
            let after = s.next_from(&edited, &caret, false).map_or(caret, select);
            if changes.is_empty() {
                return Ok(Resolved::Selections(after));
            }
            Ok(Resolved::Edit {
                changes,
                after,
                kind: EditKind::Other,
            })
        }
        // Only search actions are routed here.
        _ => Ok(Resolved::Selections(sels.clone())),
    }
}

/// One level of indentation: a tab, or `tab_width` spaces.
fn indent_unit(indent: Indent) -> String {
    if indent.use_spaces {
        " ".repeat(indent.width.max(1))
    } else {
        "\t".to_owned()
    }
}

/// One edit per selection (`f` gets its index); afterwards each selection becomes a caret at the
/// end of its replacement.
fn edit_each(
    doc: &Document,
    sels: &SelectionSet,
    kind: EditKind,
    mut f: impl FnMut(usize, &Selection) -> Edit,
) -> Resolved {
    let edits: Vec<Edit> = sels
        .ranges()
        .iter()
        .enumerate()
        .map(|(i, s)| f(i, s))
        .collect();
    let ends: Vec<ByteOffset> = edits.iter().map(|e| e.end).collect();
    let changes = ChangeSet::new(doc.len(), edits);
    let after = SelectionSet::new(
        ends.into_iter()
            .map(|e| Selection::caret(changes.map_position(e, Bias::After)))
            .collect(),
        sels.primary_index(),
    );
    if changes.is_empty() {
        Resolved::Selections(after)
    } else {
        Resolved::Edit {
            changes,
            after,
            kind,
        }
    }
}

/// Deletes each non-empty selection, or the range `range_at(head)` for carets.
/// Only an all-caret delete keeps the coalescible `kind`.
fn delete(
    doc: &Document,
    sels: &SelectionSet,
    kind: EditKind,
    range_at: impl Fn(ByteOffset) -> (ByteOffset, ByteOffset),
) -> Resolved {
    let kind = if sels.ranges().iter().all(Selection::is_empty) {
        kind
    } else {
        EditKind::Other
    };
    edit_each(doc, sels, kind, |_, s| {
        let (start, end) = if s.is_empty() {
            range_at(s.head)
        } else {
            (s.start(), s.end())
        };
        Edit::new(start, end, "")
    })
}

fn move_selection(
    doc: &Document,
    rows: &DisplayMap,
    sel: &Selection,
    motion: Motion,
    extend: bool,
) -> Selection {
    if !extend && !sel.is_empty() {
        match motion {
            Motion::Left => return Selection::caret(sel.start()),
            Motion::Right => return Selection::caret(sel.end()),
            _ => {}
        }
    }
    let head = sel.head;
    let line = doc.line_of(head);
    let (new_head, goal) = match motion {
        Motion::Left => (prev_grapheme_boundary(doc, head), None),
        Motion::Right => (next_grapheme_boundary(doc, head), None),
        Motion::WordLeft => (word_left(doc, head), None),
        Motion::WordRight => (word_right(doc, head), None),
        Motion::Home => (smart_home(doc, line, head), None),
        Motion::RowHome => {
            let row = rows.row_of(head);
            let start = rows.row_range(row).start;
            if row.sub > 0 && head != start {
                (start, None)
            } else {
                (smart_home(doc, line, head), None)
            }
        }
        Motion::RowEnd => {
            let end = rows.row_end(rows.row_of(head));
            if head != end {
                (end, None)
            } else {
                (doc.line_end(line), None)
            }
        }
        Motion::LineStart => (doc.line_start(line), None),
        Motion::LineEnd => (doc.line_end(line), None),
        Motion::DocStart => (ByteOffset(0), None),
        Motion::DocEnd => (doc.len(), None),
        Motion::Up => vertical(rows, sel, -1),
        Motion::Down => vertical(rows, sel, 1),
        Motion::PageUp(n) => vertical(rows, sel, -(n as isize)),
        Motion::PageDown(n) => vertical(rows, sel, n as isize),
    };
    Selection {
        anchor: if extend { sel.anchor } else { new_head },
        head: new_head,
        goal,
    }
}

/// Moves `delta` display rows keeping the remembered visual column (counted
/// from the row's start). On the first or last row the head stays put, as
/// in Notepad++.
fn vertical(rows: &DisplayMap, sel: &Selection, delta: isize) -> (ByteOffset, Option<VisualCol>) {
    let row = rows.row_of(sel.head);
    let goal = sel.goal.unwrap_or_else(|| rows.col_in_row(sel.head));
    let mut target = rows.advance(row, delta);
    // Padding rows hold no text: keep going the same way, or stay put.
    while rows.is_filler(target) {
        let (next, moved) = rows.step(target, delta.signum());
        if moved == 0 {
            target = row;
            break;
        }
        target = next;
    }
    if target == row {
        return (sel.head, Some(goal));
    }
    (rows.byte_at_col(target, goal), Some(goal))
}

/// One selection per line of the rectangle, anchor column to head column;
/// the primary is on the head line. Each selection stays inside the
/// rectangle: a tab or wide glyph straddling an edge is left out.
fn column_selection(doc: &Document, r: Rectangle, tab_width: usize) -> SelectionSet {
    let ranges = (r.top()..=r.bottom())
        .map(|line| {
            let right = visual_col_to_byte(doc, line, r.right(), tab_width);
            let left = visual_col_to_byte_ceil(doc, line, r.left(), tab_width).min(right);
            if r.start_col <= r.end_col {
                Selection::new(left, right)
            } else {
                Selection::new(right, left)
            }
        })
        .collect();
    let primary = r.end_line - r.top();
    SelectionSet::new(ranges, primary).with_rect(Some(r))
}

/// The visual column one grapheme left or right of `col` on `line`; past the
/// end of the line the rectangle keeps growing one column at a time.
fn step_col(
    doc: &Document,
    line: usize,
    col: VisualCol,
    right: bool,
    tab_width: usize,
) -> VisualCol {
    let at = visual_col_to_byte(doc, line, col, tab_width);
    let at_col = byte_to_visual_col(doc, at, tab_width);
    if right {
        if at < doc.line_end(line) && at_col == col {
            byte_to_visual_col(doc, next_grapheme_boundary(doc, at), tab_width)
        } else {
            VisualCol(col.0 + 1)
        }
    } else if col.0 == 0 {
        col
    } else if at_col == col && at > doc.line_start(line) {
        byte_to_visual_col(doc, prev_grapheme_boundary(doc, at), tab_width)
    } else {
        VisualCol(col.0 - 1)
    }
}

fn paste_block(doc: &Document, head: ByteOffset, text: &str, tab_width: usize) -> Resolved {
    let line = doc.line_of(head);
    let col = byte_to_visual_col(doc, head, tab_width);
    let body = text.strip_suffix('\n').unwrap_or(text);
    let mut edits = Vec::new();
    let mut tail = String::new();
    for (i, piece) in body.split('\n').enumerate() {
        let target = line + i;
        if target < doc.line_count() {
            let pos = visual_col_to_byte(doc, target, col, tab_width);
            let have = byte_to_visual_col(doc, pos, tab_width).0;
            let pad = if pos == doc.line_end(target) {
                col.0.saturating_sub(have)
            } else {
                0
            };
            edits.push(Edit::new(pos, pos, format!("{}{piece}", " ".repeat(pad))));
        } else {
            tail.push('\n');
            tail.push_str(&" ".repeat(col.0));
            tail.push_str(piece);
        }
    }
    if !tail.is_empty() {
        edits.push(Edit::new(doc.len(), doc.len(), tail));
    }
    let changes = ChangeSet::new(doc.len(), edits);
    let after = SelectionSet::caret(changes.map_position(head, Bias::After));
    if changes.is_empty() {
        return Resolved::Selections(after);
    }
    Resolved::Edit {
        changes,
        after,
        kind: EditKind::Other,
    }
}

fn add_cursor(rows: &DisplayMap, sels: &SelectionSet, delta: isize) -> Resolved {
    let from = if delta < 0 {
        &sels.ranges()[0]
    } else {
        &sels.ranges()[sels.len() - 1]
    };
    let (pos, goal) = vertical(
        rows,
        &Selection::caret(from.head).with_goal(from.goal),
        delta,
    );
    if rows.row_of(pos) == rows.row_of(from.head) {
        return Resolved::Selections(sels.clone());
    }
    let mut ranges = sels.ranges().to_vec();
    ranges.push(Selection::caret(pos).with_goal(goal));
    let primary = ranges.len() - 1;
    Resolved::Selections(SelectionSet::new(ranges, primary))
}

/// Home: the first non-blank of the line, or column 0 if already there.
fn smart_home(doc: &Document, line: usize, head: ByteOffset) -> ByteOffset {
    let first_non_blank = first_non_blank(doc, line);
    if head == first_non_blank {
        doc.line_start(line)
    } else {
        first_non_blank
    }
}

fn first_non_blank(doc: &Document, line: usize) -> ByteOffset {
    let text = doc.line_text(line);
    let blank = text.len() - text.trim_start_matches([' ', '\t']).len();
    ByteOffset(doc.line_start(line).0 + blank)
}

// ---------------------------------------------------------------------------
// Word boundaries, by grapheme class.

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Class {
    Newline,
    Space,
    Word,
    Punct,
}

fn class_of(g: &str) -> Class {
    match g.chars().next() {
        Some('\n') => Class::Newline,
        Some(c) if c.is_whitespace() => Class::Space,
        Some(c) if c.is_alphanumeric() || c == '_' => Class::Word,
        _ => Class::Punct,
    }
}

/// The grapheme after `p`: its end and class.
fn after(doc: &Document, p: ByteOffset) -> Option<(ByteOffset, Class)> {
    (p < doc.len()).then(|| {
        let n = next_grapheme_boundary(doc, p);
        (n, class_of(&doc.slice(p..n)))
    })
}

/// The grapheme before `p`: its start and class.
fn before(doc: &Document, p: ByteOffset) -> Option<(ByteOffset, Class)> {
    (p.0 > 0).then(|| {
        let s = prev_grapheme_boundary(doc, p);
        (s, class_of(&doc.slice(s..p)))
    })
}

/// Skip the run of the current class (a newline is a run of its own), then
/// any spaces: lands on the start of the next word.
pub(crate) fn word_right(doc: &Document, mut p: ByteOffset) -> ByteOffset {
    let Some((n, class)) = after(doc, p) else {
        return p;
    };
    p = n;
    if class != Class::Newline {
        while let Some((n, c)) = after(doc, p)
            && c == class
        {
            p = n;
        }
    }
    while let Some((n, Class::Space)) = after(doc, p) {
        p = n;
    }
    p
}

/// Skip spaces backwards, then the run of the class before them.
pub(crate) fn word_left(doc: &Document, mut p: ByteOffset) -> ByteOffset {
    while let Some((s, Class::Space)) = before(doc, p) {
        p = s;
    }
    let Some((s, class)) = before(doc, p) else {
        return p;
    };
    p = s;
    if class != Class::Newline {
        while let Some((s, c)) = before(doc, p)
            && c == class
        {
            p = s;
        }
    }
    p
}

/// The run of same-class graphemes around `p`, preferring a word on either
/// side. Never crosses a newline.
/// The word (or run of punctuation, or of blanks) at `p`, as a
/// double-click selects it; empty between two line breaks.
pub fn word_at(doc: &Document, p: ByteOffset) -> (ByteOffset, ByteOffset) {
    let a = after(doc, p).map(|x| x.1).filter(|c| *c != Class::Newline);
    let b = before(doc, p).map(|x| x.1).filter(|c| *c != Class::Newline);
    let class = match (a, b) {
        (Some(Class::Word), _) => Class::Word,
        (_, Some(Class::Word)) => Class::Word,
        (Some(c), _) | (None, Some(c)) => c,
        (None, None) => return (p, p),
    };
    let mut start = p;
    while let Some((s, c)) = before(doc, start)
        && c == class
    {
        start = s;
    }
    let mut end = p;
    while let Some((n, c)) = after(doc, end)
        && c == class
    {
        end = n;
    }
    (start, end)
}