2020-05-05 16:15:49 -05:00
|
|
|
//! Representation of a `TextEdit`.
|
|
|
|
//!
|
|
|
|
//! `rust-analyzer` never mutates text itself and only sends diffs to clients,
|
|
|
|
//! so `TextEdit` is the ultimate representation of the work done by
|
|
|
|
//! rust-analyzer.
|
2021-11-02 15:38:38 -05:00
|
|
|
|
2020-05-06 04:31:26 -05:00
|
|
|
pub use text_size::{TextRange, TextSize};
|
2018-12-10 15:09:12 -06:00
|
|
|
|
2020-05-05 16:15:49 -05:00
|
|
|
/// `InsertDelete` -- a single "atomic" change to text
|
|
|
|
///
|
|
|
|
/// Must not overlap with other `InDel`s
|
2021-11-02 15:38:38 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-05-05 16:15:49 -05:00
|
|
|
pub struct Indel {
|
|
|
|
pub insert: String,
|
2018-12-23 08:49:14 -06:00
|
|
|
/// Refers to offsets in the original text
|
2018-12-10 15:09:12 -06:00
|
|
|
pub delete: TextRange,
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:50:23 -05:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2020-05-05 16:15:49 -05:00
|
|
|
pub struct TextEdit {
|
2021-08-09 05:13:02 -05:00
|
|
|
/// Invariant: disjoint and sorted by `delete`.
|
2020-05-05 16:15:49 -05:00
|
|
|
indels: Vec<Indel>,
|
|
|
|
}
|
2018-12-10 15:09:12 -06:00
|
|
|
|
2020-04-24 14:57:10 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2020-05-05 16:15:49 -05:00
|
|
|
pub struct TextEditBuilder {
|
|
|
|
indels: Vec<Indel>,
|
|
|
|
}
|
2018-12-10 15:09:12 -06:00
|
|
|
|
2020-05-05 16:15:49 -05:00
|
|
|
impl Indel {
|
|
|
|
pub fn insert(offset: TextSize, text: String) -> Indel {
|
|
|
|
Indel::replace(TextRange::empty(offset), text)
|
|
|
|
}
|
|
|
|
pub fn delete(range: TextRange) -> Indel {
|
|
|
|
Indel::replace(range, String::new())
|
|
|
|
}
|
|
|
|
pub fn replace(range: TextRange, replace_with: String) -> Indel {
|
|
|
|
Indel { delete: range, insert: replace_with }
|
2018-12-10 15:09:12 -06:00
|
|
|
}
|
2019-01-08 12:59:55 -06:00
|
|
|
|
2020-05-05 16:23:29 -05:00
|
|
|
pub fn apply(&self, text: &mut String) {
|
2020-04-24 16:40:41 -05:00
|
|
|
let start: usize = self.delete.start().into();
|
|
|
|
let end: usize = self.delete.end().into();
|
2019-01-08 12:59:55 -06:00
|
|
|
text.replace_range(start..end, &self.insert);
|
|
|
|
}
|
2018-12-10 15:09:12 -06:00
|
|
|
}
|
2020-05-05 16:15:49 -05:00
|
|
|
|
|
|
|
impl TextEdit {
|
2020-08-12 09:58:56 -05:00
|
|
|
pub fn builder() -> TextEditBuilder {
|
|
|
|
TextEditBuilder::default()
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:15:49 -05:00
|
|
|
pub fn insert(offset: TextSize, text: String) -> TextEdit {
|
2020-08-12 09:58:56 -05:00
|
|
|
let mut builder = TextEdit::builder();
|
2020-05-05 16:15:49 -05:00
|
|
|
builder.insert(offset, text);
|
|
|
|
builder.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(range: TextRange) -> TextEdit {
|
2020-08-12 09:58:56 -05:00
|
|
|
let mut builder = TextEdit::builder();
|
2020-05-05 16:15:49 -05:00
|
|
|
builder.delete(range);
|
|
|
|
builder.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn replace(range: TextRange, replace_with: String) -> TextEdit {
|
2020-08-12 09:58:56 -05:00
|
|
|
let mut builder = TextEdit::builder();
|
2020-05-05 16:15:49 -05:00
|
|
|
builder.replace(range, replace_with);
|
|
|
|
builder.finish()
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:56:18 -05:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.indels.len()
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:08:37 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.indels.is_empty()
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:58:56 -05:00
|
|
|
pub fn iter(&self) -> std::slice::Iter<'_, Indel> {
|
|
|
|
self.into_iter()
|
2020-05-21 08:56:18 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:23:29 -05:00
|
|
|
pub fn apply(&self, text: &mut String) {
|
2020-05-21 08:56:18 -05:00
|
|
|
match self.len() {
|
2020-05-05 16:23:29 -05:00
|
|
|
0 => return,
|
|
|
|
1 => {
|
|
|
|
self.indels[0].apply(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-02-26 08:50:09 -06:00
|
|
|
let text_size = TextSize::of(&*text);
|
|
|
|
let mut total_len = text_size.clone();
|
2021-10-03 07:45:08 -05:00
|
|
|
for indel in &self.indels {
|
2020-05-05 16:15:49 -05:00
|
|
|
total_len += TextSize::of(&indel.insert);
|
2022-02-26 08:50:09 -06:00
|
|
|
total_len -= indel.delete.len();
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
2022-02-26 08:50:09 -06:00
|
|
|
|
|
|
|
if let Some(additional) = total_len.checked_sub(text_size.into()) {
|
|
|
|
text.reserve(additional.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
for indel in self.indels.iter().rev() {
|
|
|
|
indel.apply(text);
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
2020-05-05 16:23:29 -05:00
|
|
|
|
2022-02-26 08:50:09 -06:00
|
|
|
assert_eq!(TextSize::of(&*text), total_len);
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:50:23 -05:00
|
|
|
pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> {
|
|
|
|
// FIXME: can be done without allocating intermediate vector
|
|
|
|
let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>();
|
2021-11-03 08:42:15 -05:00
|
|
|
if !check_disjoint_and_sort(&mut all) {
|
2020-05-21 12:50:23 -05:00
|
|
|
return Err(other);
|
|
|
|
}
|
2021-11-02 15:38:38 -05:00
|
|
|
|
2021-11-03 08:42:15 -05:00
|
|
|
self.indels.extend(other.indels);
|
|
|
|
check_disjoint_and_sort(&mut self.indels);
|
2021-11-03 09:44:46 -05:00
|
|
|
// Only dedup deletions and replacements, keep all insertions
|
|
|
|
self.indels.dedup_by(|a, b| a == b && !a.delete.is_empty());
|
2020-05-21 12:50:23 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:15:49 -05:00
|
|
|
pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {
|
|
|
|
let mut res = offset;
|
2021-10-03 07:45:08 -05:00
|
|
|
for indel in &self.indels {
|
2020-05-05 16:15:49 -05:00
|
|
|
if indel.delete.start() >= offset {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if offset < indel.delete.end() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
res += TextSize::of(&indel.insert);
|
|
|
|
res -= indel.delete.len();
|
|
|
|
}
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 07:05:01 -05:00
|
|
|
impl IntoIterator for TextEdit {
|
|
|
|
type Item = Indel;
|
2020-08-12 09:58:56 -05:00
|
|
|
type IntoIter = std::vec::IntoIter<Indel>;
|
2020-08-10 07:05:01 -05:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.indels.into_iter()
|
2020-08-12 09:58:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a TextEdit {
|
|
|
|
type Item = &'a Indel;
|
|
|
|
type IntoIter = std::slice::Iter<'a, Indel>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.indels.iter()
|
2020-08-10 07:05:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:15:49 -05:00
|
|
|
impl TextEditBuilder {
|
2021-08-16 15:48:38 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.indels.is_empty()
|
|
|
|
}
|
2020-05-05 16:15:49 -05:00
|
|
|
pub fn replace(&mut self, range: TextRange, replace_with: String) {
|
2021-10-03 07:39:43 -05:00
|
|
|
self.indel(Indel::replace(range, replace_with));
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
|
|
|
pub fn delete(&mut self, range: TextRange) {
|
2021-10-03 07:39:43 -05:00
|
|
|
self.indel(Indel::delete(range));
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
|
|
|
pub fn insert(&mut self, offset: TextSize, text: String) {
|
2021-10-03 07:39:43 -05:00
|
|
|
self.indel(Indel::insert(offset, text));
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
|
|
|
pub fn finish(self) -> TextEdit {
|
2020-05-21 12:50:23 -05:00
|
|
|
let mut indels = self.indels;
|
2021-11-02 15:38:38 -05:00
|
|
|
assert_disjoint_or_equal(&mut indels);
|
2020-05-21 12:50:23 -05:00
|
|
|
TextEdit { indels }
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
|
|
|
pub fn invalidates_offset(&self, offset: TextSize) -> bool {
|
|
|
|
self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))
|
|
|
|
}
|
2020-08-25 10:59:37 -05:00
|
|
|
fn indel(&mut self, indel: Indel) {
|
|
|
|
self.indels.push(indel);
|
|
|
|
if self.indels.len() <= 16 {
|
2021-11-02 15:38:38 -05:00
|
|
|
assert_disjoint_or_equal(&mut self.indels);
|
2020-08-25 10:59:37 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 16:15:49 -05:00
|
|
|
}
|
2020-05-21 12:50:23 -05:00
|
|
|
|
2021-11-03 08:42:15 -05:00
|
|
|
fn assert_disjoint_or_equal(indels: &mut [Indel]) {
|
|
|
|
assert!(check_disjoint_and_sort(indels));
|
2020-09-03 06:24:06 -05:00
|
|
|
}
|
2021-11-03 08:42:15 -05:00
|
|
|
// FIXME: Remove the impl Bound here, it shouldn't be needed
|
|
|
|
fn check_disjoint_and_sort(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {
|
2020-05-21 12:50:23 -05:00
|
|
|
indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end()));
|
2021-11-02 15:38:38 -05:00
|
|
|
indels.iter().zip(indels.iter().skip(1)).all(|(l, r)| {
|
|
|
|
let l = l.borrow();
|
|
|
|
let r = r.borrow();
|
|
|
|
l.delete.end() <= r.delete.start() || l == r
|
|
|
|
})
|
2020-05-21 12:50:23 -05:00
|
|
|
}
|
2022-02-26 08:46:14 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_apply() {
|
|
|
|
let mut text = "_11h1_2222_xx3333_4444_6666".to_string();
|
|
|
|
let mut builder = TextEditBuilder::default();
|
|
|
|
builder.replace(TextRange::new(3.into(), 4.into()), "1".to_string());
|
|
|
|
builder.delete(TextRange::new(11.into(), 13.into()));
|
|
|
|
builder.insert(22.into(), "_5555".to_string());
|
|
|
|
|
|
|
|
let text_edit = builder.finish();
|
|
|
|
text_edit.apply(&mut text);
|
|
|
|
|
|
|
|
assert_eq!(text, "_1111_2222_3333_4444_5555_6666")
|
|
|
|
}
|