@@ -110,11 +110,25 @@ pub mod resource {
110
110
}
111
111
}
112
112
113
- /// Produce an iterator over lines, separated by LF or CRLF, suitable to create tokens using
114
- /// [`imara_diff::intern::InternedInput`].
113
+ /// Produce an iterator over lines, separated by LF or CRLF and thus keeping newlines.
114
+ ///
115
+ /// Note that this will cause unusual diffs if a file didn't end in newline but lines were added
116
+ /// on the other side.
117
+ ///
118
+ /// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
115
119
pub fn intern_source ( & self ) -> imara_diff:: sources:: ByteLines < ' a , true > {
116
120
crate :: blob:: sources:: byte_lines_with_terminator ( self . data . as_slice ( ) . unwrap_or_default ( ) )
117
121
}
122
+
123
+ /// Produce an iterator over lines, but remove LF or CRLF.
124
+ ///
125
+ /// This produces the expected diffs when lines were added at the end of a file that didn't end
126
+ /// with a newline before the change.
127
+ ///
128
+ /// Suitable to create tokens using [`imara_diff::intern::InternedInput`].
129
+ pub fn intern_source_strip_newline_separators ( & self ) -> imara_diff:: sources:: ByteLines < ' a , false > {
130
+ crate :: blob:: sources:: byte_lines ( self . data . as_slice ( ) . unwrap_or_default ( ) )
131
+ }
118
132
}
119
133
120
134
/// The data of a diffable resource, as it could be determined and computed previously.
@@ -228,8 +242,15 @@ pub mod prepare_diff {
228
242
229
243
impl < ' a > Outcome < ' a > {
230
244
/// Produce an instance of an interner which `git` would use to perform diffs.
245
+ ///
246
+ /// Note that newline separators will be removed to improve diff quality
247
+ /// at the end of files that didn't have a newline, but had lines added
248
+ /// past the end.
231
249
pub fn interned_input ( & self ) -> imara_diff:: intern:: InternedInput < & ' a [ u8 ] > {
232
- crate :: blob:: intern:: InternedInput :: new ( self . old . intern_source ( ) , self . new . intern_source ( ) )
250
+ crate :: blob:: intern:: InternedInput :: new (
251
+ self . old . intern_source_strip_newline_separators ( ) ,
252
+ self . new . intern_source_strip_newline_separators ( ) ,
253
+ )
233
254
}
234
255
}
235
256
0 commit comments