From 7239d8ca951593b66a557c989dffaf94edddf420 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Tue, 25 Aug 2020 17:59:37 +0200
Subject: Provide better stack trace for overlapping edits

Overlapping indels are a bug. Checking this *always* is tricky (needs
a sorted data structure to not suffer O(N^2) perf). But
opportunistically checking small indels should give provide 80% of the
benefits.
---
 crates/text_edit/src/lib.rs | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

(limited to 'crates/text_edit/src')

diff --git a/crates/text_edit/src/lib.rs b/crates/text_edit/src/lib.rs
index ab8cd7fd1..e417e8ea6 100644
--- a/crates/text_edit/src/lib.rs
+++ b/crates/text_edit/src/lib.rs
@@ -159,13 +159,13 @@ impl<'a> IntoIterator for &'a TextEdit {
 
 impl TextEditBuilder {
     pub fn replace(&mut self, range: TextRange, replace_with: String) {
-        self.indels.push(Indel::replace(range, replace_with))
+        self.indel(Indel::replace(range, replace_with))
     }
     pub fn delete(&mut self, range: TextRange) {
-        self.indels.push(Indel::delete(range))
+        self.indel(Indel::delete(range))
     }
     pub fn insert(&mut self, offset: TextSize, text: String) {
-        self.indels.push(Indel::insert(offset, text))
+        self.indel(Indel::insert(offset, text))
     }
     pub fn finish(self) -> TextEdit {
         let mut indels = self.indels;
@@ -175,6 +175,12 @@ impl TextEditBuilder {
     pub fn invalidates_offset(&self, offset: TextSize) -> bool {
         self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))
     }
+    fn indel(&mut self, indel: Indel) {
+        self.indels.push(indel);
+        if self.indels.len() <= 16 {
+            check_disjoint(&mut self.indels);
+        }
+    }
 }
 
 fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {
-- 
cgit v1.2.3