From 3cb7d5ab766e3858c3e01ed9e65540a44878281e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 25 Feb 2021 23:20:56 +0100 Subject: [PATCH] Fix the ranges in unified diff output --- CHANGELOG.md | 1 + src/snapshots/similar__udiff__unified_diff.snap | 4 ++-- ...lar__udiff__unified_diff_newline_hint-2.snap | 2 +- ...milar__udiff__unified_diff_newline_hint.snap | 2 +- .../snapshots/similar__text__unified_diff.snap | 2 +- src/udiff.rs | 17 +++++++++-------- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b35da..30bf8e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 fewer edits. This is happening automatically for captured diffs and is exposed through the `Capture` type. +* Fix incorrect ranges in unified diff output. ## 1.2.2 diff --git a/src/snapshots/similar__udiff__unified_diff.snap b/src/snapshots/similar__udiff__unified_diff.snap index 799b38c..5345b0b 100644 --- a/src/snapshots/similar__udiff__unified_diff.snap +++ b/src/snapshots/similar__udiff__unified_diff.snap @@ -4,7 +4,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()" --- --- a.txt +++ b.txt -@@ -15,3 +19,3 @@ +@@ -16,7 +16,7 @@ p q r @@ -13,7 +13,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()" t u v -@@ -37,3 +41,3 @@ +@@ -38,7 +38,7 @@ L M N diff --git a/src/snapshots/similar__udiff__unified_diff_newline_hint-2.snap b/src/snapshots/similar__udiff__unified_diff_newline_hint-2.snap index 7e8489e..9a5d922 100644 --- a/src/snapshots/similar__udiff__unified_diff_newline_hint-2.snap +++ b/src/snapshots/similar__udiff__unified_diff_newline_hint-2.snap @@ -4,7 +4,7 @@ expression: "&diff.unified_diff().missing_newline_hint(false).header(\"a.txt\",\ --- --- a.txt +++ b.txt -@@ -0 +0 @@ +@@ -1 +1 @@ -a +b diff --git a/src/snapshots/similar__udiff__unified_diff_newline_hint.snap b/src/snapshots/similar__udiff__unified_diff_newline_hint.snap index 0d3b137..d181b28 100644 --- a/src/snapshots/similar__udiff__unified_diff_newline_hint.snap +++ b/src/snapshots/similar__udiff__unified_diff_newline_hint.snap @@ -4,7 +4,7 @@ expression: "&diff.unified_diff().header(\"a.txt\", \"b.txt\").to_string()" --- --- a.txt +++ b.txt -@@ -0 +0 @@ +@@ -1 +1 @@ -a +b \ No newline at end of file diff --git a/src/text/snapshots/similar__text__unified_diff.snap b/src/text/snapshots/similar__text__unified_diff.snap index aa8deb2..77f409a 100644 --- a/src/text/snapshots/similar__text__unified_diff.snap +++ b/src/text/snapshots/similar__text__unified_diff.snap @@ -4,7 +4,7 @@ expression: "&diff.unified_diff().context_radius(3).header(\"old\", \"new\").to_ --- --- old +++ new -@@ -0 +2 @@ +@@ -1,3 +1,3 @@ Hello World -some stuff here +some amazing stuff here diff --git a/src/udiff.rs b/src/udiff.rs index 40969f2..80a30cf 100644 --- a/src/udiff.rs +++ b/src/udiff.rs @@ -24,7 +24,6 @@ //! The former uses [`DiffableStr::to_string_lossy`], the latter uses //! [`DiffableStr::as_bytes`] for each line. #[cfg(feature = "text")] -use std::ops::Range; use std::{fmt, io}; use crate::iter::AllChangesIter; @@ -46,10 +45,6 @@ impl fmt::Display for MissingNewlineHint { struct UnifiedDiffHunkRange(usize, usize); impl UnifiedDiffHunkRange { - fn new(range: Range) -> UnifiedDiffHunkRange { - UnifiedDiffHunkRange(range.start, range.end) - } - fn start(&self) -> usize { self.0 } @@ -61,7 +56,7 @@ impl UnifiedDiffHunkRange { impl fmt::Display for UnifiedDiffHunkRange { 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(); if len == 1 { write!(f, "{}", beginning) @@ -84,9 +79,15 @@ pub struct UnifiedHunkHeader { impl UnifiedHunkHeader { /// Creates a hunk header from a (non empty) slice of diff ops. 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 { - old_range: UnifiedDiffHunkRange::new(ops[0].old_range()), - new_range: UnifiedDiffHunkRange::new(ops[ops.len() - 1].new_range()), + old_range: UnifiedDiffHunkRange(old_start, old_end), + new_range: UnifiedDiffHunkRange(new_start, new_end), } } }