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

Code before:
                if is_whitespace_or_punct {
                    let insert_after = matching_char(c).unwrap();
                    edits_after.push((idx, insert_after));
                }
            };

            let current_selection = region_to_selection(*region);

            edits.push((current_selection, c.to_string()));
        }

        // Apply edits to current selection
        let edits = edits
            .iter()
            .map(|(selection, content)| (selection, content.as_str()))
            .collect::<Vec<_>>();

        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,

Review comment (suggestion):
Note to self: It would be better to allow edits of `SelRegion`s, not just `Selection`s. That way, we wouldn't have to create two vectors.

Code after:

                if is_whitespace_or_punct {
                    let insert_after = matching_char(c).unwrap();
                    edits_after.push((idx, insert_after));
                }
            };

            let current_selection = region_to_selection(*region);

            edits.push((current_selection, c.to_string()));
        }

        // Apply edits to current selection
        let edits = edits
            .iter()
            .map(|(selection, content)| (selection, content.as_str()))
            .collect::<Vec<_>>();

        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,