Skip to content

Commit

Permalink
Add a new without_no_newline_at_eof in PatchFormatter.
Browse files Browse the repository at this point in the history
Setting this option will prevent emitting "\ No newline at end of file"
messages.

Fixes: bmwill#8
  • Loading branch information
rjuju authored and eyakubovich committed Sep 27, 2024
1 parent b36b981 commit 256829d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/diff/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
apply::apply,
diff::{Diff, DiffRange},
patch::Patch,
PatchFormatter,
range::Range,
};

Expand Down Expand Up @@ -518,6 +519,35 @@ fn no_newline_at_eof() {
assert_patch!(old, new, expected);
}

#[test]
fn without_no_newline_at_eof() {
let old = "old line";
let new = "new line";
let expected = "\
--- original
+++ modified
@@ -1 +1 @@
-old line
+new line
";

let f = PatchFormatter::new().without_missing_newline_message();
let patch = create_patch(old, new);
let bpatch = create_patch_bytes(old.as_bytes(), new.as_bytes());
let patch_str = format!("{}", f.fmt_patch(&patch));
let mut patch_bytes = Vec::new();
f.write_patch_into(&bpatch, &mut patch_bytes).unwrap();

assert_eq!(patch_str, expected);
assert_eq!(patch_bytes, patch_str.as_bytes());
assert_eq!(patch_bytes, expected.as_bytes());
assert_eq!(apply(old, &patch).unwrap(), new);
assert_eq!(
crate::apply_bytes(old.as_bytes(), &bpatch).unwrap(),
new.as_bytes()
);
}

#[test]
fn myers_diffy_vs_git() {
let original = "\
Expand Down
16 changes: 14 additions & 2 deletions src/patch/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
#[derive(Debug)]
pub struct PatchFormatter {
with_color: bool,
without_missing_newline_message: bool,

context: Style,
delete: Style,
Expand All @@ -23,6 +24,7 @@ impl PatchFormatter {
pub fn new() -> Self {
Self {
with_color: false,
without_missing_newline_message: false,

context: Style::new(),
delete: Color::Red.normal(),
Expand All @@ -39,6 +41,12 @@ impl PatchFormatter {
self
}

/// Enable formatting a patch without a "No newline at end of file" message
pub fn without_missing_newline_message(mut self) -> Self {
self.without_missing_newline_message = true;
self
}

/// Returns a `Display` impl which can be used to print a Patch
pub fn fmt_patch<'a>(&'a self, patch: &'a Patch<'a, str>) -> impl Display + 'a {
PatchDisplay { f: self, patch }
Expand Down Expand Up @@ -238,7 +246,9 @@ impl<T: AsRef<[u8]> + ?Sized> LineDisplay<'_, T> {

if !line.ends_with(b"\n") {
writeln!(w)?;
writeln!(w, "{}", NO_NEWLINE_AT_EOF)?;
if !self.f.without_missing_newline_message {
writeln!(w, "{}", NO_NEWLINE_AT_EOF)?;
}
}

Ok(())
Expand Down Expand Up @@ -269,7 +279,9 @@ impl Display for LineDisplay<'_, str> {

if !line.ends_with('\n') {
writeln!(f)?;
writeln!(f, "{}", NO_NEWLINE_AT_EOF)?;
if !self.f.without_missing_newline_message {
writeln!(f, "{}", NO_NEWLINE_AT_EOF)?;
}
}

Ok(())
Expand Down

0 comments on commit 256829d

Please sign in to comment.