|
3 | 3 | //! `rust-analyzer` never mutates text itself and only sends diffs to clients,
|
4 | 4 | //! so `TextEdit` is the ultimate representation of the work done by
|
5 | 5 | //! rust-analyzer.
|
6 |
| -use std::collections::HashSet; |
7 | 6 |
|
8 | 7 | pub use text_size::{TextRange, TextSize};
|
9 | 8 |
|
@@ -118,17 +117,13 @@ impl TextEdit {
|
118 | 117 | pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> {
|
119 | 118 | // FIXME: can be done without allocating intermediate vector
|
120 | 119 | let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>();
|
121 |
| - if !check_disjoint_or_equal(&mut all) { |
| 120 | + if !check_disjoint_and_sort(&mut all) { |
122 | 121 | return Err(other);
|
123 | 122 | }
|
124 | 123 |
|
125 |
| - // remove duplicates |
126 |
| - // FIXME: maybe make indels a HashSet instead to get rid of this? |
127 |
| - let our_indels = self.indels.clone(); |
128 |
| - let our_indels = our_indels.iter().collect::<HashSet<_>>(); |
129 |
| - let other_indels = other.indels.into_iter().filter(|i| !our_indels.contains(i)); |
130 |
| - |
131 |
| - self.indels.extend(other_indels); |
| 124 | + self.indels.extend(other.indels); |
| 125 | + check_disjoint_and_sort(&mut self.indels); |
| 126 | + self.indels.dedup(); |
132 | 127 | Ok(())
|
133 | 128 | }
|
134 | 129 |
|
@@ -195,10 +190,11 @@ impl TextEditBuilder {
|
195 | 190 | }
|
196 | 191 | }
|
197 | 192 |
|
198 |
| -fn assert_disjoint_or_equal(indels: &mut [impl std::borrow::Borrow<Indel>]) { |
199 |
| - assert!(check_disjoint_or_equal(indels)); |
| 193 | +fn assert_disjoint_or_equal(indels: &mut [Indel]) { |
| 194 | + assert!(check_disjoint_and_sort(indels)); |
200 | 195 | }
|
201 |
| -fn check_disjoint_or_equal(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool { |
| 196 | +// FIXME: Remove the impl Bound here, it shouldn't be needed |
| 197 | +fn check_disjoint_and_sort(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool { |
202 | 198 | indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end()));
|
203 | 199 | indels.iter().zip(indels.iter().skip(1)).all(|(l, r)| {
|
204 | 200 | let l = l.borrow();
|
|
0 commit comments