Change behavior of inline diff to be word based
This also fixes a bug with bad indexes and updates the inline terminal example.
This commit is contained in:
parent
459fdfdf9d
commit
f3e401fc17
4 changed files with 208 additions and 43 deletions
|
|
@ -3,7 +3,7 @@ use std::{fmt, iter};
|
|||
use crate::algorithms::{Algorithm, DiffOp, DiffTag};
|
||||
use crate::text::{Change, ChangeTag, TextDiff};
|
||||
|
||||
use super::split_chars;
|
||||
use super::split_words;
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ impl<'s> From<Change<'s>> for InlineChange<'s> {
|
|||
InlineChange {
|
||||
tag: change.tag(),
|
||||
old_index: change.old_index(),
|
||||
new_index: change.old_index(),
|
||||
new_index: change.new_index(),
|
||||
values: vec![(false, change.value())],
|
||||
missing_newline: change.missing_newline(),
|
||||
}
|
||||
|
|
@ -118,8 +118,8 @@ pub(crate) fn iter_inline_changes<'diff>(
|
|||
(ChangeTag::Delete, Some(ChangeTag::Insert)) => {
|
||||
let old_value = change.value();
|
||||
let new_value = next_change.unwrap().value();
|
||||
let old_chars = split_chars(&old_value).collect::<Vec<_>>();
|
||||
let new_chars = split_chars(&new_value).collect::<Vec<_>>();
|
||||
let old_chars = split_words(&old_value).collect::<Vec<_>>();
|
||||
let new_chars = split_words(&new_value).collect::<Vec<_>>();
|
||||
let old_mindex = MultiIndex::new(&old_chars, old_value);
|
||||
let new_mindex = MultiIndex::new(&new_chars, new_value);
|
||||
let inline_diff = TextDiff::configure()
|
||||
|
|
@ -158,7 +158,7 @@ pub(crate) fn iter_inline_changes<'diff>(
|
|||
Some(InlineChange {
|
||||
tag: ChangeTag::Delete,
|
||||
old_index: change.old_index(),
|
||||
new_index: change.new_index(),
|
||||
new_index: None,
|
||||
values: old_values,
|
||||
missing_newline: newline_terminated
|
||||
&& !old_value.ends_with(&['\r', '\n'][..]),
|
||||
|
|
@ -167,8 +167,8 @@ pub(crate) fn iter_inline_changes<'diff>(
|
|||
.chain(
|
||||
Some(InlineChange {
|
||||
tag: ChangeTag::Insert,
|
||||
old_index: change.old_index(),
|
||||
new_index: change.new_index(),
|
||||
old_index: None,
|
||||
new_index: next_change.unwrap().new_index(),
|
||||
values: new_values,
|
||||
missing_newline: newline_terminated
|
||||
&& !new_value.ends_with(&['\r', '\n'][..]),
|
||||
|
|
|
|||
|
|
@ -483,10 +483,9 @@ impl<'old, 'new, 'bufs> TextDiff<'old, 'new, 'bufs> {
|
|||
/// Iterates over the changes the op expands to with inline emphasis.
|
||||
///
|
||||
/// This is very similar to [`iter_changes`] but it performs a second
|
||||
/// level per-character diff on adjacent line replacements. The exact
|
||||
/// behavior of this function with regards to how it detects those
|
||||
/// inline changes is currently not defined and will likely change
|
||||
/// over time.
|
||||
/// level diff on adjacent line replacements. The exact behavior of
|
||||
/// this function with regards to how it detects those inline changes
|
||||
/// is currently not defined and will likely change over time.
|
||||
pub fn iter_inline_changes(&self, op: &DiffOp) -> impl Iterator<Item = InlineChange> {
|
||||
iter_inline_changes(self, op)
|
||||
}
|
||||
|
|
@ -783,6 +782,21 @@ fn test_virtual_newlines() {
|
|||
insta::assert_debug_snapshot!(&changes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line_ops_inline() {
|
||||
let diff = TextDiff::from_lines(
|
||||
"Hello World\nsome stuff here\nsome more stuff here\n\nAha stuff here\nand more stuff",
|
||||
"Stuff\nHello World\nsome amazing stuff here\nsome more stuff here\n",
|
||||
);
|
||||
assert_eq!(diff.newline_terminated(), true);
|
||||
let changes = diff
|
||||
.ops()
|
||||
.iter()
|
||||
.flat_map(|op| diff.iter_inline_changes(op))
|
||||
.collect::<Vec<_>>();
|
||||
insta::assert_debug_snapshot!(&changes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_diff() {
|
||||
let diff = TextDiff::from_chars("Hello World", "Hallo Welt");
|
||||
|
|
|
|||
134
src/text/snapshots/similar__text__line_ops_inline.snap
Normal file
134
src/text/snapshots/similar__text__line_ops_inline.snap
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
---
|
||||
source: src/text/mod.rs
|
||||
expression: "&changes"
|
||||
---
|
||||
[
|
||||
InlineChange {
|
||||
tag: Insert,
|
||||
old_index: None,
|
||||
new_index: Some(
|
||||
0,
|
||||
),
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"Stuff\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Equal,
|
||||
old_index: Some(
|
||||
0,
|
||||
),
|
||||
new_index: Some(
|
||||
1,
|
||||
),
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"Hello World\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Delete,
|
||||
old_index: Some(
|
||||
1,
|
||||
),
|
||||
new_index: None,
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"some ",
|
||||
),
|
||||
(
|
||||
false,
|
||||
"stuff here\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Insert,
|
||||
old_index: None,
|
||||
new_index: Some(
|
||||
2,
|
||||
),
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"some ",
|
||||
),
|
||||
(
|
||||
true,
|
||||
"amazing ",
|
||||
),
|
||||
(
|
||||
false,
|
||||
"stuff here\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Equal,
|
||||
old_index: Some(
|
||||
2,
|
||||
),
|
||||
new_index: Some(
|
||||
3,
|
||||
),
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"some more stuff here\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Delete,
|
||||
old_index: Some(
|
||||
3,
|
||||
),
|
||||
new_index: None,
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Delete,
|
||||
old_index: Some(
|
||||
4,
|
||||
),
|
||||
new_index: None,
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"Aha stuff here\n",
|
||||
),
|
||||
],
|
||||
missing_newline: false,
|
||||
},
|
||||
InlineChange {
|
||||
tag: Delete,
|
||||
old_index: Some(
|
||||
5,
|
||||
),
|
||||
new_index: None,
|
||||
values: [
|
||||
(
|
||||
false,
|
||||
"and more stuff",
|
||||
),
|
||||
],
|
||||
missing_newline: true,
|
||||
},
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue