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
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<usize>) -> 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),
}
}
}