Repository: lapce/lapce
File: lapce-data/src/editor/commands/insert_chars.rs (Rust)

Code before:
        let delta = buffer.edit_multiple(&edits, EditType::InsertChars);

        // Update selection
        let mut selection =
            selection.apply_delta(&delta, true, InsertDrift::Default);

        // Apply late edits
        let edits_after = edits_after
            .iter()
            .map(|(idx, content)| {
                (
                    region_to_selection(selection.regions()[*idx]),
                    content.to_string(),
                )
            })
            .collect::<Vec<_>>();

        let edits_after = edits_after
            .iter()
            .map(|(selection, content)| (selection, content.as_str()))
            .collect::<Vec<_>>();

        buffer.edit_multiple(&edits_after, EditType::InsertChars);

        // Adjust selection according to previous late edits
        let mut adjustment = 0;
        for (region, (_, insert)) in selection
            .regions_mut()
            .iter_mut()
            .zip(edits_after.into_iter())
        {
            *region = SelRegion::new(
                region.start + adjustment,
                region.end + adjustment,
                region.horiz().map(|pos| {
                    if let ColPosition::Col(col) = pos {
                        ColPosition::Col(*col + adjustment)
                    } else {
                        *pos
                    }
                }),
            );

            adjustment += insert.len();
        }

        *cursor = Cursor::new(CursorMode::Insert(selection), None);

        None
    }
}

Review comment (suggestion):
@dzhou121 while this function got more complex, this (L155-175) is the part that was missing previously. This adjustment step fixes the `can_insert_matching_pair_multi` test.

Code after:

        let delta = buffer.edit_multiple(&edits, EditType::InsertChars);

        // Update selection
        let mut selection =
            selection.apply_delta(&delta, true, InsertDrift::Default);

        // Apply late edits
        let edits_after = edits_after
            .iter()
            .map(|(idx, content)| {
                (
                    region_to_selection(selection.regions()[*idx]),
                    content.to_string(),
                )
            })
            .collect::<Vec<_>>();

        let edits_after = edits_after
            .iter()
            .map(|(selection, content)| (selection, content.as_str()))
            .collect::<Vec<_>>();

        buffer.edit_multiple(&edits_after, EditType::InsertChars);

        // Adjust selection according to previous late edits
        let mut adjustment = 0;
        for region in selection
            .regions_mut()
            .iter_mut()
            .sorted_by(|region_a, region_b| region_a.start().cmp(&region_b.start()))
        {
            *region = SelRegion::new(
                region.start + adjustment,
                region.end + adjustment,
                region.horiz().map(|pos| {
                    if let ColPosition::Col(col) = pos {
                        ColPosition::Col(*col + adjustment)
                    } else {
                        *pos
                    }
                }),
            );

            if let Some(inserted) =
                edits_after.iter().find_map(|(selection, str)| {
                    if selection.last_inserted().map(|r| r.start())
                        == Some(region.start())
                    {
                        Some(str)
                    } else {