You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
helix-plus/helix-vcs/src/diff/worker/test.rs

150 lines
3.6 KiB
Rust

Show (git) diff signs in gutter (#3890) * Show (git) diff signs in gutter (#3890) Avoid string allocation when git diffing Incrementally diff using changesets refactor diffs to be provider indepndent and improve git implementation remove dependency on zlib-ng switch to asynchronus diffing with similar Update helix-vcs/Cargo.toml fix toml formatting Co-authored-by: Ivan Tham <pickfire@riseup.net> fix typo in documentation use ropey reexpors from helix-core fix crash when creating new file remove useless use if io::Cursor fix spelling mistakes implement suggested improvement to repository loading improve git test isolation remove lefover comments Co-authored-by: univerz <univerz@fu-solution.com> fixed spelling mistake minor cosmetic changes fix: set self.differ to None if decoding the diff_base fails fixup formatting Co-authored-by: Ivan Tham <pickfire@riseup.net> reload diff_base when file is reloaded from disk switch to imara-diff Fixup formatting Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> Redraw buffer whenever a diff is updated. Only store hunks instead of changes for individual lines to easily allow jumping between them Update to latest gitoxide version Change default diff gutter position Only update gutter after timeout * update diff gutter synchronously, with a timeout * Apply suggestions from code review Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> Co-authored-by: Michael Davis <mcarsondavis@gmail.com> * address review comments and ensure lock is always aquired * remove configuration for redraw timeout Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
1 year ago
use helix_core::Rope;
use tokio::task::JoinHandle;
use crate::diff::{DiffHandle, Hunk};
impl DiffHandle {
fn new_test(diff_base: &str, doc: &str) -> (DiffHandle, JoinHandle<()>) {
DiffHandle::new_with_handle(
Rope::from_str(diff_base),
Rope::from_str(doc),
Default::default(),
)
}
async fn into_diff(self, handle: JoinHandle<()>) -> Vec<Hunk> {
let hunks = self.hunks;
// dropping the channel terminates the task
drop(self.channel);
handle.await.unwrap();
let hunks = hunks.lock();
Vec::clone(&*hunks)
}
}
#[tokio::test]
async fn append_line() {
let (differ, handle) = DiffHandle::new_test("foo\n", "foo\nbar\n");
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[Hunk {
before: 1..1,
after: 1..2
}]
)
}
#[tokio::test]
async fn prepend_line() {
let (differ, handle) = DiffHandle::new_test("foo\n", "bar\nfoo\n");
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[Hunk {
before: 0..0,
after: 0..1
}]
)
}
#[tokio::test]
async fn modify() {
let (differ, handle) = DiffHandle::new_test("foo\nbar\n", "foo bar\nbar\n");
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[Hunk {
before: 0..1,
after: 0..1
}]
)
}
#[tokio::test]
async fn delete_line() {
let (differ, handle) = DiffHandle::new_test("foo\nfoo bar\nbar\n", "foo\nbar\n");
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[Hunk {
before: 1..2,
after: 1..1
}]
)
}
#[tokio::test]
async fn delete_line_and_modify() {
let (differ, handle) = DiffHandle::new_test("foo\nbar\ntest\nfoo", "foo\ntest\nfoo bar");
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[
Hunk {
before: 1..2,
after: 1..1
},
Hunk {
before: 3..4,
after: 2..3
},
]
)
}
#[tokio::test]
async fn add_use() {
let (differ, handle) = DiffHandle::new_test(
"use ropey::Rope;\nuse tokio::task::JoinHandle;\n",
"use ropey::Rope;\nuse ropey::RopeSlice;\nuse tokio::task::JoinHandle;\n",
);
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[Hunk {
before: 1..1,
after: 1..2
},]
)
}
#[tokio::test]
async fn update_document() {
let (differ, handle) = DiffHandle::new_test("foo\nbar\ntest\nfoo", "foo\nbar\ntest\nfoo");
differ.update_document(Rope::from_str("foo\ntest\nfoo bar"), false);
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[
Hunk {
before: 1..2,
after: 1..1
},
Hunk {
before: 3..4,
after: 2..3
},
]
)
}
#[tokio::test]
async fn update_base() {
let (differ, handle) = DiffHandle::new_test("foo\ntest\nfoo bar", "foo\ntest\nfoo bar");
differ.update_diff_base(Rope::from_str("foo\nbar\ntest\nfoo"));
let line_diffs = differ.into_diff(handle).await;
assert_eq!(
&line_diffs,
&[
Hunk {
before: 1..2,
after: 1..1
},
Hunk {
before: 3..4,
after: 2..3
},
]
)
}