Fix the ranges in unified diff output

This commit is contained in:
Armin Ronacher 2021-02-25 23:20:56 +01:00
parent 094ba84680
commit 3cb7d5ab76
6 changed files with 15 additions and 13 deletions

View file

@ -15,6 +15,7 @@ All notable changes to similar are documented here.
* Added a compacting step to clean up diffs. This results in nicer looking diffs and * Added a compacting step to clean up diffs. This results in nicer looking diffs and
fewer edits. This is happening automatically for captured diffs and is exposed fewer edits. This is happening automatically for captured diffs and is exposed
through the `Capture` type. through the `Capture` type.
* Fix incorrect ranges in unified diff output.
## 1.2.2 ## 1.2.2

View file

@ -4,7 +4,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()"
--- ---
--- a.txt --- a.txt
+++ b.txt +++ b.txt
@@ -15,3 +19,3 @@ @@ -16,7 +16,7 @@
p p
q q
r r
@ -13,7 +13,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()"
t t
u u
v v
@@ -37,3 +41,3 @@ @@ -38,7 +38,7 @@
L L
M M
N N

View file

@ -4,7 +4,7 @@ expression: "&diff.unified_diff().missing_newline_hint(false).header(\"a.txt\",\
--- ---
--- a.txt --- a.txt
+++ b.txt +++ b.txt
@@ -0 +0 @@ @@ -1 +1 @@
-a -a
+b +b

View file

@ -4,7 +4,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()"
--- ---
--- a.txt --- a.txt
+++ b.txt +++ b.txt
@@ -0 +0 @@ @@ -1 +1 @@
-a -a
+b +b
\ No newline at end of file \ No newline at end of file

View file

@ -4,7 +4,7 @@ expression: "&diff.unified_diff().context_radius(3).header(\"old\", \"new\").to_
--- ---
--- old --- old
+++ new +++ new
@@ -0 +2 @@ @@ -1,3 +1,3 @@
Hello World Hello World
-some stuff here -some stuff here
+some amazing stuff here +some amazing stuff here

View file

@ -24,7 +24,6 @@
//! The former uses [`DiffableStr::to_string_lossy`], the latter uses //! The former uses [`DiffableStr::to_string_lossy`], the latter uses
//! [`DiffableStr::as_bytes`] for each line. //! [`DiffableStr::as_bytes`] for each line.
#[cfg(feature = "text")] #[cfg(feature = "text")]
use std::ops::Range;
use std::{fmt, io}; use std::{fmt, io};
use crate::iter::AllChangesIter; use crate::iter::AllChangesIter;
@ -46,10 +45,6 @@ impl fmt::Display for MissingNewlineHint {
struct UnifiedDiffHunkRange(usize, usize); struct UnifiedDiffHunkRange(usize, usize);
impl UnifiedDiffHunkRange { impl UnifiedDiffHunkRange {
fn new(range: Range<usize>) -> UnifiedDiffHunkRange {
UnifiedDiffHunkRange(range.start, range.end)
}
fn start(&self) -> usize { fn start(&self) -> usize {
self.0 self.0
} }
@ -61,7 +56,7 @@ impl UnifiedDiffHunkRange {
impl fmt::Display for UnifiedDiffHunkRange { impl fmt::Display for UnifiedDiffHunkRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut beginning = self.start(); let mut beginning = self.start() + 1;
let len = self.end() - self.start(); let len = self.end() - self.start();
if len == 1 { if len == 1 {
write!(f, "{}", beginning) write!(f, "{}", beginning)
@ -84,9 +79,15 @@ pub struct UnifiedHunkHeader {
impl UnifiedHunkHeader { impl UnifiedHunkHeader {
/// Creates a hunk header from a (non empty) slice of diff ops. /// Creates a hunk header from a (non empty) slice of diff ops.
pub fn new(ops: &[DiffOp]) -> UnifiedHunkHeader { pub fn new(ops: &[DiffOp]) -> UnifiedHunkHeader {
let first = ops[0];
let last = ops[ops.len() - 1];
let old_start = first.old_range().start;
let new_start = first.new_range().start;
let old_end = last.old_range().end;
let new_end = last.new_range().end;
UnifiedHunkHeader { UnifiedHunkHeader {
old_range: UnifiedDiffHunkRange::new(ops[0].old_range()), old_range: UnifiedDiffHunkRange(old_start, old_end),
new_range: UnifiedDiffHunkRange::new(ops[ops.len() - 1].new_range()), new_range: UnifiedDiffHunkRange(new_start, new_end),
} }
} }
} }