diff options
author | Nathan Vegdahl | 2021-07-19 05:02:12 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-07-19 05:02:12 +0000 |
commit | e462f32723bb61899a390f438d7d856d87fb7614 (patch) | |
tree | e97a9afbf07e2735ba96d62dd5e8f71e1a241fe1 /helix-core/src/diff.rs | |
parent | 6c038bb0151c6aeb43fc94bd2dc3d516a71d346c (diff) | |
parent | 5292fe0f7df9f1a420744007aa9dd67e7a5a6610 (diff) |
Merge branch 'master' into great_line_ending_and_cursor_range_cleanup
Diffstat (limited to 'helix-core/src/diff.rs')
-rw-r--r-- | helix-core/src/diff.rs | 56 |
1 files changed, 27 insertions, 29 deletions
diff --git a/helix-core/src/diff.rs b/helix-core/src/diff.rs index 9c1fc999..a83db333 100644 --- a/helix-core/src/diff.rs +++ b/helix-core/src/diff.rs @@ -1,6 +1,4 @@ -use ropey::Rope; - -use crate::{Change, Transaction}; +use crate::{Rope, Transaction}; /// Compares `old` and `new` to generate a [`Transaction`] describing /// the steps required to get from `old` to `new`. @@ -25,34 +23,34 @@ pub fn compare_ropes(old: &Rope, new: &Rope) -> Transaction { // The current position of the change needs to be tracked to // construct the `Change`s. let mut pos = 0; - let changes: Vec<Change> = diff - .ops() - .iter() - .map(|op| op.as_tag_tuple()) - .filter_map(|(tag, old_range, new_range)| { - // `old_pos..pos` is equivalent to `start..end` for where - // the change should be applied. - let old_pos = pos; - pos += old_range.end - old_range.start; + Transaction::change( + old, + diff.ops() + .iter() + .map(|op| op.as_tag_tuple()) + .filter_map(|(tag, old_range, new_range)| { + // `old_pos..pos` is equivalent to `start..end` for where + // the change should be applied. + let old_pos = pos; + pos += old_range.end - old_range.start; - match tag { - // Semantically, inserts and replacements are the same thing. - similar::DiffTag::Insert | similar::DiffTag::Replace => { - // This is the text from the `new` rope that should be - // inserted into `old`. - let text: &str = { - let start = new.char_to_byte(new_range.start); - let end = new.char_to_byte(new_range.end); - &new_converted[start..end] - }; - Some((old_pos, pos, Some(text.into()))) + match tag { + // Semantically, inserts and replacements are the same thing. + similar::DiffTag::Insert | similar::DiffTag::Replace => { + // This is the text from the `new` rope that should be + // inserted into `old`. + let text: &str = { + let start = new.char_to_byte(new_range.start); + let end = new.char_to_byte(new_range.end); + &new_converted[start..end] + }; + Some((old_pos, pos, Some(text.into()))) + } + similar::DiffTag::Delete => Some((old_pos, pos, None)), + similar::DiffTag::Equal => None, } - similar::DiffTag::Delete => Some((old_pos, pos, None)), - similar::DiffTag::Equal => None, - } - }) - .collect(); - Transaction::change(old, changes.into_iter()) + }), + ) } #[cfg(test)] |