@@ -9,6 +9,7 @@ use std::{
99#[ derive( Debug ) ]
1010pub struct PatchFormatter {
1111 with_color : bool ,
12+ with_missing_newline_message : bool ,
1213
1314 context : Style ,
1415 delete : Style ,
@@ -23,6 +24,7 @@ impl PatchFormatter {
2324 pub fn new ( ) -> Self {
2425 Self {
2526 with_color : false ,
27+ with_missing_newline_message : true ,
2628
2729 context : Style :: new ( ) ,
2830 delete : Color :: Red . normal ( ) ,
@@ -39,6 +41,19 @@ impl PatchFormatter {
3941 self
4042 }
4143
44+ /// Sets whether to format a patch with a "No newline at end of file" message.
45+ ///
46+ /// Default is `true`.
47+ ///
48+ /// Note: If this is disabled by setting to `false`, formatted patches will no longer contain
49+ /// sufficient information to determine if a file ended with a newline character (`\n`) or not
50+ /// and the patch will be formatted as if both the original and modified files ended with a
51+ /// newline character (`\n`).
52+ pub fn missing_newline_message ( mut self , enable : bool ) -> Self {
53+ self . with_missing_newline_message = enable;
54+ self
55+ }
56+
4257 /// Returns a `Display` impl which can be used to print a Patch
4358 pub fn fmt_patch < ' a > ( & ' a self , patch : & ' a Patch < ' a , str > ) -> impl Display + ' a {
4459 PatchDisplay { f : self , patch }
@@ -238,7 +253,9 @@ impl<T: AsRef<[u8]> + ?Sized> LineDisplay<'_, T> {
238253
239254 if !line. ends_with ( b"\n " ) {
240255 writeln ! ( w) ?;
241- writeln ! ( w, "{}" , NO_NEWLINE_AT_EOF ) ?;
256+ if self . f . with_missing_newline_message {
257+ writeln ! ( w, "{}" , NO_NEWLINE_AT_EOF ) ?;
258+ }
242259 }
243260
244261 Ok ( ( ) )
@@ -269,7 +286,9 @@ impl Display for LineDisplay<'_, str> {
269286
270287 if !line. ends_with ( '\n' ) {
271288 writeln ! ( f) ?;
272- writeln ! ( f, "{}" , NO_NEWLINE_AT_EOF ) ?;
289+ if self . f . with_missing_newline_message {
290+ writeln ! ( f, "{}" , NO_NEWLINE_AT_EOF ) ?;
291+ }
273292 }
274293
275294 Ok ( ( ) )
0 commit comments