Split out a foreground and background executor

This commit is contained in:
Nathan Sobo 2023-11-01 13:53:08 -06:00
parent dd1a2a9e44
commit 11b6d9e33a
26 changed files with 165 additions and 150 deletions

View file

@ -32,7 +32,7 @@ pub fn lsp_formatting_options(tab_size: u32) -> lsp2::FormattingOptions {
}
}
#[async_trait]
#[async_trait(?Send)]
pub(crate) trait LspCommand: 'static + Sized + Send {
type Response: 'static + Default + Send;
type LspRequest: 'static + Send + lsp2::request::Request;
@ -148,7 +148,7 @@ impl From<lsp2::FormattingOptions> for FormattingOptions {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for PrepareRename {
type Response = Option<Range<Anchor>>;
type LspRequest = lsp2::request::PrepareRenameRequest;
@ -279,7 +279,7 @@ impl LspCommand for PrepareRename {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for PerformRename {
type Response = ProjectTransaction;
type LspRequest = lsp2::request::Rename;
@ -398,7 +398,7 @@ impl LspCommand for PerformRename {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetDefinition {
type Response = Vec<LocationLink>;
type LspRequest = lsp2::request::GotoDefinition;
@ -491,7 +491,7 @@ impl LspCommand for GetDefinition {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetTypeDefinition {
type Response = Vec<LocationLink>;
type LspRequest = lsp2::request::GotoTypeDefinition;
@ -783,7 +783,7 @@ fn location_links_to_proto(
.collect()
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetReferences {
type Response = Vec<Location>;
type LspRequest = lsp2::request::References;
@ -945,7 +945,7 @@ impl LspCommand for GetReferences {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetDocumentHighlights {
type Response = Vec<DocumentHighlight>;
type LspRequest = lsp2::request::DocumentHighlightRequest;
@ -1096,7 +1096,7 @@ impl LspCommand for GetDocumentHighlights {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetHover {
type Response = Option<Hover>;
type LspRequest = lsp2::request::HoverRequest;
@ -1314,7 +1314,7 @@ impl LspCommand for GetHover {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetCompletions {
type Response = Vec<Completion>;
type LspRequest = lsp2::request::Completion;
@ -1545,7 +1545,7 @@ impl LspCommand for GetCompletions {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for GetCodeActions {
type Response = Vec<CodeAction>;
type LspRequest = lsp2::request::CodeActionRequest;
@ -1684,7 +1684,7 @@ impl LspCommand for GetCodeActions {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for OnTypeFormatting {
type Response = Option<Transaction>;
type LspRequest = lsp2::request::OnTypeFormatting;
@ -2192,7 +2192,7 @@ impl InlayHints {
}
}
#[async_trait]
#[async_trait(?Send)]
impl LspCommand for InlayHints {
type Response = Vec<InlayHint>;
type LspRequest = lsp2::InlayHintRequest;

View file

@ -1758,7 +1758,7 @@ impl Project {
}
};
cx.executor().spawn(async move {
cx.background_executor().spawn(async move {
wait_for_loading_buffer(loading_watch)
.await
.map_err(|error| anyhow!("{}", error))
@ -5593,7 +5593,7 @@ impl Project {
})
.collect::<Vec<_>>();
let background = cx.executor().clone();
let background = cx.background_executor().clone();
let path_count: usize = snapshots.iter().map(|s| s.visible_file_count()).sum();
if path_count == 0 {
let (_, rx) = smol::channel::bounded(1024);
@ -5616,11 +5616,11 @@ impl Project {
}
})
.collect();
cx.executor()
cx.background_executor()
.spawn(Self::background_search(
unnamed_files,
opened_buffers,
cx.executor().clone(),
cx.background_executor().clone(),
self.fs.clone(),
workers,
query.clone(),
@ -5631,9 +5631,9 @@ impl Project {
.detach();
let (buffers, buffers_rx) = Self::sort_candidates_and_open_buffers(matching_paths_rx, cx);
let background = cx.executor().clone();
let background = cx.background_executor().clone();
let (result_tx, result_rx) = smol::channel::bounded(1024);
cx.executor()
cx.background_executor()
.spawn(async move {
let Ok(buffers) = buffers.await else {
return;
@ -5993,7 +5993,7 @@ impl Project {
Task::ready(Ok((tree, relative_path)))
} else {
let worktree = self.create_local_worktree(abs_path, visible, cx);
cx.executor()
cx.background_executor()
.spawn(async move { Ok((worktree.await?, PathBuf::new())) })
}
}
@ -6064,7 +6064,7 @@ impl Project {
.shared()
})
.clone();
cx.executor().spawn(async move {
cx.background_executor().spawn(async move {
match task.await {
Ok(worktree) => Ok(worktree),
Err(err) => Err(anyhow!("{}", err)),
@ -6519,7 +6519,7 @@ impl Project {
})
.collect::<Vec<_>>();
cx.executor()
cx.background_executor()
.spawn(async move {
for task_result in future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_task)| {
async move {
@ -7358,7 +7358,7 @@ impl Project {
})
.log_err();
cx.executor()
cx.background_executor()
.spawn(
async move {
let operations = operations.await;
@ -7960,7 +7960,7 @@ impl Project {
if let Some(buffer) = this.buffer_for_id(buffer_id) {
let operations =
buffer.read(cx).serialize_ops(Some(remote_version), cx);
cx.executor().spawn(async move {
cx.background_executor().spawn(async move {
let operations = operations.await;
for chunk in split_operations(operations) {
client
@ -8198,7 +8198,7 @@ impl Project {
cx: &mut ModelContext<Self>,
) -> Task<Result<Vec<(Range<Anchor>, String)>>> {
let snapshot = self.buffer_snapshot_for_lsp_version(buffer, server_id, version, cx);
cx.executor().spawn(async move {
cx.background_executor().spawn(async move {
let snapshot = snapshot?;
let mut lsp_edits = lsp_edits
.into_iter()

View file

@ -365,10 +365,10 @@ impl Worktree {
})
.detach();
let background_scanner_task = cx.executor().spawn({
let background_scanner_task = cx.background_executor().spawn({
let fs = fs.clone();
let snapshot = snapshot.clone();
let background = cx.executor().clone();
let background = cx.background_executor().clone();
async move {
let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
BackgroundScanner::new(
@ -429,7 +429,7 @@ impl Worktree {
let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
cx.executor()
cx.background_executor()
.spawn({
let background_snapshot = background_snapshot.clone();
async move {
@ -1008,7 +1008,7 @@ impl LocalWorktree {
let lowest_ancestor = self.lowest_ancestor(&path);
let abs_path = self.absolutize(&path);
let fs = self.fs.clone();
let write = cx.executor().spawn(async move {
let write = cx.background_executor().spawn(async move {
if is_dir {
fs.create_dir(&abs_path).await
} else {
@ -1058,7 +1058,7 @@ impl LocalWorktree {
let abs_path = self.absolutize(&path);
let fs = self.fs.clone();
let write = cx
.executor()
.background_executor()
.spawn(async move { fs.save(&abs_path, &text, line_ending).await });
cx.spawn(|this, mut cx| async move {
@ -1079,7 +1079,7 @@ impl LocalWorktree {
let abs_path = self.absolutize(&entry.path);
let fs = self.fs.clone();
let delete = cx.executor().spawn(async move {
let delete = cx.background_executor().spawn(async move {
if entry.is_file() {
fs.remove_file(&abs_path, Default::default()).await?;
} else {
@ -1119,7 +1119,7 @@ impl LocalWorktree {
let abs_old_path = self.absolutize(&old_path);
let abs_new_path = self.absolutize(&new_path);
let fs = self.fs.clone();
let rename = cx.executor().spawn(async move {
let rename = cx.background_executor().spawn(async move {
fs.rename(&abs_old_path, &abs_new_path, Default::default())
.await
});
@ -1146,7 +1146,7 @@ impl LocalWorktree {
let abs_old_path = self.absolutize(&old_path);
let abs_new_path = self.absolutize(&new_path);
let fs = self.fs.clone();
let copy = cx.executor().spawn(async move {
let copy = cx.background_executor().spawn(async move {
copy_recursive(
fs.as_ref(),
&abs_old_path,
@ -1174,7 +1174,7 @@ impl LocalWorktree {
) -> Option<Task<Result<()>>> {
let path = self.entry_for_id(entry_id)?.path.clone();
let mut refresh = self.refresh_entries_for_paths(vec![path]);
Some(cx.executor().spawn(async move {
Some(cx.background_executor().spawn(async move {
refresh.next().await;
Ok(())
}))
@ -1248,7 +1248,7 @@ impl LocalWorktree {
.ok();
let worktree_id = cx.entity_id().as_u64();
let _maintain_remote_snapshot = cx.executor().spawn(async move {
let _maintain_remote_snapshot = cx.background_executor().spawn(async move {
let mut is_first = true;
while let Some((snapshot, entry_changes, repo_changes)) = snapshots_rx.next().await {
let update;
@ -1306,7 +1306,7 @@ impl LocalWorktree {
let rx = self.observe_updates(project_id, cx, move |update| {
client.request(update).map(|result| result.is_ok())
});
cx.executor()
cx.background_executor()
.spawn(async move { rx.await.map_err(|_| anyhow!("share ended")) })
}
@ -2672,7 +2672,8 @@ impl language2::LocalFile for File {
let worktree = self.worktree.read(cx).as_local().unwrap();
let abs_path = worktree.absolutize(&self.path);
let fs = worktree.fs.clone();
cx.executor().spawn(async move { fs.load(&abs_path).await })
cx.background_executor()
.spawn(async move { fs.load(&abs_path).await })
}
fn buffer_reloaded(