Fix more improper uses of the buffer_id field of Anchor (#36636)

Follow-up to #36524 

Release Notes:

- N/A
This commit is contained in:
Cole Miller 2025-08-21 09:24:34 -04:00 committed by GitHub
parent f63d8e4c53
commit 1dd237139c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 49 additions and 59 deletions

View file

@ -6697,7 +6697,6 @@ impl Editor {
return; return;
} }
let buffer_id = cursor_position.buffer_id;
let buffer = this.buffer.read(cx); let buffer = this.buffer.read(cx);
if buffer if buffer
.text_anchor_for_position(cursor_position, cx) .text_anchor_for_position(cursor_position, cx)
@ -6710,8 +6709,8 @@ impl Editor {
let mut write_ranges = Vec::new(); let mut write_ranges = Vec::new();
let mut read_ranges = Vec::new(); let mut read_ranges = Vec::new();
for highlight in highlights { for highlight in highlights {
for (excerpt_id, excerpt_range) in let buffer_id = cursor_buffer.read(cx).remote_id();
buffer.excerpts_for_buffer(cursor_buffer.read(cx).remote_id(), cx) for (excerpt_id, excerpt_range) in buffer.excerpts_for_buffer(buffer_id, cx)
{ {
let start = highlight let start = highlight
.range .range
@ -6726,12 +6725,12 @@ impl Editor {
} }
let range = Anchor { let range = Anchor {
buffer_id, buffer_id: Some(buffer_id),
excerpt_id, excerpt_id,
text_anchor: start, text_anchor: start,
diff_base_anchor: None, diff_base_anchor: None,
}..Anchor { }..Anchor {
buffer_id, buffer_id: Some(buffer_id),
excerpt_id, excerpt_id,
text_anchor: end, text_anchor: end,
diff_base_anchor: None, diff_base_anchor: None,
@ -9496,17 +9495,21 @@ impl Editor {
selection: Range<Anchor>, selection: Range<Anchor>,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
let buffer_id = match (&selection.start.buffer_id, &selection.end.buffer_id) { let Some((_, buffer, _)) = self
(Some(a), Some(b)) if a == b => a, .buffer()
_ => { .read(cx)
log::error!("expected anchor range to have matching buffer IDs"); .excerpt_containing(selection.start, cx)
return; else {
}
};
let multi_buffer = self.buffer().read(cx);
let Some(buffer) = multi_buffer.buffer(*buffer_id) else {
return; return;
}; };
let Some((_, end_buffer, _)) = self.buffer().read(cx).excerpt_containing(selection.end, cx)
else {
return;
};
if buffer != end_buffer {
log::error!("expected anchor range to have matching buffer IDs");
return;
}
let id = post_inc(&mut self.next_completion_id); let id = post_inc(&mut self.next_completion_id);
let snippet_sort_order = EditorSettings::get_global(cx).snippet_sort_order; let snippet_sort_order = EditorSettings::get_global(cx).snippet_sort_order;
@ -10593,16 +10596,12 @@ impl Editor {
snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> Option<(Anchor, Breakpoint)> { ) -> Option<(Anchor, Breakpoint)> {
let project = self.project.clone()?; let buffer = self
.buffer
let buffer_id = breakpoint_position.buffer_id.or_else(|| { .read(cx)
snapshot .buffer_for_anchor(breakpoint_position, cx)?;
.buffer_snapshot
.buffer_id_for_excerpt(breakpoint_position.excerpt_id)
})?;
let enclosing_excerpt = breakpoint_position.excerpt_id; let enclosing_excerpt = breakpoint_position.excerpt_id;
let buffer = project.read(cx).buffer_for_id(buffer_id, cx)?;
let buffer_snapshot = buffer.read(cx).snapshot(); let buffer_snapshot = buffer.read(cx).snapshot();
let row = buffer_snapshot let row = buffer_snapshot
@ -10775,21 +10774,11 @@ impl Editor {
return; return;
}; };
let Some(buffer_id) = breakpoint_position.buffer_id.or_else(|| { let Some(buffer) = self
if breakpoint_position == Anchor::min() { .buffer
self.buffer() .read(cx)
.read(cx) .buffer_for_anchor(breakpoint_position, cx)
.excerpt_buffer_ids() else {
.into_iter()
.next()
} else {
None
}
}) else {
return;
};
let Some(buffer) = self.buffer().read(cx).buffer(buffer_id) else {
return; return;
}; };
@ -15432,7 +15421,8 @@ impl Editor {
return; return;
}; };
let Some(buffer_id) = buffer.anchor_after(next_diagnostic.range.start).buffer_id else { let next_diagnostic_start = buffer.anchor_after(next_diagnostic.range.start);
let Some(buffer_id) = buffer.buffer_id_for_anchor(next_diagnostic_start) else {
return; return;
}; };
self.change_selections(Default::default(), window, cx, |s| { self.change_selections(Default::default(), window, cx, |s| {
@ -20425,11 +20415,8 @@ impl Editor {
.range_to_buffer_ranges_with_deleted_hunks(selection.range()) .range_to_buffer_ranges_with_deleted_hunks(selection.range())
{ {
if let Some(anchor) = anchor { if let Some(anchor) = anchor {
// selection is in a deleted hunk let Some(buffer_handle) = multi_buffer.buffer_for_anchor(anchor, cx)
let Some(buffer_id) = anchor.buffer_id else { else {
continue;
};
let Some(buffer_handle) = multi_buffer.buffer(buffer_id) else {
continue; continue;
}; };
let offset = text::ToOffset::to_offset( let offset = text::ToOffset::to_offset(

View file

@ -321,7 +321,10 @@ pub fn update_inlay_link_and_hover_points(
if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_hint.id) { if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_hint.id) {
match cached_hint.resolve_state { match cached_hint.resolve_state {
ResolveState::CanResolve(_, _) => { ResolveState::CanResolve(_, _) => {
if let Some(buffer_id) = previous_valid_anchor.buffer_id { if let Some(buffer_id) = snapshot
.buffer_snapshot
.buffer_id_for_anchor(previous_valid_anchor)
{
inlay_hint_cache.spawn_hint_resolve( inlay_hint_cache.spawn_hint_resolve(
buffer_id, buffer_id,
excerpt_id, excerpt_id,

View file

@ -475,10 +475,7 @@ impl InlayHintCache {
let excerpt_cached_hints = excerpt_cached_hints.read(); let excerpt_cached_hints = excerpt_cached_hints.read();
let mut excerpt_cache = excerpt_cached_hints.ordered_hints.iter().fuse().peekable(); let mut excerpt_cache = excerpt_cached_hints.ordered_hints.iter().fuse().peekable();
shown_excerpt_hints_to_remove.retain(|(shown_anchor, shown_hint_id)| { shown_excerpt_hints_to_remove.retain(|(shown_anchor, shown_hint_id)| {
let Some(buffer) = shown_anchor let Some(buffer) = multi_buffer.buffer_for_anchor(*shown_anchor, cx) else {
.buffer_id
.and_then(|buffer_id| multi_buffer.buffer(buffer_id))
else {
return false; return false;
}; };
let buffer_snapshot = buffer.read(cx).snapshot(); let buffer_snapshot = buffer.read(cx).snapshot();

View file

@ -72,7 +72,7 @@ pub(super) fn refresh_linked_ranges(
// Throw away selections spanning multiple buffers. // Throw away selections spanning multiple buffers.
continue; continue;
} }
if let Some(buffer) = end_position.buffer_id.and_then(|id| buffer.buffer(id)) { if let Some(buffer) = buffer.buffer_for_anchor(end_position, cx) {
applicable_selections.push(( applicable_selections.push((
buffer, buffer,
start_position.text_anchor, start_position.text_anchor,

View file

@ -190,14 +190,16 @@ pub fn deploy_context_menu(
.all::<PointUtf16>(cx) .all::<PointUtf16>(cx)
.into_iter() .into_iter()
.any(|s| !s.is_empty()); .any(|s| !s.is_empty());
let has_git_repo = anchor.buffer_id.is_some_and(|buffer_id| { let has_git_repo = buffer
project .buffer_id_for_anchor(anchor)
.read(cx) .is_some_and(|buffer_id| {
.git_store() project
.read(cx) .read(cx)
.repository_and_path_for_buffer_id(buffer_id, cx) .git_store()
.is_some() .read(cx)
}); .repository_and_path_for_buffer_id(buffer_id, cx)
.is_some()
});
let evaluate_selection = window.is_action_available(&EvaluateSelectedText, cx); let evaluate_selection = window.is_action_available(&EvaluateSelectedText, cx);
let run_to_cursor = window.is_action_available(&RunToCursor, cx); let run_to_cursor = window.is_action_available(&RunToCursor, cx);

View file

@ -4393,12 +4393,13 @@ impl OutlinePanel {
}) })
.filter(|(match_range, _)| { .filter(|(match_range, _)| {
let editor = active_editor.read(cx); let editor = active_editor.read(cx);
if let Some(buffer_id) = match_range.start.buffer_id let snapshot = editor.buffer().read(cx).snapshot(cx);
if let Some(buffer_id) = snapshot.buffer_id_for_anchor(match_range.start)
&& editor.is_buffer_folded(buffer_id, cx) && editor.is_buffer_folded(buffer_id, cx)
{ {
return false; return false;
} }
if let Some(buffer_id) = match_range.start.buffer_id if let Some(buffer_id) = snapshot.buffer_id_for_anchor(match_range.end)
&& editor.is_buffer_folded(buffer_id, cx) && editor.is_buffer_folded(buffer_id, cx)
{ {
return false; return false;