diff --git a/radicle-surf/src/diff.rs b/radicle-surf/src/diff.rs index d688a6e..f8fe509 100644 --- a/radicle-surf/src/diff.rs +++ b/radicle-surf/src/diff.rs @@ -17,7 +17,7 @@ //! Types that represent diff(s) in a Git repo. -use std::{borrow::Cow, path::PathBuf, string::FromUtf8Error}; +use std::{borrow::Cow, ops::Range, path::PathBuf, string::FromUtf8Error}; #[cfg(feature = "serde")] use serde::{ser, ser::SerializeStruct, Serialize, Serializer}; @@ -358,6 +358,10 @@ pub struct Stats { pub struct Hunk { pub header: Line, pub lines: Vec, + /// Old line range. + pub old: Range, + /// New line range. + pub new: Range, } /// A set of [`Hunk`] changes. diff --git a/radicle-surf/src/diff/git.rs b/radicle-surf/src/diff/git.rs index 57a3315..b3f61e0 100644 --- a/radicle-surf/src/diff/git.rs +++ b/radicle-surf/src/diff/git.rs @@ -179,7 +179,12 @@ impl TryFrom> for DiffContent { let line = Modification::try_from(line)?; lines.push(line); } - hunks.push(Hunk { header, lines }); + hunks.push(Hunk { + header, + lines, + old: hunk.old_start()..hunk.old_start() + hunk.old_lines(), + new: hunk.new_start()..hunk.new_start() + hunk.new_lines(), + }); } let eof = match (old_missing_eof, new_missing_eof) { (true, true) => EofNewLine::BothMissing, diff --git a/radicle-surf/t/src/diff.rs b/radicle-surf/t/src/diff.rs index c5c9963..4b53f22 100644 --- a/radicle-surf/t/src/diff.rs +++ b/radicle-surf/t/src/diff.rs @@ -44,6 +44,8 @@ fn test_initial_diff() -> Result<(), Error> { .to_vec(), 1, )], + old: 0..0, + new: 1..2, }] .into(), eof: EofNewLine::default(), @@ -93,6 +95,8 @@ fn test_diff_file() -> Result<(), Error> { Modification::addition(b"This repository is a data source for the Upstream front-end tests and the\n".to_vec(), 1), Modification::addition(b"[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n".to_vec(), 2), ], + old: 1..2, + new: 1..3, }] .into(), eof: EofNewLine::default(), @@ -129,6 +133,8 @@ fn test_diff() -> Result<(), Error> { Modification::addition(b"This repository is a data source for the Upstream front-end tests and the\n".to_vec(), 1), Modification::addition(b"[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n".to_vec(), 2), ], + old: 1..2, + new: 1..3, }] .into(), eof: EofNewLine::default(), @@ -228,7 +234,9 @@ fn test_diff_serde() -> Result<(), Error> { "line": "\n", "lineNo": 2, "type": "addition", - }] + }], + "old": { "start": 0, "end": 0 }, + "new": { "start": 1, "end": 3 }, }], "eof": "noneMissing", }, @@ -248,42 +256,44 @@ fn test_diff_serde() -> Result<(), Error> { "hunks": [{ "header": "@@ -1,7 +0,0 @@\n", "lines": [ - { - "line": " ;;;;; ;;;;; ;;;;;\n", - "lineNo": 1, - "type": "deletion", - }, - { - "line": " ;;;;; ;;;;; ;;;;;\n", - "lineNo": 2, - "type": "deletion", - }, - { - "line": " ;;;;; ;;;;; ;;;;;\n", - "lineNo": 3, - "type": "deletion", - }, - { - "line": " ;;;;; ;;;;; ;;;;;\n", - "lineNo": 4, - "type": "deletion", - }, - { - "line": "..;;;;;.. ..;;;;;.. ..;;;;;..\n", - "lineNo": 5, - "type": "deletion", - }, - { - "line": " ':::::' ':::::' ':::::'\n", - "lineNo": 6, - "type": "deletion", - }, - { - "line": " ':` ':` ':`\n", - "lineNo": 7, - "type": "deletion", - }, - ] + { + "line": " ;;;;; ;;;;; ;;;;;\n", + "lineNo": 1, + "type": "deletion", + }, + { + "line": " ;;;;; ;;;;; ;;;;;\n", + "lineNo": 2, + "type": "deletion", + }, + { + "line": " ;;;;; ;;;;; ;;;;;\n", + "lineNo": 3, + "type": "deletion", + }, + { + "line": " ;;;;; ;;;;; ;;;;;\n", + "lineNo": 4, + "type": "deletion", + }, + { + "line": "..;;;;;.. ..;;;;;.. ..;;;;;..\n", + "lineNo": 5, + "type": "deletion", + }, + { + "line": " ':::::' ':::::' ':::::'\n", + "lineNo": 6, + "type": "deletion", + }, + { + "line": " ':` ':` ':`\n", + "lineNo": 7, + "type": "deletion", + }, + ], + "old": { "start": 1, "end": 8 }, + "new": { "start": 0, "end": 0 }, }], "eof": "noneMissing", }, @@ -316,7 +326,9 @@ fn test_diff_serde() -> Result<(), Error> { "line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n", "type": "addition" }, - ] + ], + "old": { "start": 1, "end": 3 }, + "new": { "start": 1, "end": 3 }, }], "eof": "noneMissing", },