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.
This commit is contained in:
Aleksey Kladov 2020-08-25 17:59:37 +02:00
parent 7fcda5aa46
commit 7239d8ca95

View file

@ -159,13 +159,13 @@ impl<'a> IntoIterator for &'a TextEdit {
impl TextEditBuilder { impl TextEditBuilder {
pub fn replace(&mut self, range: TextRange, replace_with: String) { 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) { 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) { 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 { pub fn finish(self) -> TextEdit {
let mut indels = self.indels; let mut indels = self.indels;
@ -175,6 +175,12 @@ impl TextEditBuilder {
pub fn invalidates_offset(&self, offset: TextSize) -> bool { pub fn invalidates_offset(&self, offset: TextSize) -> bool {
self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset)) 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 { fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {