Move "async move" a few characters to the left in cx.spawn() (#26758)

This is the core change:
https://github.com/zed-industries/zed/pull/26758/files#diff-044302c0d57147af17e68a0009fee3e8dcdfb4f32c27a915e70cfa80e987f765R1052

TODO:
- [x] Use AsyncFn instead of Fn() -> Future in GPUI spawn methods
- [x] Implement it in the whole app
- [x] Implement it in the debugger 
- [x] Glance at the RPC crate, and see if those box future methods can
be switched over. Answer: It can't directly, as you can't make an
AsyncFn* into a trait object. There's ways around that, but they're all
more complex than just keeping the code as is.
- [ ] Fix platform specific code

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2025-03-18 19:09:02 -07:00 committed by GitHub
parent 7f2e3fb5bd
commit 1aefa5178b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
256 changed files with 3110 additions and 3200 deletions

View file

@ -64,9 +64,9 @@ impl ChannelView {
window,
cx,
);
window.spawn(cx, |mut cx| async move {
window.spawn(cx, async move |cx| {
let channel_view = channel_view.await?;
pane.update_in(&mut cx, |pane, window, cx| {
pane.update_in(cx, |pane, window, cx| {
telemetry::event!(
"Channel Notes Opened",
channel_id,
@ -90,10 +90,10 @@ impl ChannelView {
cx: &mut App,
) -> Task<Result<Entity<Self>>> {
let channel_view = Self::load(channel_id, workspace, window, cx);
window.spawn(cx, |mut cx| async move {
window.spawn(cx, async move |cx| {
let channel_view = channel_view.await?;
pane.update_in(&mut cx, |pane, window, cx| {
pane.update_in(cx, |pane, window, cx| {
let buffer_id = channel_view.read(cx).channel_buffer.read(cx).remote_id(cx);
let existing_view = pane
@ -166,11 +166,11 @@ impl ChannelView {
let channel_buffer =
channel_store.update(cx, |store, cx| store.open_channel_buffer(channel_id, cx));
window.spawn(cx, |mut cx| async move {
window.spawn(cx, async move |cx| {
let channel_buffer = channel_buffer.await?;
let markdown = markdown.await.log_err();
channel_buffer.update(&mut cx, |channel_buffer, cx| {
channel_buffer.update(cx, |channel_buffer, cx| {
channel_buffer.buffer().update(cx, |buffer, cx| {
buffer.set_language_registry(language_registry);
let Some(markdown) = markdown else {
@ -583,10 +583,10 @@ impl FollowableItem for ChannelView {
let open = ChannelView::load(ChannelId(state.channel_id), workspace, window, cx);
Some(window.spawn(cx, |mut cx| async move {
Some(window.spawn(cx, async move |cx| {
let this = open.await?;
let task = this.update_in(&mut cx, |this, window, cx| {
let task = this.update_in(cx, |this, window, cx| {
this.remote_id = Some(remote_id);
if let Some(state) = state.editor {

View file

@ -199,7 +199,7 @@ impl ChatPanel {
workspace: WeakEntity<Workspace>,
cx: AsyncWindowContext,
) -> Task<Result<Entity<Self>>> {
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let serialized_panel = if let Some(panel) = cx
.background_spawn(async move { KEY_VALUE_STORE.read_kvp(CHAT_PANEL_KEY) })
.await
@ -211,7 +211,7 @@ impl ChatPanel {
None
};
workspace.update_in(&mut cx, |workspace, window, cx| {
workspace.update_in(cx, |workspace, window, cx| {
let panel = Self::new(workspace, window, cx);
if let Some(serialized_panel) = serialized_panel {
panel.update(cx, |panel, cx| {
@ -867,10 +867,10 @@ impl ChatPanel {
})
});
cx.spawn(|this, mut cx| async move {
cx.spawn(async move |this, cx| {
let chat = open_chat.await?;
let highlight_message_id = scroll_to_message_id;
let scroll_to_message_id = this.update(&mut cx, |this, cx| {
let scroll_to_message_id = this.update(cx, |this, cx| {
this.set_active_chat(chat.clone(), cx);
scroll_to_message_id.or(this.last_acknowledged_message_id)
@ -881,11 +881,11 @@ impl ChatPanel {
ChannelChat::load_history_since_message(chat.clone(), message_id, cx.clone())
.await
{
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
if let Some(highlight_message_id) = highlight_message_id {
let task = cx.spawn(|this, mut cx| async move {
let task = cx.spawn(async move |this, cx| {
cx.background_executor().timer(Duration::from_secs(2)).await;
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
this.highlighted_message.take();
cx.notify();
})

View file

@ -137,11 +137,9 @@ impl MessageEditor {
.detach();
let markdown = language_registry.language_for_name("Markdown");
cx.spawn_in(window, |_, mut cx| async move {
cx.spawn_in(window, async move |_, cx| {
let markdown = markdown.await.context("failed to load Markdown language")?;
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(Some(markdown), cx)
})
buffer.update(cx, |buffer, cx| buffer.set_language(Some(markdown), cx))
})
.detach_and_log_err(cx);
@ -232,7 +230,7 @@ impl MessageEditor {
) {
if let language::BufferEvent::Reparsed | language::BufferEvent::Edited = event {
let buffer = buffer.read(cx).snapshot();
self.mentions_task = Some(cx.spawn_in(window, |this, cx| async move {
self.mentions_task = Some(cx.spawn_in(window, async move |this, cx| {
cx.background_executor()
.timer(MENTIONS_DEBOUNCE_INTERVAL)
.await;
@ -251,7 +249,7 @@ impl MessageEditor {
self.collect_mention_candidates(buffer, end_anchor, cx)
{
if !candidates.is_empty() {
return cx.spawn(|_, cx| async move {
return cx.spawn(async move |_, cx| {
Ok(Some(
Self::resolve_completions_for_candidates(
&cx,
@ -270,7 +268,7 @@ impl MessageEditor {
self.collect_emoji_candidates(buffer, end_anchor, cx)
{
if !candidates.is_empty() {
return cx.spawn(|_, cx| async move {
return cx.spawn(async move |_, cx| {
Ok(Some(
Self::resolve_completions_for_candidates(
&cx,
@ -453,7 +451,7 @@ impl MessageEditor {
async fn find_mentions(
this: WeakEntity<MessageEditor>,
buffer: BufferSnapshot,
mut cx: AsyncWindowContext,
cx: &mut AsyncWindowContext,
) {
let (buffer, ranges) = cx
.background_spawn(async move {
@ -462,7 +460,7 @@ impl MessageEditor {
})
.await;
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
let mut anchor_ranges = Vec::new();
let mut mentioned_user_ids = Vec::new();
let mut text = String::new();

View file

@ -1569,9 +1569,9 @@ impl CollabPanel {
channel_store.create_channel(&channel_name, *location, cx)
});
if location.is_none() {
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
let channel_id = create.await?;
this.update_in(&mut cx, |this, window, cx| {
this.update_in(cx, |this, window, cx| {
this.show_channel_modal(
channel_id,
channel_modal::Mode::InviteMembers,
@ -1944,8 +1944,8 @@ impl CollabPanel {
let user_store = self.user_store.clone();
let channel_store = self.channel_store.clone();
cx.spawn_in(window, |_, mut cx| async move {
workspace.update_in(&mut cx, |workspace, window, cx| {
cx.spawn_in(window, async move |_, cx| {
workspace.update_in(cx, |workspace, window, cx| {
workspace.toggle_modal(window, cx, |window, cx| {
ChannelModal::new(
user_store.clone(),
@ -1976,11 +1976,11 @@ impl CollabPanel {
&["Leave", "Cancel"],
cx,
);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
if answer.await? != 0 {
return Ok(());
}
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
this.channel_store.update(cx, |channel_store, cx| {
channel_store.remove_member(channel_id, user_id, cx)
})
@ -2009,13 +2009,13 @@ impl CollabPanel {
&["Remove", "Cancel"],
cx,
);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
if answer.await? == 0 {
channel_store
.update(&mut cx, |channels, _| channels.remove_channel(channel_id))?
.update(cx, |channels, _| channels.remove_channel(channel_id))?
.await
.notify_async_err(&mut cx);
this.update_in(&mut cx, |_, window, cx| cx.focus_self(window))
.notify_async_err(cx);
this.update_in(cx, |_, window, cx| cx.focus_self(window))
.ok();
}
anyhow::Ok(())
@ -2043,12 +2043,12 @@ impl CollabPanel {
&["Remove", "Cancel"],
cx,
);
cx.spawn_in(window, |_, mut cx| async move {
cx.spawn_in(window, async move |_, cx| {
if answer.await? == 0 {
user_store
.update(&mut cx, |store, cx| store.remove_contact(user_id, cx))?
.update(cx, |store, cx| store.remove_contact(user_id, cx))?
.await
.notify_async_err(&mut cx);
.notify_async_err(cx);
}
anyhow::Ok(())
})
@ -2161,11 +2161,11 @@ impl CollabPanel {
.full_width()
.on_click(cx.listener(|this, _, window, cx| {
let client = this.client.clone();
cx.spawn_in(window, |_, mut cx| async move {
cx.spawn_in(window, async move |_, cx| {
client
.authenticate_and_connect(true, &cx)
.await
.notify_async_err(&mut cx);
.notify_async_err(cx);
})
.detach()
})),

View file

@ -300,9 +300,9 @@ impl PickerDelegate for ChannelModalDelegate {
cx.background_executor().clone(),
));
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
picker
.update(&mut cx, |picker, cx| {
.update(cx, |picker, cx| {
let delegate = &mut picker.delegate;
delegate.matching_member_indices.clear();
delegate
@ -316,10 +316,10 @@ impl PickerDelegate for ChannelModalDelegate {
let search_members = self.channel_store.update(cx, |store, cx| {
store.fuzzy_search_members(self.channel_id, query.clone(), 100, cx)
});
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
async {
let members = search_members.await?;
picker.update(&mut cx, |picker, cx| {
picker.update(cx, |picker, cx| {
picker.delegate.has_all_members =
query.is_empty() && members.len() < 100;
picker.delegate.matching_member_indices =
@ -338,10 +338,10 @@ impl PickerDelegate for ChannelModalDelegate {
let search_users = self
.user_store
.update(cx, |store, cx| store.fuzzy_search_users(query, cx));
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
async {
let users = search_users.await?;
picker.update(&mut cx, |picker, cx| {
picker.update(cx, |picker, cx| {
picker.delegate.matching_users = users;
cx.notify();
})?;
@ -489,9 +489,9 @@ impl ChannelModalDelegate {
let update = self.channel_store.update(cx, |store, cx| {
store.set_member_role(self.channel_id, user_id, new_role, cx)
});
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
update.await?;
picker.update_in(&mut cx, |picker, window, cx| {
picker.update_in(cx, |picker, window, cx| {
let this = &mut picker.delegate;
if let Some(member) = this.members.iter_mut().find(|m| m.user.id == user_id) {
member.role = new_role;
@ -513,9 +513,9 @@ impl ChannelModalDelegate {
let update = self.channel_store.update(cx, |store, cx| {
store.remove_member(self.channel_id, user_id, cx)
});
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
update.await?;
picker.update_in(&mut cx, |picker, window, cx| {
picker.update_in(cx, |picker, window, cx| {
let this = &mut picker.delegate;
if let Some(ix) = this.members.iter_mut().position(|m| m.user.id == user_id) {
this.members.remove(ix);
@ -551,10 +551,10 @@ impl ChannelModalDelegate {
store.invite_member(self.channel_id, user.id, ChannelRole::Member, cx)
});
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
invite_member.await?;
this.update(&mut cx, |this, cx| {
this.update(cx, |this, cx| {
let new_member = ChannelMembership {
user,
kind: proto::channel_member::Kind::Invitee,

View file

@ -102,10 +102,10 @@ impl PickerDelegate for ContactFinderDelegate {
.user_store
.update(cx, |store, cx| store.fuzzy_search_users(query, cx));
cx.spawn_in(window, |picker, mut cx| async move {
cx.spawn_in(window, async move |picker, cx| {
async {
let potential_contacts = search_users.await?;
picker.update(&mut cx, |picker, cx| {
picker.update(cx, |picker, cx| {
picker.delegate.potential_contacts = potential_contacts.into();
cx.notify();
})?;

View file

@ -96,10 +96,10 @@ impl NotificationPanel {
cx.new(|cx| {
let mut status = client.status();
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
while (status.next().await).is_some() {
if this
.update(&mut cx, |_: &mut Self, cx| {
.update(cx, |_: &mut Self, cx| {
cx.notify();
})
.is_err()
@ -181,7 +181,7 @@ impl NotificationPanel {
workspace: WeakEntity<Workspace>,
cx: AsyncWindowContext,
) -> Task<Result<Entity<Self>>> {
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let serialized_panel = if let Some(panel) = cx
.background_spawn(async move { KEY_VALUE_STORE.read_kvp(NOTIFICATION_PANEL_KEY) })
.await
@ -193,7 +193,7 @@ impl NotificationPanel {
None
};
workspace.update_in(&mut cx, |workspace, window, cx| {
workspace.update_in(cx, |workspace, window, cx| {
let panel = Self::new(workspace, window, cx);
if let Some(serialized_panel) = serialized_panel {
panel.update(cx, |panel, cx| {
@ -445,12 +445,12 @@ impl NotificationPanel {
.entry(notification_id)
.or_insert_with(|| {
let client = self.client.clone();
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
cx.background_executor().timer(MARK_AS_READ_DELAY).await;
client
.request(proto::MarkNotificationRead { notification_id })
.await?;
this.update(&mut cx, |this, _| {
this.update(cx, |this, _| {
this.mark_as_read_tasks.remove(&notification_id);
})?;
Ok(())
@ -556,9 +556,9 @@ impl NotificationPanel {
let notification_id = entry.id;
self.current_notification_toast = Some((
notification_id,
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
cx.background_executor().timer(TOAST_DURATION).await;
this.update(&mut cx, |this, cx| this.remove_toast(notification_id, cx))
this.update(cx, |this, cx| this.remove_toast(notification_id, cx))
.ok();
}),
));
@ -643,7 +643,7 @@ impl Render for NotificationPanel {
move |_, window, cx| {
let client = client.clone();
window
.spawn(cx, move |cx| async move {
.spawn(cx, async move |cx| {
client
.authenticate_and_connect(true, &cx)
.log_err()

View file

@ -12,12 +12,12 @@ use workspace::AppState;
pub fn init(app_state: &Arc<AppState>, cx: &mut App) {
let app_state = Arc::downgrade(app_state);
let mut incoming_call = ActiveCall::global(cx).read(cx).incoming();
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let mut notification_windows: Vec<WindowHandle<IncomingCallNotification>> = Vec::new();
while let Some(incoming_call) = incoming_call.next().await {
for window in notification_windows.drain(..) {
window
.update(&mut cx, |_, window, _| {
.update(cx, |_, window, _| {
window.remove_window();
})
.log_err();
@ -75,7 +75,7 @@ impl IncomingCallNotificationState {
let initial_project_id = self.call.initial_project.as_ref().map(|project| project.id);
let app_state = self.app_state.clone();
let cx: &mut App = cx;
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
join.await?;
if let Some(project_id) = initial_project_id {
cx.update(|cx| {