zeta2: Include a single unified diff for the edit history (#40400)
Instead of producing multiple code blocks for each edit history event, we now produce a continuous unified diff. Release Notes: - N/A --------- Co-authored-by: Oleksiy Syvokon <oleksiy.syvokon@gmail.com>
This commit is contained in:
co-authored by
Oleksiy Syvokon
parent
923e880150
commit
23a0b6503c
@@ -1,6 +1,7 @@
|
||||
use chrono::Duration;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fmt::Display,
|
||||
ops::{Add, Range, Sub},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
@@ -91,6 +92,38 @@ pub enum Event {
|
||||
},
|
||||
}
|
||||
|
||||
impl Display for Event {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Event::BufferChange {
|
||||
path,
|
||||
old_path,
|
||||
diff,
|
||||
predicted,
|
||||
} => {
|
||||
let new_path = path.as_deref().unwrap_or(Path::new("untitled"));
|
||||
let old_path = old_path.as_deref().unwrap_or(new_path);
|
||||
|
||||
if *predicted {
|
||||
write!(
|
||||
f,
|
||||
"// User accepted prediction:\n--- a/{}\n+++ b/{}\n{diff}",
|
||||
old_path.display(),
|
||||
new_path.display()
|
||||
)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"--- a/{}\n+++ b/{}\n{diff}",
|
||||
old_path.display(),
|
||||
new_path.display()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Signature {
|
||||
pub text: String,
|
||||
@@ -204,3 +237,82 @@ impl Sub for Line {
|
||||
Self(self.0 - rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_event_display() {
|
||||
let ev = Event::BufferChange {
|
||||
path: None,
|
||||
old_path: None,
|
||||
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
|
||||
predicted: false,
|
||||
};
|
||||
assert_eq!(
|
||||
ev.to_string(),
|
||||
indoc! {"
|
||||
--- a/untitled
|
||||
+++ b/untitled
|
||||
@@ -1,2 +1,2 @@
|
||||
-a
|
||||
-b
|
||||
"}
|
||||
);
|
||||
|
||||
let ev = Event::BufferChange {
|
||||
path: Some(PathBuf::from("foo/bar.txt")),
|
||||
old_path: Some(PathBuf::from("foo/bar.txt")),
|
||||
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
|
||||
predicted: false,
|
||||
};
|
||||
assert_eq!(
|
||||
ev.to_string(),
|
||||
indoc! {"
|
||||
--- a/foo/bar.txt
|
||||
+++ b/foo/bar.txt
|
||||
@@ -1,2 +1,2 @@
|
||||
-a
|
||||
-b
|
||||
"}
|
||||
);
|
||||
|
||||
let ev = Event::BufferChange {
|
||||
path: Some(PathBuf::from("abc.txt")),
|
||||
old_path: Some(PathBuf::from("123.txt")),
|
||||
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
|
||||
predicted: false,
|
||||
};
|
||||
assert_eq!(
|
||||
ev.to_string(),
|
||||
indoc! {"
|
||||
--- a/123.txt
|
||||
+++ b/abc.txt
|
||||
@@ -1,2 +1,2 @@
|
||||
-a
|
||||
-b
|
||||
"}
|
||||
);
|
||||
|
||||
let ev = Event::BufferChange {
|
||||
path: Some(PathBuf::from("abc.txt")),
|
||||
old_path: Some(PathBuf::from("123.txt")),
|
||||
diff: "@@ -1,2 +1,2 @@\n-a\n-b\n".into(),
|
||||
predicted: true,
|
||||
};
|
||||
assert_eq!(
|
||||
ev.to_string(),
|
||||
indoc! {"
|
||||
// User accepted prediction:
|
||||
--- a/123.txt
|
||||
+++ b/abc.txt
|
||||
@@ -1,2 +1,2 @@
|
||||
-a
|
||||
-b
|
||||
"}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user