| 174 | } |
| 175 | |
| 176 | pub fn commit_overlays(&self) -> Result<(), CommitError> { |
| 177 | let mut edits = self.edits.borrow_mut(); |
| 178 | if edits.is_empty() { |
| 179 | return Ok(()); |
| 180 | } |
| 181 | |
| 182 | let mut pending_segments: Vec<'a, PendingSegment<'a>> = Vec::with_capacity_in(edits.len(), self.bump); |
| 183 | |
| 184 | for (order, edit) in edits.drain(..).enumerate() { |
| 185 | match edit { |
| 186 | TransformEdit::Replace { target, cursors } => { |
| 187 | if target.start() > target.end() { |
| 188 | return Err(CommitError::InvalidEdit { span: target }); |
| 189 | } |
| 190 | pending_segments.push(PendingSegment { |
| 191 | span: target, |
| 192 | intent: OverlayKind::Replace, |
| 193 | order, |
| 194 | cursors, |
| 195 | }); |
| 196 | } |
| 197 | TransformEdit::InsertBefore { anchor, cursors } => { |
| 198 | let span = Span::new(anchor, anchor); |
| 199 | pending_segments.push(PendingSegment { span, intent: OverlayKind::InsertBefore, order, cursors }); |
| 200 | } |
| 201 | TransformEdit::InsertAfter { anchor, cursors } => { |
| 202 | let span = Span::new(anchor, anchor); |
| 203 | pending_segments.push(PendingSegment { span, intent: OverlayKind::InsertAfter, order, cursors }); |
| 204 | } |
| 205 | TransformEdit::Delete { target } => { |
| 206 | pending_segments.push(PendingSegment { |
| 207 | span: target, |
| 208 | intent: OverlayKind::Replace, |
| 209 | order, |
| 210 | cursors: Vec::with_capacity_in(0, self.bump()), |
| 211 | }); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | pending_segments.sort_by(|a, b| { |
| 217 | a.span |
| 218 | .start() |
| 219 | .cmp(&b.span.start()) |
| 220 | .then_with(|| a.span.end().cmp(&b.span.end())) |
| 221 | .then_with(|| a.intent.cmp(&b.intent)) |
| 222 | .then_with(|| a.order.cmp(&b.order)) |
| 223 | }); |
| 224 | |
| 225 | let mut last_non_zero: Option<Span> = None; |
| 226 | for segment in &pending_segments { |
| 227 | if segment.span.start() > segment.span.end() { |
| 228 | return Err(CommitError::InvalidEdit { span: segment.span }); |
| 229 | } |
| 230 | if segment.span.start() == segment.span.end() { |
| 231 | continue; |
| 232 | } |
| 233 | if let Some(prev) = last_non_zero |