Use read() over read_with() to improve readability in simple cases (#31455)

Follow up to: #31263 

Release Notes:

- N/A
This commit is contained in:
Joseph T. Lyons 2025-05-26 16:14:07 -04:00 committed by GitHub
parent 5bafb2b160
commit 534bb0620d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 80 additions and 94 deletions

View file

@ -6601,8 +6601,7 @@ impl Editor {
}
// Store the transaction ID and selections before applying the edit
let transaction_id_prev =
self.buffer.read_with(cx, |b, cx| b.last_transaction_id(cx));
let transaction_id_prev = self.buffer.read(cx).last_transaction_id(cx);
let snapshot = self.buffer.read(cx).snapshot(cx);
let last_edit_end = edits.last().unwrap().0.end.bias_right(&snapshot);
@ -6616,9 +6615,7 @@ impl Editor {
});
let selections = self.selections.disjoint_anchors();
if let Some(transaction_id_now) =
self.buffer.read_with(cx, |b, cx| b.last_transaction_id(cx))
{
if let Some(transaction_id_now) = self.buffer.read(cx).last_transaction_id(cx) {
let has_new_transaction = transaction_id_prev != Some(transaction_id_now);
if has_new_transaction {
self.selection_history
@ -7114,9 +7111,10 @@ impl Editor {
for (buffer_snapshot, range, excerpt_id) in
multi_buffer_snapshot.range_to_buffer_ranges(range)
{
let Some(buffer) = project.read_with(cx, |this, cx| {
this.buffer_for_id(buffer_snapshot.remote_id(), cx)
}) else {
let Some(buffer) = project
.read(cx)
.buffer_for_id(buffer_snapshot.remote_id(), cx)
else {
continue;
};
let breakpoints = breakpoint_store.read(cx).breakpoints(
@ -9724,7 +9722,7 @@ impl Editor {
})?;
let enclosing_excerpt = breakpoint_position.excerpt_id;
let buffer = project.read_with(cx, |project, cx| project.buffer_for_id(buffer_id, cx))?;
let buffer = project.read(cx).buffer_for_id(buffer_id, cx)?;
let buffer_snapshot = buffer.read(cx).snapshot();
let row = buffer_snapshot
@ -15153,7 +15151,7 @@ impl Editor {
}
};
let transaction_id_prev = buffer.read_with(cx, |b, cx| b.last_transaction_id(cx));
let transaction_id_prev = buffer.read(cx).last_transaction_id(cx);
let selections_prev = transaction_id_prev
.and_then(|transaction_id_prev| {
// default to selections as they were after the last edit, if we have them,
@ -19516,9 +19514,7 @@ impl CollaborationHub for Entity<Project> {
fn user_names(&self, cx: &App) -> HashMap<u64, SharedString> {
let this = self.read(cx);
let user_ids = this.collaborators().values().map(|c| c.user_id);
this.user_store().read_with(cx, |user_store, cx| {
user_store.participant_names(user_ids, cx)
})
this.user_store().read(cx).participant_names(user_ids, cx)
}
}

View file

@ -1533,9 +1533,7 @@ impl EditorElement {
window: &mut Window,
cx: &mut App,
) -> Option<MinimapLayout> {
let minimap_editor = self
.editor
.read_with(cx, |editor, _| editor.minimap().cloned())?;
let minimap_editor = self.editor.read(cx).minimap().cloned()?;
let minimap_settings = EditorSettings::get_global(cx).minimap;
@ -1581,12 +1579,10 @@ impl EditorElement {
);
let minimap_line_height = self.get_minimap_line_height(
minimap_editor
.read_with(cx, |editor, _| {
editor
.text_style_refinement
.as_ref()
.and_then(|refinement| refinement.font_size)
})
.read(cx)
.text_style_refinement
.as_ref()
.and_then(|refinement| refinement.font_size)
.unwrap_or(MINIMAP_FONT_SIZE),
window,
cx,
@ -7562,14 +7558,14 @@ impl Element for EditorElement {
let scrollbars_shown = settings.scrollbar.show != ShowScrollbar::Never;
let vertical_scrollbar_width = (scrollbars_shown
&& settings.scrollbar.axes.vertical
&& self
.editor
.read_with(cx, |editor, _| editor.show_scrollbars))
.then_some(style.scrollbar_width)
.unwrap_or_default();
&& self.editor.read(cx).show_scrollbars)
.then_some(style.scrollbar_width)
.unwrap_or_default();
let minimap_width = self
.editor
.read_with(cx, |editor, _| editor.minimap().is_some())
.read(cx)
.minimap()
.is_some()
.then(|| match settings.minimap.show {
ShowMinimap::Auto => {
scrollbars_shown.then_some(MinimapLayout::MINIMAP_WIDTH)

View file

@ -458,13 +458,12 @@ pub(crate) fn handle_from(
let ensure_no_edits_since_start = || -> Option<()> {
let has_edits_since_start = this
.read_with(cx, |this, cx| {
this.buffer.read_with(cx, |buffer, cx| {
buffer.buffer(buffer_id).map_or(true, |buffer| {
buffer.read_with(cx, |buffer, _| {
buffer.has_edits_since(&buffer_version_initial)
})
this.buffer
.read(cx)
.buffer(buffer_id)
.map_or(true, |buffer| {
buffer.read(cx).has_edits_since(&buffer_version_initial)
})
})
})
.ok()?;
@ -507,9 +506,7 @@ pub(crate) fn handle_from(
ensure_no_edits_since_start()?;
let multi_buffer_snapshot = this
.read_with(cx, |this, cx| {
this.buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx))
})
.read_with(cx, |this, cx| this.buffer.read(cx).snapshot(cx))
.ok()?;
let mut base_selections = Vec::new();