fix: preserve review comments across unrelated edits

This commit is contained in:
Valdemar Lorenzen
2026-10-20 14:58:56 +02:00
parent 2727425518
commit 31203889bb
10 changed files with 422 additions and 147 deletions
+42 -35
View File
@@ -744,29 +744,35 @@ impl Editor {
} else {
"unresolved"
};
let mapped = diff.review_anchor_range(anchor);
let current = mapped.is_some();
let location = if current {
"current"
} else {
"original snapshot"
};
let range = mapped.unwrap_or(anchor.start..anchor.end);
let title = format!(
"Comment #{id} · {status} · {}:{}:{}–{}:{} · {:?} · :w saves review",
"Comment #{id} · {status} · {location} · {}:{}:{}–{}:{} · {:?} · :w saves review",
anchor.path.display(),
anchor.start.row + 1,
anchor.start.column + 1,
anchor.end.row + 1,
anchor.end.column + 1,
range.start.row + 1,
range.start.column + 1,
range.end.row + 1,
range.end.column + 1,
anchor.side
);
let target = diff.comparison.as_ref().and_then(|comparison| {
(comparison.revisions[anchor.side.index()] == anchor.revision)
.then(|| {
comparison.files.iter().position(|file| {
let path = if anchor.side == Side::Before {
file.old_path.as_ref().unwrap_or(&file.path)
} else {
&file.path
};
path == &anchor.path
})
})
.flatten()
comparison.files.iter().position(|file| {
let path = if anchor.side == Side::Before {
file.old_path.as_ref().unwrap_or(&file.path)
} else {
&file.path
};
path == &anchor.path
})
});
let stale = !current
&& target.is_none_or(|index| diff.comparison.as_ref().unwrap().files[index].loaded);
let start = anchor.start;
let owner = self.diff_owner.unwrap();
self.select_pane(owner);
@@ -781,7 +787,7 @@ impl Editor {
self.place_navigation(start);
}
self.open_review_buffer(ReviewText::Comment(id), &text, title)?;
if target.is_none() {
if stale {
self.set_message("Comment is stale; original snapshot anchor preserved");
}
Ok(())
@@ -821,11 +827,11 @@ impl Editor {
if selected_path != Some(&comment.anchor.path) {
return;
}
if !diff.review_anchor_current(&comment.anchor) {
let Some(range) = diff.review_anchor_range(&comment.anchor) else {
self.set_message("Comment is stale; original snapshot anchor preserved");
return;
}
let position = comment.anchor.start;
};
let position = range.start;
let focus = if comment.anchor.side == Side::Before {
Focus::Before
} else {
@@ -867,18 +873,19 @@ impl Editor {
);
for comment in &state.document.comments {
let anchor = &comment.anchor;
let status = if diff.review_anchor_current(anchor) {
let mapped = diff.review_anchor_range(anchor);
let range = mapped.clone().unwrap_or(anchor.start..anchor.end);
let status = if mapped.is_some() {
"current"
} else if diff.comparison.as_ref().is_some_and(|comparison| {
comparison.revisions[anchor.side.index()] == anchor.revision
&& comparison.files.iter().any(|file| {
!file.loaded
&& if anchor.side == Side::Before {
file.old_path.as_ref().unwrap_or(&file.path) == &anchor.path
} else {
file.path == anchor.path
}
})
comparison.files.iter().any(|file| {
!file.loaded
&& if anchor.side == Side::Before {
file.old_path.as_ref().unwrap_or(&file.path) == &anchor.path
} else {
file.path == anchor.path
}
})
}) {
"not checked (file not loaded)"
} else {
@@ -895,10 +902,10 @@ impl Editor {
"\n## Comment #{} · {resolution} · {status}\n{}:{}:{}–{}:{} · {:?}\nRevision: {}\n{}\n",
comment.id,
anchor.path.display(),
anchor.start.row + 1,
anchor.start.column + 1,
anchor.end.row + 1,
anchor.end.column + 1,
range.start.row + 1,
range.start.column + 1,
range.end.row + 1,
range.end.column + 1,
anchor.side,
anchor.revision,
comment.body