2022-09-16 18:49:24 +00:00
|
|
|
use std::ops::Range;
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-15 17:57:57 +00:00
|
|
|
use sum_tree::SumTree;
|
2022-09-15 20:17:17 +00:00
|
|
|
use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, ToPoint};
|
2022-09-01 21:22:12 +00:00
|
|
|
|
|
|
|
pub use git2 as libgit;
|
2022-09-15 17:57:57 +00:00
|
|
|
use libgit::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
|
2022-09-01 21:22:12 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum DiffHunkStatus {
|
|
|
|
Added,
|
|
|
|
Modified,
|
|
|
|
Removed,
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-09-01 21:22:12 +00:00
|
|
|
pub struct DiffHunk<T> {
|
|
|
|
pub buffer_range: Range<T>,
|
2022-09-14 00:41:38 +00:00
|
|
|
pub head_byte_range: Range<usize>,
|
2022-09-01 21:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiffHunk<u32> {
|
|
|
|
pub fn status(&self) -> DiffHunkStatus {
|
2022-09-14 00:41:38 +00:00
|
|
|
if self.head_byte_range.is_empty() {
|
2022-09-01 21:22:12 +00:00
|
|
|
DiffHunkStatus::Added
|
|
|
|
} else if self.buffer_range.is_empty() {
|
|
|
|
DiffHunkStatus::Removed
|
|
|
|
} else {
|
|
|
|
DiffHunkStatus::Modified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:52:42 +00:00
|
|
|
impl sum_tree::Item for DiffHunk<Anchor> {
|
|
|
|
type Summary = DiffHunkSummary;
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-09 15:52:42 +00:00
|
|
|
fn summary(&self) -> Self::Summary {
|
|
|
|
DiffHunkSummary {
|
2022-09-09 19:40:33 +00:00
|
|
|
buffer_range: self.buffer_range.clone(),
|
2022-09-14 00:41:38 +00:00
|
|
|
head_range: self.head_byte_range.clone(),
|
2022-09-01 21:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct DiffHunkSummary {
|
2022-09-09 19:40:33 +00:00
|
|
|
buffer_range: Range<Anchor>,
|
2022-09-09 21:32:19 +00:00
|
|
|
head_range: Range<usize>,
|
2022-09-09 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl sum_tree::Summary for DiffHunkSummary {
|
2022-09-09 19:40:33 +00:00
|
|
|
type Context = text::BufferSnapshot;
|
2022-09-09 15:52:42 +00:00
|
|
|
|
|
|
|
fn add_summary(&mut self, other: &Self, _: &Self::Context) {
|
|
|
|
self.head_range.start = self.head_range.start.min(other.head_range.start);
|
|
|
|
self.head_range.end = self.head_range.end.max(other.head_range.end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
2022-09-09 21:32:19 +00:00
|
|
|
struct HunkHeadEnd(usize);
|
2022-09-09 15:52:42 +00:00
|
|
|
|
|
|
|
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkHeadEnd {
|
2022-09-09 19:40:33 +00:00
|
|
|
fn add_summary(&mut self, summary: &'a DiffHunkSummary, _: &text::BufferSnapshot) {
|
2022-09-09 15:52:42 +00:00
|
|
|
self.0 = summary.head_range.end;
|
|
|
|
}
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
fn from_summary(summary: &'a DiffHunkSummary, _: &text::BufferSnapshot) -> Self {
|
2022-09-09 15:52:42 +00:00
|
|
|
HunkHeadEnd(summary.head_range.end)
|
2022-09-01 21:22:12 +00:00
|
|
|
}
|
2022-09-09 15:52:42 +00:00
|
|
|
}
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-14 00:41:38 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
struct HunkBufferStart(u32);
|
|
|
|
|
|
|
|
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkBufferStart {
|
|
|
|
fn add_summary(&mut self, summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) {
|
|
|
|
self.0 = summary.buffer_range.start.to_point(buffer).row;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_summary(summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) -> Self {
|
|
|
|
HunkBufferStart(summary.buffer_range.start.to_point(buffer).row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
struct HunkBufferEnd(u32);
|
|
|
|
|
|
|
|
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkBufferEnd {
|
|
|
|
fn add_summary(&mut self, summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) {
|
|
|
|
self.0 = summary.buffer_range.end.to_point(buffer).row;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_summary(summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) -> Self {
|
|
|
|
HunkBufferEnd(summary.buffer_range.end.to_point(buffer).row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-09-15 23:06:45 +00:00
|
|
|
pub struct BufferDiff {
|
2022-09-16 18:49:24 +00:00
|
|
|
last_buffer_version: Option<clock::Global>,
|
2022-09-09 19:40:33 +00:00
|
|
|
tree: SumTree<DiffHunk<Anchor>>,
|
|
|
|
}
|
|
|
|
|
2022-09-15 23:06:45 +00:00
|
|
|
impl BufferDiff {
|
2022-09-16 18:49:24 +00:00
|
|
|
pub fn new() -> BufferDiff {
|
|
|
|
BufferDiff {
|
|
|
|
last_buffer_version: None,
|
2022-09-15 23:06:45 +00:00
|
|
|
tree: SumTree::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
pub fn hunks_in_range<'a>(
|
|
|
|
&'a self,
|
|
|
|
query_row_range: Range<u32>,
|
|
|
|
buffer: &'a BufferSnapshot,
|
|
|
|
) -> impl 'a + Iterator<Item = DiffHunk<u32>> {
|
|
|
|
self.tree.iter().filter_map(move |hunk| {
|
|
|
|
let range = hunk.buffer_range.to_point(&buffer);
|
|
|
|
|
2022-09-14 16:38:23 +00:00
|
|
|
if range.start.row <= query_row_range.end && query_row_range.start <= range.end.row {
|
2022-09-09 19:40:33 +00:00
|
|
|
let end_row = if range.end.column > 0 {
|
|
|
|
range.end.row + 1
|
|
|
|
} else {
|
|
|
|
range.end.row
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(DiffHunk {
|
|
|
|
buffer_range: range.start.row..end_row,
|
2022-09-14 00:41:38 +00:00
|
|
|
head_byte_range: hunk.head_byte_range.clone(),
|
2022-09-09 19:40:33 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-15 23:06:45 +00:00
|
|
|
pub fn needs_update(&self, buffer: &text::BufferSnapshot) -> bool {
|
2022-09-16 18:49:24 +00:00
|
|
|
match &self.last_buffer_version {
|
|
|
|
Some(last) => buffer.version().changed_since(last),
|
|
|
|
None => true,
|
|
|
|
}
|
2022-09-09 19:40:33 +00:00
|
|
|
}
|
2022-09-09 15:52:42 +00:00
|
|
|
|
2022-09-16 18:49:24 +00:00
|
|
|
pub async fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
|
2022-09-14 22:44:00 +00:00
|
|
|
let mut tree = SumTree::new();
|
2022-09-12 19:38:44 +00:00
|
|
|
|
2022-09-14 22:44:00 +00:00
|
|
|
let buffer_text = buffer.as_rope().to_string();
|
|
|
|
let patch = Self::diff(&head_text, &buffer_text);
|
|
|
|
|
|
|
|
if let Some(patch) = patch {
|
2022-09-15 21:44:01 +00:00
|
|
|
let mut divergence = 0;
|
2022-09-14 22:44:00 +00:00
|
|
|
for hunk_index in 0..patch.num_hunks() {
|
2022-09-15 21:44:01 +00:00
|
|
|
let hunk = Self::process_patch_hunk(&patch, hunk_index, buffer, &mut divergence);
|
2022-09-14 22:44:00 +00:00
|
|
|
tree.push(hunk, buffer);
|
|
|
|
}
|
2022-09-14 00:41:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:06:45 +00:00
|
|
|
self.tree = tree;
|
2022-09-16 18:49:24 +00:00
|
|
|
self.last_buffer_version = Some(buffer.version().clone());
|
2022-09-15 23:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn hunks<'a>(&'a self, text: &'a BufferSnapshot) -> impl 'a + Iterator<Item = DiffHunk<u32>> {
|
|
|
|
self.hunks_in_range(0..u32::MAX, text)
|
2022-09-14 00:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn diff<'a>(head: &'a str, current: &'a str) -> Option<GitPatch<'a>> {
|
2022-09-12 19:38:44 +00:00
|
|
|
let mut options = GitOptions::default();
|
|
|
|
options.context_lines(0);
|
2022-09-14 00:41:38 +00:00
|
|
|
|
|
|
|
let patch = GitPatch::from_buffers(
|
|
|
|
head.as_bytes(),
|
2022-09-12 19:38:44 +00:00
|
|
|
None,
|
2022-09-14 00:41:38 +00:00
|
|
|
current.as_bytes(),
|
2022-09-12 19:38:44 +00:00
|
|
|
None,
|
|
|
|
Some(&mut options),
|
2022-09-14 00:41:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
match patch {
|
|
|
|
Ok(patch) => Some(patch),
|
2022-09-12 19:38:44 +00:00
|
|
|
|
2022-09-14 00:41:38 +00:00
|
|
|
Err(err) => {
|
|
|
|
log::error!("`GitPatch::from_buffers` failed: {}", err);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_patch_hunk<'a>(
|
|
|
|
patch: &GitPatch<'a>,
|
|
|
|
hunk_index: usize,
|
|
|
|
buffer: &text::BufferSnapshot,
|
2022-09-15 21:44:01 +00:00
|
|
|
buffer_row_divergence: &mut i64,
|
2022-09-14 00:41:38 +00:00
|
|
|
) -> DiffHunk<Anchor> {
|
2022-09-14 16:38:23 +00:00
|
|
|
let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
|
|
|
|
assert!(line_item_count > 0);
|
|
|
|
|
|
|
|
let mut first_deletion_buffer_row: Option<u32> = None;
|
2022-09-15 20:17:17 +00:00
|
|
|
let mut buffer_row_range: Option<Range<u32>> = None;
|
2022-09-14 00:41:38 +00:00
|
|
|
let mut head_byte_range: Option<Range<usize>> = None;
|
|
|
|
|
2022-09-14 16:38:23 +00:00
|
|
|
for line_index in 0..line_item_count {
|
2022-09-14 00:41:38 +00:00
|
|
|
let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
|
|
|
|
let kind = line.origin_value();
|
|
|
|
let content_offset = line.content_offset() as isize;
|
2022-09-14 15:20:01 +00:00
|
|
|
let content_len = line.content().len() as isize;
|
2022-09-14 00:41:38 +00:00
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
if kind == GitDiffLineType::Addition {
|
|
|
|
*buffer_row_divergence += 1;
|
|
|
|
let row = line.new_lineno().unwrap().saturating_sub(1);
|
2022-09-14 00:41:38 +00:00
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
match &mut buffer_row_range {
|
|
|
|
Some(buffer_row_range) => buffer_row_range.end = row + 1,
|
|
|
|
None => buffer_row_range = Some(row..row + 1),
|
2022-09-14 00:41:38 +00:00
|
|
|
}
|
2022-09-15 21:44:01 +00:00
|
|
|
}
|
2022-09-14 00:41:38 +00:00
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
if kind == GitDiffLineType::Deletion {
|
|
|
|
*buffer_row_divergence -= 1;
|
|
|
|
let end = content_offset + content_len;
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
match &mut head_byte_range {
|
|
|
|
Some(head_byte_range) => head_byte_range.end = end as usize,
|
|
|
|
None => head_byte_range = Some(content_offset as usize..end as usize),
|
2022-09-14 00:41:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
if first_deletion_buffer_row.is_none() {
|
|
|
|
let old_row = line.old_lineno().unwrap().saturating_sub(1);
|
|
|
|
let row = old_row as i64 + *buffer_row_divergence;
|
|
|
|
first_deletion_buffer_row = Some(row as u32);
|
|
|
|
}
|
2022-09-14 16:38:23 +00:00
|
|
|
}
|
2022-09-14 00:41:38 +00:00
|
|
|
}
|
2022-09-01 21:22:12 +00:00
|
|
|
|
2022-09-14 00:41:38 +00:00
|
|
|
//unwrap_or deletion without addition
|
2022-09-15 21:44:01 +00:00
|
|
|
let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
|
2022-09-14 16:38:23 +00:00
|
|
|
//we cannot have an addition-less hunk without deletion(s) or else there would be no hunk
|
|
|
|
let row = first_deletion_buffer_row.unwrap();
|
2022-09-15 20:17:17 +00:00
|
|
|
row..row
|
2022-09-14 16:38:23 +00:00
|
|
|
});
|
|
|
|
|
2022-09-14 00:41:38 +00:00
|
|
|
//unwrap_or addition without deletion
|
|
|
|
let head_byte_range = head_byte_range.unwrap_or(0..0);
|
|
|
|
|
2022-09-15 21:44:01 +00:00
|
|
|
let start = Point::new(buffer_row_range.start, 0);
|
|
|
|
let end = Point::new(buffer_row_range.end, 0);
|
2022-09-15 20:17:17 +00:00
|
|
|
let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
|
2022-09-14 00:41:38 +00:00
|
|
|
DiffHunk {
|
2022-09-15 20:17:17 +00:00
|
|
|
buffer_range,
|
2022-09-14 00:41:38 +00:00
|
|
|
head_byte_range,
|
2022-09-01 21:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 19:40:33 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use text::Buffer;
|
|
|
|
use unindent::Unindent as _;
|
|
|
|
|
2022-09-28 15:43:33 +00:00
|
|
|
#[test]
|
2022-09-09 19:40:33 +00:00
|
|
|
fn test_buffer_diff_simple() {
|
|
|
|
let head_text = "
|
|
|
|
one
|
|
|
|
two
|
|
|
|
three
|
|
|
|
"
|
|
|
|
.unindent();
|
|
|
|
|
|
|
|
let buffer_text = "
|
|
|
|
one
|
|
|
|
hello
|
|
|
|
three
|
|
|
|
"
|
|
|
|
.unindent();
|
|
|
|
|
|
|
|
let mut buffer = Buffer::new(0, 0, buffer_text);
|
2022-09-16 18:49:24 +00:00
|
|
|
let mut diff = BufferDiff::new();
|
|
|
|
smol::block_on(diff.update(&head_text, &buffer));
|
2022-09-09 21:32:19 +00:00
|
|
|
assert_hunks(&diff, &buffer, &head_text, &[(1..2, "two\n")]);
|
2022-09-09 19:40:33 +00:00
|
|
|
|
|
|
|
buffer.edit([(0..0, "point five\n")]);
|
2022-09-09 21:32:19 +00:00
|
|
|
assert_hunks(&diff, &buffer, &head_text, &[(2..3, "two\n")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn assert_hunks(
|
|
|
|
diff: &BufferDiff,
|
|
|
|
buffer: &BufferSnapshot,
|
|
|
|
head_text: &str,
|
|
|
|
expected_hunks: &[(Range<u32>, &str)],
|
|
|
|
) {
|
2022-09-15 23:06:45 +00:00
|
|
|
let hunks = diff.hunks(buffer).collect::<Vec<_>>();
|
2022-09-09 19:40:33 +00:00
|
|
|
assert_eq!(
|
2022-09-09 21:32:19 +00:00
|
|
|
hunks.len(),
|
|
|
|
expected_hunks.len(),
|
|
|
|
"actual hunks are {hunks:#?}"
|
2022-09-09 19:40:33 +00:00
|
|
|
);
|
2022-09-09 21:32:19 +00:00
|
|
|
|
|
|
|
let diff_iter = hunks.iter().enumerate();
|
|
|
|
for ((index, hunk), (expected_range, expected_str)) in diff_iter.zip(expected_hunks) {
|
|
|
|
assert_eq!(&hunk.buffer_range, expected_range, "for hunk {index}");
|
|
|
|
assert_eq!(
|
2022-09-14 00:41:38 +00:00
|
|
|
&head_text[hunk.head_byte_range.clone()],
|
2022-09-09 21:32:19 +00:00
|
|
|
*expected_str,
|
|
|
|
"for hunk {index}"
|
|
|
|
);
|
|
|
|
}
|
2022-09-09 19:40:33 +00:00
|
|
|
}
|
|
|
|
}
|