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

@ -109,7 +109,7 @@ fn files_not_created_on_launch(errors: HashMap<io::ErrorKind, Vec<&Path>>) {
cx,
);
cx.spawn_in(window, |_, mut cx| async move {
cx.spawn_in(window, async move |_, cx| {
response.await?;
cx.update(|_, cx| cx.quit())
})
@ -138,7 +138,7 @@ fn fail_to_open_window(e: anyhow::Error, _cx: &mut App) {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
{
use ashpd::desktop::notification::{Notification, NotificationProxy, Priority};
_cx.spawn(|_cx| async move {
_cx.spawn(async move |_cx| {
let Ok(proxy) = NotificationProxy::new().await else {
process::exit(1);
};
@ -285,7 +285,7 @@ fn main() {
{
cx.spawn({
let app_state = app_state.clone();
|mut cx| async move {
async move |mut cx| {
if let Err(e) = restore_or_create_workspace(app_state, &mut cx).await {
fail_to_open_window_async(e, &mut cx)
}
@ -580,7 +580,7 @@ fn main() {
cx.spawn({
let client = app_state.client.clone();
|cx| async move { authenticate(client, &cx).await }
async move |cx| authenticate(client, &cx).await
})
.detach_and_log_err(cx);
@ -606,7 +606,7 @@ fn main() {
None => {
cx.spawn({
let app_state = app_state.clone();
|mut cx| async move {
async move |mut cx| {
if let Err(e) = restore_or_create_workspace(app_state, &mut cx).await {
fail_to_open_window_async(e, &mut cx)
}
@ -620,7 +620,7 @@ fn main() {
component_preview::init(app_state.clone(), cx);
cx.spawn(move |cx| async move {
cx.spawn(async move |cx| {
while let Some(urls) = open_rx.next().await {
cx.update(|cx| {
if let Some(request) = OpenRequest::parse(urls, cx).log_err() {
@ -637,7 +637,7 @@ fn main() {
fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut App) {
if let Some(connection) = request.cli_connection {
let app_state = app_state.clone();
cx.spawn(move |cx| handle_cli_connection(connection, app_state, cx))
cx.spawn(async move |cx| handle_cli_connection(connection, app_state, cx).await)
.detach();
return;
}
@ -648,7 +648,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
}
if let Some(connection_options) = request.ssh_connection {
cx.spawn(|mut cx| async move {
cx.spawn(async move |mut cx| {
let paths_with_position =
derive_paths_with_position(app_state.fs.as_ref(), request.open_paths).await;
open_ssh_project(
@ -667,7 +667,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
let mut task = None;
if !request.open_paths.is_empty() {
let app_state = app_state.clone();
task = Some(cx.spawn(|mut cx| async move {
task = Some(cx.spawn(async move |mut cx| {
let paths_with_position =
derive_paths_with_position(app_state.fs.as_ref(), request.open_paths).await;
let (_window, results) = open_paths_with_positions(
@ -687,7 +687,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
}
if !request.open_channel_notes.is_empty() || request.join_channel.is_some() {
cx.spawn(|mut cx| async move {
cx.spawn(async move |mut cx| {
let result = maybe!(async {
if let Some(task) = task {
task.await?;
@ -711,7 +711,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
let workspace_window =
workspace::get_any_active_workspace(app_state, cx.clone()).await?;
let workspace = workspace_window.entity(&cx)?;
let workspace = workspace_window.entity(cx)?;
let mut promises = Vec::new();
for (channel_id, heading) in request.open_channel_notes {
@ -736,7 +736,7 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
})
.detach()
} else if let Some(task) = task {
cx.spawn(|mut cx| async move {
cx.spawn(async move |mut cx| {
if let Err(err) = task.await {
fail_to_open_window_async(err, &mut cx);
}
@ -821,13 +821,13 @@ async fn restore_or_create_workspace(app_state: Arc<AppState>, cx: &mut AsyncApp
.connection_options_for(ssh.host, ssh.port, ssh.user)
})?;
let app_state = app_state.clone();
cx.spawn(move |mut cx| async move {
cx.spawn(async move |cx| {
recent_projects::open_ssh_project(
connection_options,
ssh.paths.into_iter().map(PathBuf::from).collect(),
app_state,
workspace::OpenOptions::default(),
&mut cx,
cx,
)
.await
.log_err();
@ -1040,7 +1040,7 @@ fn eager_load_active_theme_and_icon_theme(fs: Arc<dyn Fs>, cx: &App) {
cx.spawn({
let theme_registry = theme_registry.clone();
let fs = fs.clone();
|cx| async move {
async move |cx| {
theme_registry.load_user_theme(&theme_path, fs).await?;
cx.update(|cx| {
@ -1066,7 +1066,7 @@ fn eager_load_active_theme_and_icon_theme(fs: Arc<dyn Fs>, cx: &App) {
cx.spawn({
let theme_registry = theme_registry.clone();
let fs = fs.clone();
|cx| async move {
async move |cx| {
theme_registry
.load_icon_theme(&icon_theme_path, &icons_root_path, fs)
.await?;
@ -1086,7 +1086,7 @@ fn eager_load_active_theme_and_icon_theme(fs: Arc<dyn Fs>, cx: &App) {
fn load_user_themes_in_background(fs: Arc<dyn fs::Fs>, cx: &mut App) {
cx.spawn({
let fs = fs.clone();
|cx| async move {
async move |cx| {
if let Some(theme_registry) =
cx.update(|cx| ThemeRegistry::global(cx).clone()).log_err()
{
@ -1119,7 +1119,7 @@ fn load_user_themes_in_background(fs: Arc<dyn fs::Fs>, cx: &mut App) {
/// Spawns a background task to watch the themes directory for changes.
fn watch_themes(fs: Arc<dyn fs::Fs>, cx: &mut App) {
use std::time::Duration;
cx.spawn(|cx| async move {
cx.spawn(async move |cx| {
let (mut events, _) = fs
.watch(paths::themes_dir(), Duration::from_millis(100))
.await;
@ -1157,7 +1157,7 @@ fn watch_languages(fs: Arc<dyn fs::Fs>, languages: Arc<LanguageRegistry>, cx: &m
full_path
};
cx.spawn(|_| async move {
cx.spawn(async move |_| {
let (mut events, _) = fs.watch(path.as_path(), Duration::from_millis(100)).await;
while let Some(event) = events.next().await {
let has_language_file = event.iter().any(|event| {

View file

@ -26,10 +26,10 @@ use futures::{channel::mpsc, select_biased, StreamExt};
use git_ui::git_panel::GitPanel;
use git_ui::project_diff::ProjectDiffToolbar;
use gpui::{
actions, point, px, Action, App, AppContext as _, AsyncApp, Context, DismissEvent, Element,
Entity, Focusable, KeyBinding, MenuItem, ParentElement, PathPromptOptions, PromptLevel,
ReadGlobal, SharedString, Styled, Task, TitlebarOptions, UpdateGlobal, Window, WindowKind,
WindowOptions,
actions, point, px, Action, App, AppContext as _, AsyncApp, AsyncWindowContext, Context,
DismissEvent, Element, Entity, Focusable, KeyBinding, MenuItem, ParentElement,
PathPromptOptions, PromptLevel, ReadGlobal, SharedString, Styled, Task, TitlebarOptions,
UpdateGlobal, Window, WindowKind, WindowOptions,
};
use image_viewer::ImageInfo;
use migrate::{MigrationBanner, MigrationEvent, MigrationNotification, MigrationType};
@ -307,7 +307,7 @@ fn initialize_file_watcher(window: &mut Window, cx: &mut Context<Workspace>) {
&["Troubleshoot and Quit"],
cx,
);
cx.spawn(|_, cx| async move {
cx.spawn(async move |_, cx| {
if prompt.await == Ok(0) {
cx.update(|cx| {
cx.open_url("https://zed.dev/docs/linux#could-not-start-inotify");
@ -338,7 +338,7 @@ fn initialize_file_watcher(window: &mut Window, cx: &mut Context<Workspace>) {
&["Troubleshoot and Quit"],
cx,
);
cx.spawn(|_, cx| async move {
cx.spawn(async move |_, cx| {
if prompt.await == Ok(0) {
cx.update(|cx| {
cx.open_url("https://zed.dev/docs/windows");
@ -376,7 +376,7 @@ fn show_software_emulation_warning_if_needed(
&["Skip", "Troubleshoot and Quit"],
cx,
);
cx.spawn(|_, cx| async move {
cx.spawn(async move |_, cx| {
if prompt.await == Ok(1) {
cx.update(|cx| {
cx.open_url("https://zed.dev/docs/linux#zed-fails-to-open-windows");
@ -399,7 +399,7 @@ fn initialize_panels(
let prompt_builder = prompt_builder.clone();
cx.spawn_in(window, |workspace_handle, mut cx| async move {
cx.spawn_in(window, async move |workspace_handle, cx| {
let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone());
let outline_panel = OutlinePanel::load(workspace_handle.clone(), cx.clone());
let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone());
@ -428,7 +428,7 @@ fn initialize_panels(
notification_panel,
)?;
workspace_handle.update_in(&mut cx, |workspace, window, cx| {
workspace_handle.update_in(cx, |workspace, window, cx| {
workspace.add_panel(project_panel, window, cx);
workspace.add_panel(outline_panel, window, cx);
workspace.add_panel(terminal_panel, window, cx);
@ -436,13 +436,17 @@ fn initialize_panels(
workspace.add_panel(chat_panel, window, cx);
workspace.add_panel(notification_panel, window, cx);
cx.when_flag_enabled::<Debugger>(window, |_, window, cx| {
cx.spawn_in(window, |workspace, mut cx| async move {
let debug_panel = DebugPanel::load(workspace.clone(), cx.clone()).await?;
workspace.update_in(&mut cx, |workspace, window, cx| {
workspace.add_panel(debug_panel, window, cx);
})?;
Result::<_, anyhow::Error>::Ok(())
})
cx.spawn_in(
window,
async move |workspace: gpui::WeakEntity<Workspace>,
cx: &mut AsyncWindowContext| {
let debug_panel = DebugPanel::load(workspace.clone(), cx.clone()).await?;
workspace.update_in(cx, |workspace, window, cx| {
workspace.add_panel(debug_panel, window, cx);
})?;
Result::<_, anyhow::Error>::Ok(())
},
)
.detach()
});
@ -479,7 +483,7 @@ fn initialize_panels(
(Some(assistant_panel), None)
};
workspace_handle.update_in(&mut cx, |workspace, window, cx| {
workspace_handle.update_in(cx, |workspace, window, cx| {
if let Some(assistant2_panel) = assistant2_panel {
workspace.add_panel(assistant2_panel, window, cx);
}
@ -554,13 +558,13 @@ fn register_actions(
cx,
);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
let Some(paths) = paths.await.log_err().flatten() else {
return;
};
if let Some(task) = this
.update_in(&mut cx, |this, window, cx| {
.update_in(cx, |this, window, cx| {
if this.project().read(cx).is_local() {
this.open_workspace_for_paths(false, paths, window, cx)
} else {
@ -670,9 +674,9 @@ fn register_actions(
})
.register_action(install_cli)
.register_action(|_, _: &install_cli::RegisterZedScheme, window, cx| {
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
register_zed_scheme(&cx).await?;
workspace.update_in(&mut cx, |workspace, _, cx| {
workspace.update_in(cx, |workspace, _, cx| {
struct RegisterZedScheme;
workspace.show_toast(
@ -870,11 +874,11 @@ fn register_actions(
.project()
.update(cx, |project, cx| project.open_server_settings(cx));
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
let buffer = open_server_settings.await?;
workspace
.update_in(&mut cx, |workspace, window, cx| {
.update_in(cx, |workspace, window, cx| {
workspace.open_path(
buffer
.read(cx)
@ -975,7 +979,7 @@ fn install_cli(
) {
const LINUX_PROMPT_DETAIL: &str = "If you installed Zed from our official release add ~/.local/bin to your PATH.\n\nIf you installed Zed from a different source like your package manager, then you may need to create an alias/symlink manually.\n\nDepending on your package manager, the CLI might be named zeditor, zedit, zed-editor or something else.";
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
if cfg!(any(target_os = "linux", target_os = "freebsd")) {
let prompt = cx.prompt(
PromptLevel::Warning,
@ -990,7 +994,7 @@ fn install_cli(
.await
.context("error creating CLI symlink")?;
workspace.update_in(&mut cx, |workspace, _, cx| {
workspace.update_in(cx, |workspace, _, cx| {
struct InstalledZedCli;
workspace.show_toast(
@ -1018,7 +1022,7 @@ fn quit(_: &Quit, cx: &mut App) {
}
let should_confirm = WorkspaceSettings::get_global(cx).confirm_quit;
cx.spawn(|mut cx| async move {
cx.spawn(async move |cx| {
let mut workspace_windows = cx.update(|cx| {
cx.windows()
.into_iter()
@ -1036,7 +1040,7 @@ fn quit(_: &Quit, cx: &mut App) {
if should_confirm {
if let Some(workspace) = workspace_windows.first() {
let answer = workspace
.update(&mut cx, |_, window, cx| {
.update(cx, |_, window, cx| {
window.prompt(
PromptLevel::Info,
"Are you sure you want to quit?",
@ -1061,7 +1065,7 @@ fn quit(_: &Quit, cx: &mut App) {
// If the user cancels any save prompt, then keep the app open.
for window in workspace_windows {
if let Some(should_close) = window
.update(&mut cx, |workspace, window, cx| {
.update(cx, |workspace, window, cx| {
workspace.prepare_to_close(CloseIntent::Quit, window, cx)
})
.log_err()
@ -1082,7 +1086,7 @@ fn open_log_file(workspace: &mut Workspace, window: &mut Window, cx: &mut Contex
workspace
.with_local_workspace(window, cx, move |workspace, window, cx| {
let fs = workspace.app_state().fs.clone();
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
let (old_log, new_log) =
futures::join!(fs.load(paths::old_log_file()), fs.load(paths::log_file()));
let log = match (old_log, new_log) {
@ -1109,7 +1113,7 @@ fn open_log_file(workspace: &mut Workspace, window: &mut Window, cx: &mut Contex
};
workspace
.update_in(&mut cx, |workspace, window, cx| {
.update_in(cx, |workspace, window, cx| {
let Some(log) = log else {
struct OpenLogError;
@ -1189,7 +1193,7 @@ pub fn handle_settings_file_changes(
}
settings_changed(result.err(), cx);
});
cx.spawn(move |cx| async move {
cx.spawn(async move |cx| {
while let Some(content) = user_settings_file_rx.next().await {
let user_settings_content;
let content_migrated;
@ -1267,7 +1271,7 @@ pub fn handle_keymap_file_changes(
struct KeymapParseErrorNotification;
let notification_id = NotificationId::unique::<KeymapParseErrorNotification>();
cx.spawn(move |cx| async move {
cx.spawn(async move |cx| {
let mut user_keymap_content = String::new();
let mut content_migrated = false;
loop {
@ -1377,7 +1381,7 @@ fn show_markdown_app_notification<F>(
.await
});
cx.spawn(move |cx| async move {
cx.spawn(async move |cx| {
let parsed_markdown = Arc::new(parsed_markdown.await);
let primary_button_message = primary_button_message.clone();
let primary_button_on_click = Arc::new(primary_button_on_click);
@ -1475,7 +1479,7 @@ pub fn open_new_ssh_project_from_project(
return Task::ready(Err(anyhow::anyhow!("Not an ssh project")));
};
let connection_options = ssh_client.read(cx).connection_options();
cx.spawn_in(window, |_, mut cx| async move {
cx.spawn_in(window, async move |_, cx| {
open_ssh_project(
connection_options,
paths,
@ -1484,7 +1488,7 @@ pub fn open_new_ssh_project_from_project(
open_new_workspace: Some(true),
..Default::default()
},
&mut cx,
cx,
)
.await
})
@ -1549,11 +1553,11 @@ fn open_local_file(
.find_map(|tree| tree.read(cx).root_entry()?.is_dir().then_some(tree));
if let Some(worktree) = worktree {
let tree_id = worktree.read(cx).id();
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
if let Some(dir_path) = settings_relative_path.parent() {
if worktree.update(&mut cx, |tree, _| tree.entry_for_path(dir_path).is_none())? {
if worktree.update(cx, |tree, _| tree.entry_for_path(dir_path).is_none())? {
project
.update(&mut cx, |project, cx| {
.update(cx, |project, cx| {
project.create_entry((tree_id, dir_path), true, cx)
})?
.await
@ -1561,11 +1565,11 @@ fn open_local_file(
}
}
if worktree.update(&mut cx, |tree, _| {
if worktree.update(cx, |tree, _| {
tree.entry_for_path(settings_relative_path).is_none()
})? {
project
.update(&mut cx, |project, cx| {
.update(cx, |project, cx| {
project.create_entry((tree_id, settings_relative_path), false, cx)
})?
.await
@ -1573,7 +1577,7 @@ fn open_local_file(
}
let editor = workspace
.update_in(&mut cx, |workspace, window, cx| {
.update_in(cx, |workspace, window, cx| {
workspace.open_path((tree_id, settings_relative_path), None, true, window, cx)
})?
.await?
@ -1582,7 +1586,7 @@ fn open_local_file(
editor
.downgrade()
.update(&mut cx, |editor, cx| {
.update(cx, |editor, cx| {
if let Some(buffer) = editor.buffer().read(cx).as_singleton() {
if buffer.read(cx).is_empty() {
buffer.update(cx, |buffer, cx| {
@ -1612,7 +1616,7 @@ fn open_telemetry_log_file(
) {
workspace.with_local_workspace(window, cx, move |workspace, window, cx| {
let app_state = workspace.app_state().clone();
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
async fn fetch_log_string(app_state: &Arc<AppState>) -> Option<String> {
let path = client::telemetry::Telemetry::log_file_path();
app_state.fs.load(&path).await.log_err()
@ -1634,7 +1638,7 @@ fn open_telemetry_log_file(
let content = format!("{}\n{}", header, log_suffix);
let json = app_state.languages.language_for_name("JSON").await.log_err();
workspace.update_in(&mut cx, |workspace, window, cx| {
workspace.update_in( cx, |workspace, window, cx| {
let project = workspace.project().clone();
let buffer = project.update(cx, |project, cx| project.create_local_buffer(&content, json, cx));
let buffer = cx.new(|cx| {
@ -1668,10 +1672,10 @@ fn open_bundled_file(
cx: &mut Context<Workspace>,
) {
let language = workspace.app_state().languages.language_for_name(language);
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
let language = language.await.log_err();
workspace
.update_in(&mut cx, |workspace, window, cx| {
.update_in(cx, |workspace, window, cx| {
workspace.with_local_workspace(window, cx, |workspace, window, cx| {
let project = workspace.project();
let buffer = project.update(cx, move |project, cx| {
@ -1705,9 +1709,9 @@ fn open_settings_file(
window: &mut Window,
cx: &mut Context<Workspace>,
) {
cx.spawn_in(window, |workspace, mut cx| async move {
cx.spawn_in(window, async move |workspace, cx| {
let (worktree_creation_task, settings_open_task) = workspace
.update_in(&mut cx, |workspace, window, cx| {
.update_in(cx, |workspace, window, cx| {
workspace.with_local_workspace(window, cx, move |workspace, window, cx| {
let worktree_creation_task = workspace.project().update(cx, |project, cx| {
// Set up a dedicated worktree for settings, since

View file

@ -121,9 +121,9 @@ impl ToolbarItemView for MigrationBanner {
self.migration_type = Some(MigrationType::Keymap);
let fs = <dyn Fs>::global(cx);
let should_migrate = should_migrate_keymap(fs);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
if let Ok(true) = should_migrate.await {
this.update(&mut cx, |_, cx| {
this.update(cx, |_, cx| {
cx.emit(ToolbarItemEvent::ChangeLocation(
ToolbarItemLocation::Secondary,
));
@ -137,9 +137,9 @@ impl ToolbarItemView for MigrationBanner {
self.migration_type = Some(MigrationType::Settings);
let fs = <dyn Fs>::global(cx);
let should_migrate = should_migrate_settings(fs);
cx.spawn_in(window, |this, mut cx| async move {
cx.spawn_in(window, async move |this, cx| {
if let Ok(true) = should_migrate.await {
this.update(&mut cx, |_, cx| {
this.update(cx, |_, cx| {
cx.emit(ToolbarItemEvent::ChangeLocation(
ToolbarItemLocation::Secondary,
));
@ -213,16 +213,12 @@ impl Render for MigrationBanner {
let fs = <dyn Fs>::global(cx);
match migration_type {
Some(MigrationType::Keymap) => {
cx.spawn(
move |_| async move { write_keymap_migration(&fs).await.ok() },
)
.detach();
cx.spawn(async move |_| write_keymap_migration(&fs).await.ok())
.detach();
}
Some(MigrationType::Settings) => {
cx.spawn(
move |_| async move { write_settings_migration(&fs).await.ok() },
)
.detach();
cx.spawn(async move |_| write_settings_migration(&fs).await.ok())
.detach();
}
None => unreachable!(),
}

View file

@ -251,7 +251,7 @@ pub async fn open_paths_with_positions(
pub async fn handle_cli_connection(
(mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
app_state: Arc<AppState>,
mut cx: AsyncApp,
cx: &mut AsyncApp,
) {
if let Some(request) = requests.next().await {
match request {
@ -290,7 +290,7 @@ pub async fn handle_cli_connection(
wait,
app_state.clone(),
env,
&mut cx,
cx,
)
.await;
@ -379,7 +379,7 @@ async fn open_workspaces(
.connection_options_for(ssh.host, ssh.port, ssh.user)
});
if let Ok(connection_options) = connection_options {
cx.spawn(|mut cx| async move {
cx.spawn(async move |mut cx| {
open_ssh_project(
connection_options,
ssh.paths.into_iter().map(PathBuf::from).collect(),