Skip to content

Commit

Permalink
Refactor changes_since to report code_units
Browse files Browse the repository at this point in the history
And also coalesce more changes.

Co-Authored-By: Nathan Sobo <nathan@github.com>
  • Loading branch information
Antonio Scandurra and Nathan Sobo committed Sep 18, 2018
1 parent eb07512 commit 20963dc
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions memo/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct ChangesIter {
#[derive(Debug, Eq, PartialEq)]
pub struct Change {
range: Range<usize>,
text: Text,
code_units: Vec<u16>,
}

#[derive(Clone, Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -1329,31 +1329,28 @@ impl Iterator for ChangesIter {
let position = self.cursor.start::<usize>();
if !fragment.was_visible(&self.since) && fragment.is_visible() {
if let Some(ref mut change) = change {
if change.range.start + change.text.len() == position {
let text = mem::replace(&mut change.text, Text::new(Vec::new()));
let mut code_units = text.code_units;
code_units.extend(fragment.code_units());
change.text = Text::new(Vec::from(code_units));
if change.range.start + change.code_units.len() == position {
change.code_units.extend(fragment.code_units());
} else {
break;
}
} else {
change = Some(Change {
range: position..position,
text: Text::new(Vec::from(fragment.code_units())),
code_units: Vec::from(fragment.code_units()),
});
}
} else if fragment.was_visible(&self.since) && !fragment.is_visible() {
if let Some(ref mut change) = change {
if change.range.start == position {
if change.range.start + change.code_units.len() == position {
change.range.end += &fragment.extent();
} else {
break;
}
} else {
change = Some(Change {
range: position..position + &fragment.extent(),
text: Text::new(Vec::new()),
code_units: Vec::new(),
});
}
}
Expand Down Expand Up @@ -1984,7 +1981,7 @@ mod tests {
for change in buffer.changes_since(old_buffer.version.clone()) {
old_buffer.edit(
&[change.range],
change.text,
Text::new(change.code_units),
&mut local_clock,
&mut lamport_clock,
);
Expand Down

0 comments on commit 20963dc

Please sign in to comment.