Fix splice edits generation

Co-Authored-By: Antonio Scandurra <antonio@zed.dev>
This commit is contained in:
Kirill Bulatov 2023-06-09 14:30:58 +03:00
parent 9ce9b73879
commit dbd4b33568

View file

@ -352,9 +352,8 @@ impl InlayMap {
.fold_snapshot .fold_snapshot
.to_fold_point(buffer_point, Bias::Left); .to_fold_point(buffer_point, Bias::Left);
let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point); let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point);
let inlay_point = snapshot.to_inlay_point(suggestion_point);
inlays.insert((inlay_point, Reverse(inlay.id)), Some(inlay)); inlays.insert((suggestion_point, Reverse(inlay.id)), Some(inlay));
} }
for inlay_id in to_remove { for inlay_id in to_remove {
@ -368,8 +367,7 @@ impl InlayMap {
.fold_snapshot .fold_snapshot
.to_fold_point(buffer_point, Bias::Left); .to_fold_point(buffer_point, Bias::Left);
let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point); let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point);
let inlay_point = snapshot.to_inlay_point(suggestion_point); inlays.insert((suggestion_point, Reverse(inlay.id)), None);
inlays.insert((inlay_point, Reverse(inlay.id)), None);
} }
} }
@ -377,12 +375,21 @@ impl InlayMap {
let mut new_transforms = SumTree::new(); let mut new_transforms = SumTree::new();
let mut cursor = snapshot let mut cursor = snapshot
.transforms .transforms
.cursor::<(InlayPoint, (SuggestionPoint, InlayOffset))>(); .cursor::<(SuggestionPoint, (InlayOffset, InlayPoint))>();
for ((inlay_point, inlay_id), inlay) in inlays { let mut inlays = inlays.into_iter().peekable();
new_transforms.push_tree(cursor.slice(&inlay_point, Bias::Right, &()), &()); while let Some(((suggestion_point, inlay_id), inlay)) = inlays.next() {
new_transforms.push_tree(cursor.slice(&suggestion_point, Bias::Left, &()), &());
while let Some(transform) = cursor.item() { while let Some(transform) = cursor.item() {
match transform { match transform {
Transform::Isomorphic(_) => break, Transform::Isomorphic(_) => {
if suggestion_point >= cursor.end(&()).0 {
new_transforms.push(transform.clone(), &());
cursor.next(&());
} else {
break;
}
}
Transform::Inlay(inlay) => { Transform::Inlay(inlay) => {
if inlay.id > inlay_id.0 { if inlay.id > inlay_id.0 {
new_transforms.push(transform.clone(), &()); new_transforms.push(transform.clone(), &());
@ -391,7 +398,7 @@ impl InlayMap {
if inlay.id == inlay_id.0 { if inlay.id == inlay_id.0 {
let new_start = InlayOffset(new_transforms.summary().output.len); let new_start = InlayOffset(new_transforms.summary().output.len);
inlay_edits.push(Edit { inlay_edits.push(Edit {
old: cursor.start().1 .1..cursor.end(&()).1 .1, old: cursor.start().1 .0..cursor.end(&()).1 .0,
new: new_start..new_start, new: new_start..new_start,
}); });
cursor.next(&()); cursor.next(&());
@ -406,42 +413,44 @@ impl InlayMap {
let new_start = InlayOffset(new_transforms.summary().output.len); let new_start = InlayOffset(new_transforms.summary().output.len);
let new_end = InlayOffset(new_start.0 + inlay.properties.text.len()); let new_end = InlayOffset(new_start.0 + inlay.properties.text.len());
if let Some(Transform::Isomorphic(transform)) = cursor.item() { if let Some(Transform::Isomorphic(transform)) = cursor.item() {
let prefix = inlay_point.0 - cursor.start().0 .0; let prefix_suggestion_start =
if !prefix.is_zero() { SuggestionPoint(new_transforms.summary().input.lines);
let prefix_suggestion_start = cursor.start().1 .0; push_isomorphic(
let prefix_suggestion_end = &mut new_transforms,
SuggestionPoint(cursor.start().1 .0 .0 + prefix); snapshot
new_transforms.push( .suggestion_snapshot
Transform::Isomorphic( .text_summary_for_range(prefix_suggestion_start..suggestion_point),
snapshot.suggestion_snapshot.text_summary_for_range( );
prefix_suggestion_start..prefix_suggestion_end, let old_start = snapshot.to_offset(InlayPoint(
), cursor.start().1 .1 .0 + (suggestion_point.0 - cursor.start().0 .0),
), ));
&(), inlay_edits.push(Edit {
); old: old_start..old_start,
} new: new_start..new_end,
});
new_transforms.push(Transform::Inlay(inlay), &()); new_transforms.push(Transform::Inlay(inlay), &());
let suffix_suggestion_start = SuggestionPoint(cursor.start().1 .0 .0 + prefix); if inlays.peek().map_or(true, |((suggestion_point, _), _)| {
let suffix_suggestion_end = cursor.end(&()).1 .0; *suggestion_point >= cursor.end(&()).0
new_transforms.push( }) {
Transform::Isomorphic(snapshot.suggestion_snapshot.text_summary_for_range( let suffix_suggestion_end = cursor.end(&()).0;
suffix_suggestion_start..suffix_suggestion_end, push_isomorphic(
)), &mut new_transforms,
&(), snapshot
); .suggestion_snapshot
.text_summary_for_range(suggestion_point..suffix_suggestion_end),
cursor.next(&()); );
cursor.next(&());
}
} else { } else {
let old_start = cursor.start().1 .0;
inlay_edits.push(Edit {
old: old_start..old_start,
new: new_start..new_end,
});
new_transforms.push(Transform::Inlay(inlay), &()); new_transforms.push(Transform::Inlay(inlay), &());
} }
let old_start = snapshot.to_offset(inlay_point);
inlay_edits.push(Edit {
old: old_start..old_start,
new: new_start..new_end,
});
} }
} }
@ -711,6 +720,10 @@ impl InlaySnapshot {
} }
fn push_isomorphic(sum_tree: &mut SumTree<Transform>, summary: TextSummary) { fn push_isomorphic(sum_tree: &mut SumTree<Transform>, summary: TextSummary) {
if summary.len == 0 {
return;
}
let mut summary = Some(summary); let mut summary = Some(summary);
sum_tree.update_last( sum_tree.update_last(
|transform| { |transform| {