diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 17333727..ee4ac231 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -49,7 +49,7 @@ impl Default for DiffLineType { #[derive(Default, Clone, Hash, Debug)] pub struct DiffLine { /// - pub content: String, + pub content: Box, /// pub line_type: DiffLineType, /// @@ -292,7 +292,9 @@ fn raw_diff_to_file_diff<'a>( let diff_line = DiffLine { position: DiffLinePosition::from(&line), content: String::from_utf8_lossy(line.content()) - .to_string(), + //Note: trim await trailing newline characters + .trim_matches(is_newline) + .into(), line_type: line.origin_value().into(), }; @@ -376,6 +378,10 @@ fn raw_diff_to_file_diff<'a>( Ok(res.into_inner()) } +const fn is_newline(c: char) -> bool { + c == '\n' || c == '\r' +} + fn new_file_content(path: &Path) -> Option> { if let Ok(meta) = fs::symlink_metadata(path) { if meta.file_type().is_symlink() { @@ -429,7 +435,7 @@ mod tests { get_diff(repo_path, "foo/bar.txt", false, None).unwrap(); assert_eq!(diff.hunks.len(), 1); - assert_eq!(diff.hunks[0].lines[1].content, "test\n"); + assert_eq!(&*diff.hunks[0].lines[1].content, "test\n"); } #[test] @@ -552,7 +558,7 @@ mod tests { ) .unwrap(); - assert_eq!(diff.hunks[0].lines[1].content, "test"); + assert_eq!(&*diff.hunks[0].lines[1].content, "test"); } #[test] diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 38016cf5..34515c36 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -332,7 +332,7 @@ mod tests { let diff = get_diff(repo_path, "test.txt", true, None).unwrap(); assert_eq!( - diff.hunks[0].lines[0].content, + &*diff.hunks[0].lines[0].content, String::from("@@ -1 +1 @@\n") ); } diff --git a/src/components/diff.rs b/src/components/diff.rs index d1ed6758..c4496400 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -417,15 +417,12 @@ impl DiffComponent { } }; - let trimmed = - line.content.trim_matches(|c| c == '\n' || c == '\r'); - let filled = if selected { // selected line - format!("{:w$}\n", trimmed, w = width as usize) + format!("{:w$}\n", line.content, w = width as usize) } else { // weird eof missing eol line - format!("{}\n", trimmed) + format!("{}\n", line.content) }; Spans::from(vec![