continuity-engine 0.2.41

Synchronous, storage-neutral Continuity editor engine
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
//! Selection-aware edit planning for the synchronous engine.
//!
//! Every command that mutates buffer text routes through here. The public
//! [`SelectionEdit`] enum names the operation; [`plan`] turns it into a
//! [`SelectionEditPlan`] of atomic [`EditOp`]s in **descending byte order**
//! (so sequential `Buffer::apply` calls keep their pre-edit offsets valid)
//! plus the post-edit selection set; [`apply_plan`] runs the ops against a
//! mutable buffer and returns the final revision.
//!
//! Per-family planners (newlines, word-scope deletes, line ops, inline
//! shape edits, markdown) live in sibling modules to keep this file under
//! the 600-line cap.

use continuity_buffer::{Buffer, Revision};
use continuity_text::{EditOp, Selection};
use ropey::Rope;

use crate::edit_inline::{
    plan_change_case, plan_delete_to_bracket, plan_reflow_paragraph, plan_surround_selection,
    plan_transpose_chars, plan_wrap_at_column,
};
use crate::edit_line_text::{
    plan_convert_line_endings, plan_indent, plan_outdent, plan_reverse_lines, plan_shuffle_lines,
    plan_sort_lines, plan_spaces_to_tabs, plan_tabs_to_spaces, plan_trim_trailing_whitespace,
    plan_trim_trailing_whitespace_all, plan_trim_whitespace_all, plan_unique_lines,
};
use crate::edit_lines::{
    plan_delete_to_line_end, plan_delete_to_line_start, plan_duplicate_line,
    plan_duplicate_selection, plan_insert_newline_above, plan_insert_newline_below,
    plan_insert_newline_smart, plan_join_lines, plan_join_selected_lines, plan_move_line_down,
    plan_move_line_up, plan_toggle_bullet_at_line_start,
    plan_toggle_bullet_with_continuation_indent,
};
use crate::edit_markdown::{
    plan_markdown_cycle_list_marker, plan_markdown_insert_code_fence,
    plan_markdown_insert_image_ref, plan_markdown_insert_link, plan_markdown_toggle_checkbox,
    plan_markdown_toggle_emphasis, plan_markdown_toggle_numbered, plan_markdown_toggle_task,
    plan_markdown_wrap_in_blockquote,
};
use crate::edit_markdown_blocks::{
    plan_markdown_cycle_heading, plan_markdown_demote_section, plan_markdown_move_section_down,
    plan_markdown_move_section_up, plan_markdown_promote_section, plan_markdown_set_heading,
};
use crate::edit_pairs::{plan_delete_pair, plan_insert_pair};
use crate::edit_planning::{
    caret_delete_range, finalize_specs_with_transformed_selections, merge_specs,
    ranges_for_selection, EditSpec,
};
use crate::edit_words::{
    plan_delete_word_backward, plan_delete_word_forward, plan_transpose_words,
};
use crate::Error;

/// A selection-aware edit requested by an editor command.
///
/// New variants land here as commands graduate from "named in spec" to
/// "implemented". Each variant maps to exactly one undo group.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SelectionEdit {
    /// Insert or replace each active selection with the supplied text.
    InsertText(String),
    /// Delete the selected text, or one character before each caret.
    DeleteBack,
    /// Delete the selected text, or one character after each caret.
    DeleteForward,

    /// Insert a newline on a fresh line above each selection's line, with
    /// the caret landing on the new line.
    InsertNewlineAbove,
    /// Insert a newline on a fresh line below each selection's line, caret
    /// at the new line.
    InsertNewlineBelow,
    /// Insert a newline that inherits the leading indentation of the
    /// caret's current line.
    InsertNewlineSmart,

    /// Toggle a `- ` bullet marker right after each selection line's
    /// leading whitespace. Adding the marker shifts the caret's byte
    /// column by `+2` so the cursor stays on the same content character
    /// (visually unchanged). Removing it shifts by `-2` (clamped to the
    /// leading-whitespace column). Plain-text variant of
    /// [`SelectionEdit::MarkdownToggleBullet`] — it doesn't depend on
    /// markdown parse state and is meant for the Ctrl+R quick-toggle
    /// shortcut.
    ToggleBulletAtLineStart,

    /// Like [`SelectionEdit::ToggleBulletAtLineStart`], but for a multi-line
    /// selection the add path also prepends one indent unit to every covered
    /// line after the first (turning the selection into a bulleted list whose
    /// continuation lines are indented under the first item); the strip path
    /// removes both the bullet and that indent. A single-line selection
    /// behaves identically to the plain toggle. Bound to `Ctrl+Shift+R`.
    ToggleBulletWithContinuationIndent {
        /// Indent unit (tab or N spaces) prepended to continuation lines,
        /// read live from the dispatch context (mirrors `editor.indent`).
        unit: IndentUnit,
    },

    /// Delete text from the caret backward to the previous word boundary.
    DeleteWordBackward,
    /// Delete text from the caret forward to the next word boundary.
    DeleteWordForward,
    /// Delete from the caret back to the start of the line (or first
    /// non-whitespace if already at line start).
    DeleteToLineStart,
    /// Delete from the caret forward to the end of the line.
    DeleteToLineEnd,
    /// Delete from the caret to the matching bracket, if any.
    DeleteToBracket,

    /// Duplicate the entire line(s) covered by each selection.
    DuplicateLine,
    /// Duplicate the bytes spanned by each selection (caret selections
    /// duplicate nothing).
    DuplicateSelection,
    /// Move each selected line up one position.
    MoveLineUp,
    /// Move each selected line down one position.
    MoveLineDown,
    /// Join the line below each selection's line into the current line.
    JoinLines,
    /// Collapse every line break within the lines covered by each selection
    /// into a single space, joining the whole selected block into one line
    /// (VS Code "Join Lines"). Distinct from [`SelectionEdit::JoinLines`],
    /// which only folds the single line below each caret (Vim `J`).
    JoinSelectedLines,
    /// Sort the lines covered by selections.
    SortLines(SortKind),
    /// Reverse the order of lines covered by selections.
    ReverseLines,
    /// Drop duplicate lines covered by selections (case-sensitive, stable).
    UniqueLines,
    /// Pseudo-randomly reorder the lines covered by selections, seeded for
    /// determinism (the seed is hashed into the LCG).
    ShuffleLines(u64),
    /// Trim trailing whitespace from every line covered by selections.
    TrimTrailingWhitespace,
    /// Phase B14: trim trailing whitespace on every line in the buffer
    /// regardless of selection. Used by the explicit
    /// `editor.trim_trailing_whitespace` command and the on-save hook.
    TrimTrailingWhitespaceAll,
    /// Strip leading **and** trailing whitespace from every line in the
    /// buffer (one undo group). Used by the explicit `editor.trim_whitespace`
    /// command. Per-line leading strip removes indentation by design — the
    /// literal "trim each line" reading, distinct from
    /// [`SelectionEdit::TrimTrailingWhitespaceAll`].
    TrimWhitespaceAll,

    /// Hard-wrap each paragraph covered by selections at `column` columns.
    WrapAtColumn(u32),
    /// Reflow each paragraph at `column` columns, preserving leading
    /// whitespace.
    ReflowParagraph(u32),

    /// Transpose the two characters straddling each caret.
    TransposeChars,
    /// Transpose the two words straddling each caret.
    TransposeWords,

    /// Change case of the bytes inside each selection (or the word at each
    /// caret).
    ChangeCase(CaseKind),

    /// Indent each selected line by one indent unit.
    Indent {
        /// Indent unit (tab or N spaces).
        unit: IndentUnit,
    },
    /// Outdent each selected line by one indent unit.
    Outdent {
        /// Indent unit (tab or N spaces).
        unit: IndentUnit,
    },

    /// Replace runs of `tab_width` spaces with tabs.
    SpacesToTabs {
        /// Number of contiguous spaces that map to one tab.
        tab_width: u32,
    },
    /// Replace tabs with `tab_width` spaces.
    TabsToSpaces {
        /// Number of spaces emitted per tab.
        tab_width: u32,
    },
    /// Convert line endings of every covered line.
    ConvertLineEndings(LineEnding),
    /// Phase C2 — convert line endings on every line in the buffer
    /// regardless of selection. Used by the status-bar click handler
    /// and the C3 mixed-LE normalize chip.
    ConvertLineEndingsAll(LineEnding),
    /// Phase C3 — replace every tab in the buffer with `tab_width`
    /// spaces, regardless of selection. Used by the mixed-indent
    /// normalize chip.
    TabsToSpacesAll {
        /// Number of spaces emitted per tab.
        tab_width: u32,
    },

    /// Wrap each non-empty selection in `open` … `close`.
    SurroundSelection {
        /// Inserted at the start of the selection.
        open: String,
        /// Inserted at the end of the selection.
        close: String,
    },

    /// Toggle markdown emphasis around each selection.
    MarkdownToggleEmphasis(EmphasisKind),
    /// Set the heading level of each covered line. `level == 0` strips the
    /// heading; `1..=6` rewrites the prefix.
    MarkdownSetHeading(u8),
    /// Cycle the heading level on each covered line (`+1`/`-1`).
    MarkdownCycleHeading(i32),
    /// Promote (decrease level) the section enclosing each caret.
    MarkdownPromoteSection,
    /// Demote (increase level) the section enclosing each caret.
    MarkdownDemoteSection,
    /// Move the section enclosing each caret upward past the previous
    /// sibling section.
    MarkdownMoveSectionUp,
    /// Move the section enclosing each caret downward past the next
    /// sibling section.
    MarkdownMoveSectionDown,
    /// Toggle a `- ` bullet prefix on each covered line. Shares the
    /// content-relative planner with [`SelectionEdit::ToggleBulletAtLineStart`]
    /// (`plan_toggle_bullet_at_line_start`), so the caret stays on the same
    /// content character across add/remove and an ordered→bullet conversion.
    MarkdownToggleBullet,
    /// Toggle a `1. ` numbered prefix on each covered line.
    MarkdownToggleNumbered,
    /// Toggle a `[ ]`/`[x]` checkbox prefix on each covered line.
    MarkdownToggleCheckbox,
    /// Toggle a `- [ ] ` task-bullet prefix on each covered line: plain
    /// or bulleted lines gain the task marker; existing task lines drop
    /// it. Bound to `Ctrl+E`.
    MarkdownToggleTask,
    /// Cycle the list marker between `-`, `*`, `+`.
    MarkdownCycleListMarker,
    /// Phase B11: renumber the ordered list containing the caret.
    MarkdownRenumberList,
    /// Prefix `> ` on each covered line.
    MarkdownWrapInBlockquote,
    /// Wrap selections in fenced code blocks (or insert an empty fence).
    MarkdownInsertCodeFence,
    /// Wrap each non-empty selection as `[text](url)` with placeholder url.
    MarkdownInsertLink,
    /// Insert `![alt](path)` at each caret.
    MarkdownInsertImageRef,
    /// Strip markdown formatting from every covered line: heading
    /// hashes, list/checkbox markers, blockquote prefixes, emphasis /
    /// code / strikethrough delimiters, and link syntax (keeping the
    /// link text). Conservative on intraword `_` / lone `*` so code
    /// identifiers and arithmetic survive.
    MarkdownStripFormatting,

    /// Phase-16.5 auto-pair insert: at each caret, insert `open`
    /// immediately followed by `close`, leaving the caret between
    /// them. Lands as one undo group. Selections that already cover a
    /// non-empty range fall back to surrounding the range with
    /// `open` … `close` (so highlight + `(` keeps the surround
    /// behavior of [`Self::SurroundSelection`]).
    InsertPair {
        /// Leading character (e.g. `(`).
        open: String,
        /// Trailing character (e.g. `)`).
        close: String,
    },
    /// Phase-16.5 backspace-aware delete-pair: at each caret, if the
    /// preceding byte is `open` and the following byte is `close`,
    /// delete both as one undo group. Otherwise the planner returns
    /// `Ok(None)` so the caller can fall through to a normal
    /// [`Self::DeleteBack`].
    DeletePair {
        /// Open delimiter to the left of the caret.
        open: char,
        /// Close delimiter to the right of the caret.
        close: char,
    },
}

/// Sort variant used by [`SelectionEdit::SortLines`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SortKind {
    /// Ascending, case-sensitive byte order.
    Asc,
    /// Descending, case-sensitive byte order.
    Desc,
    /// Ascending, case-insensitive.
    AscCaseInsensitive,
    /// Descending, case-insensitive.
    DescCaseInsensitive,
    /// Ascending, by ASCII length.
    AscByLength,
    /// Descending, by ASCII length.
    DescByLength,
    /// Ascending, natural-numeric (digit runs as integers).
    AscNumeric,
    /// Descending, natural-numeric.
    DescNumeric,
}

/// Case-conversion kinds.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CaseKind {
    /// `EXAMPLE`.
    Upper,
    /// `example`.
    Lower,
    /// `Example Title`.
    Title,
    /// Invert the case of each ASCII letter.
    Toggle,
    /// `Example. case.` — first letter of each sentence uppercased.
    Sentence,
}

/// Indent unit used by [`SelectionEdit::Indent`] / [`SelectionEdit::Outdent`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum IndentUnit {
    /// One tab character.
    Tab,
    /// `n` spaces.
    Spaces(u32),
}

/// Line-ending convention.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LineEnding {
    /// `\n`.
    Lf,
    /// `\r\n`.
    Crlf,
}

/// Markdown emphasis flavor.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum EmphasisKind {
    /// `**text**`.
    Bold,
    /// `*text*`.
    Italic,
    /// `~~text~~`.
    Strikethrough,
    /// `` `text` ``.
    InlineCode,
}

/// A planned selection edit against one immutable pre-edit buffer state.
pub struct SelectionEditPlan {
    /// Atomic edit ops to apply, ordered from highest byte offset to lowest.
    pub ops: Vec<EditOp>,
    /// Selection set before the edit.
    pub selections_before: Vec<Selection>,
    /// Selection set after the whole edit group.
    pub selections_after: Vec<Selection>,
}

/// Build atomic edit ops for a selection-aware edit.
///
/// Returns `Ok(None)` when the operation has no effect (e.g. backspace at
/// the document start with all carets at offset 0).
///
/// # Errors
///
/// Returns core errors when any selection position is outside the buffer,
/// or argument validation fails (e.g. an out-of-range heading level).
pub fn plan(buffer: &Buffer, edit: &SelectionEdit) -> Result<Option<SelectionEditPlan>, Error> {
    match edit {
        SelectionEdit::InsertText(text) => plan_insert_text(buffer, text),
        SelectionEdit::DeleteBack => plan_delete(buffer, true),
        SelectionEdit::DeleteForward => plan_delete(buffer, false),

        SelectionEdit::InsertNewlineAbove => plan_insert_newline_above(buffer),
        SelectionEdit::InsertNewlineBelow => plan_insert_newline_below(buffer),
        SelectionEdit::InsertNewlineSmart => plan_insert_newline_smart(buffer),
        SelectionEdit::ToggleBulletAtLineStart => plan_toggle_bullet_at_line_start(buffer),
        SelectionEdit::ToggleBulletWithContinuationIndent { unit } => {
            let indent_unit = crate::edit_line_text_helpers::indent_text(*unit);
            plan_toggle_bullet_with_continuation_indent(buffer, &indent_unit)
        }

        SelectionEdit::DeleteWordBackward => plan_delete_word_backward(buffer),
        SelectionEdit::DeleteWordForward => plan_delete_word_forward(buffer),
        SelectionEdit::DeleteToLineStart => plan_delete_to_line_start(buffer),
        SelectionEdit::DeleteToLineEnd => plan_delete_to_line_end(buffer),
        SelectionEdit::DeleteToBracket => plan_delete_to_bracket(buffer),

        SelectionEdit::DuplicateLine => plan_duplicate_line(buffer),
        SelectionEdit::DuplicateSelection => plan_duplicate_selection(buffer),
        SelectionEdit::MoveLineUp => plan_move_line_up(buffer),
        SelectionEdit::MoveLineDown => plan_move_line_down(buffer),
        SelectionEdit::JoinLines => plan_join_lines(buffer),
        SelectionEdit::JoinSelectedLines => plan_join_selected_lines(buffer),
        SelectionEdit::SortLines(kind) => plan_sort_lines(buffer, *kind),
        SelectionEdit::ReverseLines => plan_reverse_lines(buffer),
        SelectionEdit::UniqueLines => plan_unique_lines(buffer),
        SelectionEdit::ShuffleLines(seed) => plan_shuffle_lines(buffer, *seed),
        SelectionEdit::TrimTrailingWhitespace => plan_trim_trailing_whitespace(buffer),
        SelectionEdit::TrimTrailingWhitespaceAll => plan_trim_trailing_whitespace_all(buffer),
        SelectionEdit::TrimWhitespaceAll => plan_trim_whitespace_all(buffer),

        SelectionEdit::WrapAtColumn(width) => plan_wrap_at_column(buffer, *width),
        SelectionEdit::ReflowParagraph(width) => plan_reflow_paragraph(buffer, *width),

        SelectionEdit::TransposeChars => plan_transpose_chars(buffer),
        SelectionEdit::TransposeWords => plan_transpose_words(buffer),

        SelectionEdit::ChangeCase(kind) => plan_change_case(buffer, *kind),

        SelectionEdit::Indent { unit } => plan_indent(buffer, *unit),
        SelectionEdit::Outdent { unit } => plan_outdent(buffer, *unit),

        SelectionEdit::SpacesToTabs { tab_width } => plan_spaces_to_tabs(buffer, *tab_width),
        SelectionEdit::TabsToSpaces { tab_width } => plan_tabs_to_spaces(buffer, *tab_width),
        SelectionEdit::ConvertLineEndings(eol) => plan_convert_line_endings(buffer, *eol),
        SelectionEdit::ConvertLineEndingsAll(eol) => {
            crate::edit_normalize::plan_convert_line_endings_all(buffer, *eol)
        }
        SelectionEdit::TabsToSpacesAll { tab_width } => {
            crate::edit_normalize::plan_tabs_to_spaces_all(buffer, *tab_width)
        }

        SelectionEdit::SurroundSelection { open, close } => {
            plan_surround_selection(buffer, open, close)
        }

        SelectionEdit::MarkdownToggleEmphasis(kind) => plan_markdown_toggle_emphasis(buffer, *kind),
        SelectionEdit::MarkdownSetHeading(level) => plan_markdown_set_heading(buffer, *level),
        SelectionEdit::MarkdownCycleHeading(delta) => plan_markdown_cycle_heading(buffer, *delta),
        SelectionEdit::MarkdownPromoteSection => plan_markdown_promote_section(buffer),
        SelectionEdit::MarkdownDemoteSection => plan_markdown_demote_section(buffer),
        SelectionEdit::MarkdownMoveSectionUp => plan_markdown_move_section_up(buffer),
        SelectionEdit::MarkdownMoveSectionDown => plan_markdown_move_section_down(buffer),
        SelectionEdit::MarkdownToggleBullet => plan_toggle_bullet_at_line_start(buffer),
        SelectionEdit::MarkdownToggleNumbered => plan_markdown_toggle_numbered(buffer),
        SelectionEdit::MarkdownToggleCheckbox => plan_markdown_toggle_checkbox(buffer),
        SelectionEdit::MarkdownToggleTask => plan_markdown_toggle_task(buffer),
        SelectionEdit::MarkdownCycleListMarker => plan_markdown_cycle_list_marker(buffer),
        SelectionEdit::MarkdownRenumberList => {
            crate::edit_list::plan_markdown_renumber_list(buffer)
        }
        SelectionEdit::MarkdownWrapInBlockquote => plan_markdown_wrap_in_blockquote(buffer),
        SelectionEdit::MarkdownInsertCodeFence => plan_markdown_insert_code_fence(buffer),
        SelectionEdit::MarkdownInsertLink => plan_markdown_insert_link(buffer),
        SelectionEdit::MarkdownInsertImageRef => plan_markdown_insert_image_ref(buffer),
        SelectionEdit::MarkdownStripFormatting => {
            crate::edit_markdown_strip::plan_markdown_strip_formatting(buffer)
        }

        SelectionEdit::InsertPair { open, close } => plan_insert_pair(buffer, open, close),
        SelectionEdit::DeletePair { open, close } => plan_delete_pair(buffer, *open, *close),
    }
}

/// Apply already-planned ops and assign the final selection set.
///
/// # Errors
///
/// Returns buffer-layer errors if an op is invalid against the current
/// buffer state.
pub fn apply_plan(
    buffer: &mut Buffer,
    plan: &SelectionEditPlan,
) -> Result<Option<Revision>, Error> {
    let mut last = None;
    for op in &plan.ops {
        last = Some(buffer.apply(op)?);
    }
    let mut selections = plan.selections_after.clone();
    crate::selection_coalesce::coalesce_selections(&mut selections);
    buffer.set_selections(selections);
    Ok(last)
}

fn plan_insert_text(buffer: &Buffer, text: &str) -> Result<Option<SelectionEditPlan>, Error> {
    let selections_before = buffer.selections().to_vec();
    let specs = insert_specs(buffer.rope(), &selections_before, text)?;
    Ok(finalize_specs_with_transformed_selections(
        specs,
        selections_before,
    ))
}

fn plan_delete(buffer: &Buffer, backward: bool) -> Result<Option<SelectionEditPlan>, Error> {
    let selections_before = buffer.selections().to_vec();
    let mut specs = delete_specs(buffer.rope(), &selections_before, backward)?;
    if specs.is_empty() {
        return Ok(None);
    }
    specs.sort_by_key(|spec| spec.start);
    specs = merge_specs(specs);
    let selections_after = specs
        .iter()
        .map(|spec| Selection::caret_at(spec.start_position))
        .collect();
    let ops = specs.into_iter().rev().map(EditSpec::into_op).collect();
    Ok(Some(SelectionEditPlan {
        ops,
        selections_before,
        selections_after,
    }))
}

fn insert_specs(rope: &Rope, selections: &[Selection], text: &str) -> Result<Vec<EditSpec>, Error> {
    let mut specs = Vec::new();
    for selection in selections {
        for range in ranges_for_selection(rope, *selection)? {
            let start = range.start.to_byte_offset(rope)?;
            let end = range.end.to_byte_offset(rope)?;
            specs.push(EditSpec {
                start,
                end,
                start_position: range.start,
                inserted: text.to_string(),
                end_position_in_rope: Some(range.end),
            });
        }
    }
    Ok(specs)
}

fn delete_specs(
    rope: &Rope,
    selections: &[Selection],
    backward: bool,
) -> Result<Vec<EditSpec>, Error> {
    let mut specs = Vec::new();
    for selection in selections {
        for range in ranges_for_selection(rope, *selection)? {
            let start = range.start.to_byte_offset(rope)?;
            let end = range.end.to_byte_offset(rope)?;
            if start != end {
                specs.push(EditSpec {
                    start,
                    end,
                    start_position: range.start,
                    inserted: String::new(),
                    end_position_in_rope: Some(range.end),
                });
                continue;
            }
            let Some((delete_start, delete_end)) = caret_delete_range(rope, start, backward) else {
                continue;
            };
            specs.push(EditSpec::delete(rope, delete_start, delete_end)?);
        }
    }
    Ok(specs)
}

// Tests live in `selection_edit/tests.rs` so this file stays under the
// 600-line cap.
#[cfg(test)]
mod tests;