Fix warnings/errors now that AsyncAppContext::update returns Result

This commit is contained in:
Antonio Scandurra 2023-04-18 14:58:57 +02:00
parent 31e6bb4fc1
commit d03c431f9a
33 changed files with 494 additions and 371 deletions

View file

@ -606,71 +606,87 @@ async fn handle_cli_connection(
} else {
paths
};
let (workspace, items) = cx
.update(|cx| workspace::open_paths(&paths, &app_state, cx))
.await;
let mut errored = false;
let mut item_release_futures = Vec::new();
cx.update(|cx| {
for (item, path) in items.into_iter().zip(&paths) {
match item {
Some(Ok(item)) => {
let released = oneshot::channel();
item.on_release(
cx,
Box::new(move |_| {
let _ = released.0.send(());
}),
)
.detach();
item_release_futures.push(released.1);
match cx
.update(|cx| workspace::open_paths(&paths, &app_state, cx))
.await
{
Ok((workspace, items)) => {
let mut item_release_futures = Vec::new();
cx.update(|cx| {
for (item, path) in items.into_iter().zip(&paths) {
match item {
Some(Ok(item)) => {
let released = oneshot::channel();
item.on_release(
cx,
Box::new(move |_| {
let _ = released.0.send(());
}),
)
.detach();
item_release_futures.push(released.1);
}
Some(Err(err)) => {
responses
.send(CliResponse::Stderr {
message: format!(
"error opening {:?}: {}",
path, err
),
})
.log_err();
errored = true;
}
None => {}
}
}
Some(Err(err)) => {
responses
.send(CliResponse::Stderr {
message: format!("error opening {:?}: {}", path, err),
})
.log_err();
errored = true;
});
if wait {
let background = cx.background();
let wait = async move {
if paths.is_empty() {
let (done_tx, done_rx) = oneshot::channel();
let _subscription = cx.update(|cx| {
cx.observe_release(&workspace, move |_, _| {
let _ = done_tx.send(());
})
});
drop(workspace);
let _ = done_rx.await;
} else {
let _ =
futures::future::try_join_all(item_release_futures).await;
};
}
None => {}
}
}
});
.fuse();
futures::pin_mut!(wait);
if wait {
let background = cx.background();
let wait = async move {
if paths.is_empty() {
let (done_tx, done_rx) = oneshot::channel();
let _subscription = cx.update(|cx| {
cx.observe_release(&workspace, move |_, _| {
let _ = done_tx.send(());
})
});
drop(workspace);
let _ = done_rx.await;
} else {
let _ = futures::future::try_join_all(item_release_futures).await;
};
}
.fuse();
futures::pin_mut!(wait);
loop {
// Repeatedly check if CLI is still open to avoid wasting resources
// waiting for files or workspaces to close.
let mut timer = background.timer(Duration::from_secs(1)).fuse();
futures::select_biased! {
_ = wait => break,
_ = timer => {
if responses.send(CliResponse::Ping).is_err() {
break;
loop {
// Repeatedly check if CLI is still open to avoid wasting resources
// waiting for files or workspaces to close.
let mut timer = background.timer(Duration::from_secs(1)).fuse();
futures::select_biased! {
_ = wait => break,
_ = timer => {
if responses.send(CliResponse::Ping).is_err() {
break;
}
}
}
}
}
}
Err(error) => {
errored = true;
responses
.send(CliResponse::Stderr {
message: format!("error opening {:?}: {}", paths, error),
})
.log_err();
}
}
responses

View file

@ -247,10 +247,10 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::AppContext) {
cx,
);
})
})
.await;
})?
.await
})
.detach();
.detach_and_log_err(cx);
}
});
cx.add_action(
@ -399,7 +399,7 @@ fn restart(_: &Restart, cx: &mut gpui::AppContext) {
if !workspace
.update(&mut cx, |workspace, cx| {
workspace.prepare_to_close(true, cx)
})
})?
.await?
{
return Ok(());
@ -444,7 +444,7 @@ fn quit(_: &Quit, cx: &mut gpui::AppContext) {
if !workspace
.update(&mut cx, |workspace, cx| {
workspace.prepare_to_close(true, cx)
})
})?
.await?
{
return Ok(());
@ -481,8 +481,8 @@ fn open_config_file(
workspace.with_local_workspace(&app_state, cx, |workspace, cx| {
workspace.open_paths(vec![path.to_path_buf()], false, cx)
})
})
.await
})?
.await?
.await;
Ok::<_, anyhow::Error>(())
})
@ -521,25 +521,25 @@ fn open_log_file(
.flat_map(|line| [line, "\n"])
.collect::<String>();
workspace.update(&mut cx, |workspace, cx| {
let project = workspace.project().clone();
let buffer = project
.update(cx, |project, cx| project.create_buffer("", None, cx))
.expect("creating buffers on a local workspace always succeeds");
buffer.update(cx, |buffer, cx| buffer.edit([(0..0, log)], None, cx));
workspace
.update(&mut cx, |workspace, cx| {
let project = workspace.project().clone();
let buffer = project
.update(cx, |project, cx| project.create_buffer("", None, cx))
.expect("creating buffers on a local workspace always succeeds");
buffer.update(cx, |buffer, cx| buffer.edit([(0..0, log)], None, cx));
let buffer = cx.add_model(|cx| {
MultiBuffer::singleton(buffer, cx).with_title("Log".into())
});
workspace.add_item(
Box::new(
cx.add_view(|cx| {
let buffer = cx.add_model(|cx| {
MultiBuffer::singleton(buffer, cx).with_title("Log".into())
});
workspace.add_item(
Box::new(cx.add_view(|cx| {
Editor::for_multibuffer(buffer, Some(project), cx)
}),
),
cx,
);
});
})),
cx,
);
})
.log_err();
}
})
.detach();
@ -601,7 +601,7 @@ fn open_telemetry_log_file(
Box::new(cx.add_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx))),
cx,
);
});
}).log_err()?;
Some(())
})
@ -642,10 +642,10 @@ fn open_bundled_file(
cx,
);
})
})
.await;
})?
.await
})
.detach();
.detach_and_log_err(cx);
}
#[cfg(test)]
@ -705,11 +705,13 @@ mod tests {
cx,
)
})
.await;
.await
.unwrap();
assert_eq!(cx.window_ids().len(), 1);
cx.update(|cx| open_paths(&[PathBuf::from("/root/a")], &app_state, cx))
.await;
.await
.unwrap();
assert_eq!(cx.window_ids().len(), 1);
let workspace_1 = cx
.read_window(cx.window_ids()[0], |cx| cx.root_view().clone())
@ -729,7 +731,8 @@ mod tests {
cx,
)
})
.await;
.await
.unwrap();
assert_eq!(cx.window_ids().len(), 2);
// Replace existing windows
@ -741,7 +744,8 @@ mod tests {
cx,
)
})
.await;
.await
.unwrap();
assert_eq!(cx.window_ids().len(), 2);
cx.read_window(window_id, |cx| {
let workspace = cx.root_view().clone().downcast::<Workspace>().unwrap();
@ -768,7 +772,8 @@ mod tests {
.await;
cx.update(|cx| open_paths(&[PathBuf::from("/root/a")], &app_state, cx))
.await;
.await
.unwrap();
assert_eq!(cx.window_ids().len(), 1);
// When opening the workspace, the window is not in a edited state.
@ -810,7 +815,8 @@ mod tests {
// Opening the buffer again doesn't impact the window's edited state.
cx.update(|cx| open_paths(&[PathBuf::from("/root/a")], &app_state, cx))
.await;
.await
.unwrap();
let editor = workspace.read_with(cx, |workspace, cx| {
workspace
.active_item(cx)
@ -1475,7 +1481,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0), 0.)
@ -1483,7 +1490,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file2.clone(), DisplayPoint::new(0, 0), 0.)
@ -1491,7 +1499,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0), 0.)
@ -1499,7 +1508,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(0, 0), 0.)
@ -1508,7 +1518,8 @@ mod tests {
// Go back one more time and ensure we don't navigate past the first item in the history.
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(0, 0), 0.)
@ -1516,7 +1527,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_forward(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0), 0.)
@ -1524,7 +1536,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_forward(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file2.clone(), DisplayPoint::new(0, 0), 0.)
@ -1542,7 +1555,8 @@ mod tests {
.unwrap();
workspace
.update(cx, |w, cx| Pane::go_forward(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0), 0.)
@ -1550,7 +1564,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_forward(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(16, 0), 12.5)
@ -1558,7 +1573,8 @@ mod tests {
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0), 0.)
@ -1580,14 +1596,16 @@ mod tests {
.unwrap();
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0), 0.)
);
workspace
.update(cx, |w, cx| Pane::go_forward(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0), 0.)
@ -1630,14 +1648,16 @@ mod tests {
});
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(2, 0), 0.)
);
workspace
.update(cx, |w, cx| Pane::go_back(w, None, cx))
.await;
.await
.unwrap();
assert_eq!(
active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(3, 0), 0.)
@ -1751,61 +1771,84 @@ mod tests {
// Reopen all the closed items, ensuring they are reopened in the same order
// in which they were closed.
workspace.update(cx, Pane::reopen_closed_item).await;
workspace
.update(cx, Pane::reopen_closed_item)
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file3.clone()));
workspace.update(cx, Pane::reopen_closed_item).await;
workspace
.update(cx, Pane::reopen_closed_item)
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file2.clone()));
workspace.update(cx, Pane::reopen_closed_item).await;
workspace
.update(cx, Pane::reopen_closed_item)
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file4.clone()));
workspace.update(cx, Pane::reopen_closed_item).await;
workspace
.update(cx, Pane::reopen_closed_item)
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file1.clone()));
// Reopening past the last closed item is a no-op.
workspace.update(cx, Pane::reopen_closed_item).await;
workspace
.update(cx, Pane::reopen_closed_item)
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file1.clone()));
// Reopening closed items doesn't interfere with navigation history.
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file4.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file2.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file3.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file4.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file3.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file2.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file1.clone()));
workspace
.update(cx, |workspace, cx| Pane::go_back(workspace, None, cx))
.await;
.await
.unwrap();
assert_eq!(active_path(&workspace, cx), Some(file1.clone()));
fn active_path(