Rename head text to indicate that it's not always going to be from head

Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Julia 2022-10-03 15:11:06 -04:00
parent a5c2f22bf7
commit e6487de069
11 changed files with 78 additions and 78 deletions

View file

@ -111,11 +111,11 @@ impl BufferDiff {
}
}
pub async fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
pub async fn update(&mut self, diff_base: &str, buffer: &text::BufferSnapshot) {
let mut tree = SumTree::new();
let buffer_text = buffer.as_rope().to_string();
let patch = Self::diff(&head_text, &buffer_text);
let patch = Self::diff(&diff_base, &buffer_text);
if let Some(patch) = patch {
let mut divergence = 0;
@ -228,7 +228,7 @@ impl BufferDiff {
pub fn assert_hunks<Iter>(
diff_hunks: Iter,
buffer: &BufferSnapshot,
head_text: &str,
diff_base: &str,
expected_hunks: &[(Range<u32>, &str, &str)],
) where
Iter: Iterator<Item = DiffHunk<u32>>,
@ -237,7 +237,7 @@ pub fn assert_hunks<Iter>(
.map(|hunk| {
(
hunk.buffer_range.clone(),
&head_text[hunk.head_byte_range],
&diff_base[hunk.head_byte_range],
buffer
.text_for_range(
Point::new(hunk.buffer_range.start, 0)
@ -264,7 +264,7 @@ mod tests {
#[test]
fn test_buffer_diff_simple() {
let head_text = "
let diff_base = "
one
two
three
@ -280,27 +280,27 @@ mod tests {
let mut buffer = Buffer::new(0, 0, buffer_text);
let mut diff = BufferDiff::new();
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_hunks(
diff.hunks(&buffer),
&buffer,
&head_text,
&diff_base,
&[(1..2, "two\n", "HELLO\n")],
);
buffer.edit([(0..0, "point five\n")]);
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_hunks(
diff.hunks(&buffer),
&buffer,
&head_text,
&diff_base,
&[(0..1, "", "point five\n"), (2..3, "two\n", "HELLO\n")],
);
}
#[test]
fn test_buffer_diff_range() {
let head_text = "
let diff_base = "
one
two
three
@ -337,13 +337,13 @@ mod tests {
let buffer = Buffer::new(0, 0, buffer_text);
let mut diff = BufferDiff::new();
smol::block_on(diff.update(&head_text, &buffer));
smol::block_on(diff.update(&diff_base, &buffer));
assert_eq!(diff.hunks(&buffer).count(), 8);
assert_hunks(
diff.hunks_in_range(7..12, &buffer),
&buffer,
&head_text,
&diff_base,
&[
(6..7, "", "HELLO\n"),
(9..10, "six\n", "SIXTEEN\n"),

View file

@ -10,12 +10,12 @@ pub use git2::Repository as LibGitRepository;
#[async_trait::async_trait]
pub trait GitRepository: Send {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
fn load_index(&self, relative_file_path: &Path) -> Option<String>;
}
#[async_trait::async_trait]
impl GitRepository for LibGitRepository {
fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
fn load_index(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0;
let index = repo.index()?;
@ -25,8 +25,7 @@ impl GitRepository for LibGitRepository {
};
let content = repo.find_blob(oid)?.content().to_owned();
let head_text = String::from_utf8(content)?;
Ok(Some(head_text))
Ok(Some(String::from_utf8(content)?))
}
match logic(&self, relative_file_path) {
@ -55,7 +54,7 @@ impl FakeGitRepository {
#[async_trait::async_trait]
impl GitRepository for FakeGitRepository {
fn load_head_text(&self, path: &Path) -> Option<String> {
fn load_index(&self, path: &Path) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
}