More work on transitioning to async, need to figure out when to stop

Isaac Clayton created

Change summary

crates/plugin_runtime/src/plugin.rs    |  7 --
crates/project/src/project.rs          | 63 ++++++++++++++++------------
crates/zed/src/languages/c.rs          | 22 ++++-----
crates/zed/src/languages/go.rs         | 26 +++++------
crates/zed/src/languages/json.rs       | 24 ++++-----
crates/zed/src/languages/python.rs     | 26 +++++------
crates/zed/src/languages/rust.rs       | 30 ++++++------
crates/zed/src/languages/typescript.rs | 24 ++++-----
8 files changed, 109 insertions(+), 113 deletions(-)

Detailed changes

crates/plugin_runtime/src/plugin.rs 🔗

@@ -66,15 +66,11 @@ impl PluginBuilder {
     /// Create a new [`PluginBuilder`] with the given WASI context.
     /// Using the default context is a safe bet, see [`new_with_default_context`].
     pub fn new(wasi_ctx: WasiCtx) -> Result<Self, Error> {
-        dbg!("new plugin");
         let mut config = Config::default();
         config.async_support(true);
-        dbg!("Creating engine");
-        let start = std::time::Instant::now();
+        config.epoch_interruption(true);
         let engine = Engine::new(&config)?;
-        dbg!(start.elapsed());
         let linker = Linker::new(&engine);
-        dbg!(start.elapsed());
 
         Ok(PluginBuilder {
             // host_functions: HashMap::new(),
@@ -317,6 +313,7 @@ impl Plugin {
                 alloc: None,
             },
         );
+        // store.epoch_deadline_async_yield_and_update(todo!());
         let module = Module::new(&engine, module)?;
 
         // load the provided module into the asynchronous runtime

crates/project/src/project.rs 🔗

@@ -31,8 +31,8 @@ use language::{
     Transaction,
 };
 use lsp::{
-    DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, LanguageString,
-    MarkedString,
+    CompletionList, DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer,
+    LanguageString, MarkedString,
 };
 use lsp_command::*;
 use parking_lot::Mutex;
@@ -708,7 +708,7 @@ impl Project {
         })
     }
 
-    async fn on_settings_changed(&mut self, cx: &mut ModelContext<'_, Self>) {
+    fn on_settings_changed(&mut self, cx: &mut ModelContext<'_, Self>) {
         let settings = cx.global::<Settings>();
 
         let mut language_servers_to_start = Vec::new();
@@ -3302,15 +3302,15 @@ impl Project {
                                 path = relativize_path(&worktree_abs_path, &abs_path);
                             }
 
-                            let label = this
-                                .languages
-                                .select_language(&path)
-                                .and_then(|language| {
+                            let label = match this.languages.select_language(&path) {
+                                Some(language) => {
                                     language
                                         .label_for_symbol(&lsp_symbol.name, lsp_symbol.kind)
                                         .await
-                                })
-                                .unwrap_or_else(|| CodeLabel::plain(lsp_symbol.name.clone(), None));
+                                }
+                                None => None,
+                            }
+                            .unwrap_or_else(|| CodeLabel::plain(lsp_symbol.name.clone(), None));
                             let signature = this.symbol_signature(worktree_id, &path);
 
                             Some(Symbol {
@@ -3550,15 +3550,18 @@ impl Project {
                             Some(Completion {
                                 old_range,
                                 new_text,
-                                label: language
-                                    .as_ref()
-                                    .and_then(|l| l.label_for_completion(&lsp_completion).await)
+                                label: {
+                                    match language.as_ref() {
+                                        Some(l) => l.label_for_completion(&lsp_completion).await,
+                                        None => None,
+                                    }
                                     .unwrap_or_else(|| {
                                         CodeLabel::plain(
                                             lsp_completion.label.clone(),
                                             lsp_completion.filter_text.as_deref(),
                                         )
-                                    }),
+                                    })
+                                },
                                 lsp_completion,
                             })
                         })
@@ -3582,13 +3585,14 @@ impl Project {
                     })
                     .await;
 
-                response
-                    .completions
-                    .into_iter()
-                    .map(|completion| {
-                        language::proto::deserialize_completion(completion, language.as_ref()).await
-                    })
-                    .collect()
+                let completions = Vec::new();
+                for completion in response.completions.into_iter() {
+                    completions.push(
+                        language::proto::deserialize_completion(completion, language.as_ref())
+                            .await,
+                    );
+                }
+                completions.into_iter().collect()
             })
         } else {
             Task::ready(Ok(Default::default()))
@@ -5189,7 +5193,7 @@ impl Project {
         _: Arc<Client>,
         mut cx: AsyncAppContext,
     ) -> Result<proto::ApplyCompletionAdditionalEditsResponse> {
-        let apply_additional_edits = this.update(&mut cx, |this, cx| {
+        let (buffer, completion) = this.update(&mut cx, |this, cx| {
             let buffer = this
                 .opened_buffers
                 .get(&envelope.payload.buffer_id)
@@ -5202,13 +5206,16 @@ impl Project {
                     .completion
                     .ok_or_else(|| anyhow!("invalid completion"))?,
                 language,
-            )
-            .await?;
-            Ok::<_, anyhow::Error>(
-                this.apply_additional_edits_for_completion(buffer, completion, false, cx),
-            )
+            );
+            Ok::<_, anyhow::Error>((buffer, completion))
         })?;
 
+        let completion = completion.await?;
+
+        let apply_additional_edits = this.update(&mut cx, |this, cx| {
+            this.apply_additional_edits_for_completion(buffer, completion, false, cx)
+        });
+
         Ok(proto::ApplyCompletionAdditionalEditsResponse {
             transaction: apply_additional_edits
                 .await?
@@ -5390,8 +5397,10 @@ impl Project {
             .payload
             .symbol
             .ok_or_else(|| anyhow!("invalid symbol"))?;
+        let symbol = this
+            .read_with(&cx, |this, _| this.deserialize_symbol(symbol))
+            .await?;
         let symbol = this.read_with(&cx, |this, _| {
-            let symbol = this.deserialize_symbol(symbol).await?;
             let signature = this.symbol_signature(symbol.worktree_id, &symbol.path);
             if signature == symbol.signature {
                 Ok(symbol)

crates/zed/src/languages/c.rs 🔗

@@ -13,15 +13,16 @@ use util::{ResultExt, TryFutureExt};
 
 pub struct CLspAdapter;
 
+#[async_trait]
 impl super::LspAdapter for CLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("clangd".into())
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         http: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
+    ) -> Result<Box<dyn 'static + Send + Any>> {
         async move {
             let release = latest_github_release("clangd/clangd", http).await?;
             let asset_name = format!("clangd-mac-{}.zip", release.name);
@@ -39,12 +40,12 @@ impl super::LspAdapter for CLspAdapter {
         .boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         version: Box<dyn 'static + Send + Any>,
         http: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
         async move {
             let zip_path = container_dir.join(format!("clangd_{}.zip", version.name));
@@ -92,10 +93,7 @@ impl super::LspAdapter for CLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
-        &self,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Option<PathBuf>> {
+    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
         async move {
             let mut last_clangd_dir = None;
             let mut entries = fs::read_dir(&container_dir).await?;
@@ -120,7 +118,7 @@ impl super::LspAdapter for CLspAdapter {
         .boxed()
     }
 
-    fn label_for_completion(
+    async fn label_for_completion(
         &self,
         completion: &lsp::CompletionItem,
         language: &Language,
@@ -197,7 +195,7 @@ impl super::LspAdapter for CLspAdapter {
         Some(CodeLabel::plain(label.to_string(), None))
     }
 
-    fn label_for_symbol(
+    async fn label_for_symbol(
         &self,
         name: &str,
         kind: lsp::SymbolKind,

crates/zed/src/languages/go.rs 🔗

@@ -22,19 +22,20 @@ lazy_static! {
     static ref GOPLS_VERSION_REGEX: Regex = Regex::new(r"\d+\.\d+\.\d+").unwrap();
 }
 
+#[async_trait]
 impl super::LspAdapter for GoLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("gopls".into())
     }
 
-    fn server_args(&self) -> &[&str] {
-        &["-mode=stdio"]
+    async fn server_args(&self) -> Vec<String> {
+        vec!["-mode=stdio".into()]
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         http: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
+    ) -> Result<Box<dyn 'static + Send + Any>> {
         async move {
             let release = latest_github_release("golang/tools", http).await?;
             let version: Option<String> = release.name.strip_prefix("gopls/v").map(str::to_string);
@@ -49,12 +50,12 @@ impl super::LspAdapter for GoLspAdapter {
         .boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         version: Box<dyn 'static + Send + Any>,
         _: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         let version = version.downcast::<Option<String>>().unwrap();
         let this = *self;
 
@@ -115,10 +116,7 @@ impl super::LspAdapter for GoLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
-        &self,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Option<PathBuf>> {
+    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
         async move {
             let mut last_binary_path = None;
             let mut entries = fs::read_dir(&container_dir).await?;
@@ -144,7 +142,7 @@ impl super::LspAdapter for GoLspAdapter {
         .boxed()
     }
 
-    fn label_for_completion(
+    async fn label_for_completion(
         &self,
         completion: &lsp::CompletionItem,
         language: &Language,
@@ -244,7 +242,7 @@ impl super::LspAdapter for GoLspAdapter {
         None
     }
 
-    fn label_for_symbol(
+    async fn label_for_symbol(
         &self,
         name: &str,
         kind: lsp::SymbolKind,

crates/zed/src/languages/json.rs 🔗

@@ -19,31 +19,32 @@ impl JsonLspAdapter {
         "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
 }
 
+#[async_trait]
 impl LspAdapter for JsonLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("vscode-json-languageserver".into())
     }
 
-    fn server_args(&self) -> Vec<String> {
+    async fn server_args(&self) -> Vec<String> {
         vec!["--stdio".into()]
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         _: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
+    ) -> Result<Box<dyn 'static + Any + Send>> {
         async move {
             Ok(Box::new(npm_package_latest_version("vscode-json-languageserver").await?) as Box<_>)
         }
         .boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         version: Box<dyn 'static + Send + Any>,
         _: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         let version = version.downcast::<String>().unwrap();
         async move {
             let version_dir = container_dir.join(version.as_str());
@@ -76,10 +77,7 @@ impl LspAdapter for JsonLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
-        &self,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Option<PathBuf>> {
+    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
         async move {
             let mut last_version_dir = None;
             let mut entries = fs::read_dir(&container_dir).await?;
@@ -104,13 +102,13 @@ impl LspAdapter for JsonLspAdapter {
         .boxed()
     }
 
-    fn initialization_options(&self) -> Option<serde_json::Value> {
+    async fn initialization_options(&self) -> Option<serde_json::Value> {
         Some(json!({
             "provideFormatter": true
         }))
     }
 
-    fn id_for_language(&self, name: &str) -> Option<String> {
+    async fn id_for_language(&self, name: &str) -> Option<String> {
         if name == "JSON" {
             Some("jsonc".into())
         } else {

crates/zed/src/languages/python.rs 🔗

@@ -17,28 +17,29 @@ impl PythonLspAdapter {
     const BIN_PATH: &'static str = "node_modules/pyright/langserver.index.js";
 }
 
+#[async_trait]
 impl LspAdapter for PythonLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("pyright".into())
     }
 
-    fn server_args(&self) -> &[&str] {
-        &["--stdio"]
+    async fn server_args(&self) -> Vec<String> {
+        vec!["--stdio".into()]
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         _: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
+    ) -> Result<Box<dyn 'static + Any + Send>> {
         async move { Ok(Box::new(npm_package_latest_version("pyright").await?) as Box<_>) }.boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         version: Box<dyn 'static + Send + Any>,
         _: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         let version = version.downcast::<String>().unwrap();
         async move {
             let version_dir = container_dir.join(version.as_str());
@@ -67,10 +68,7 @@ impl LspAdapter for PythonLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
-        &self,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Option<PathBuf>> {
+    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
         async move {
             let mut last_version_dir = None;
             let mut entries = fs::read_dir(&container_dir).await?;
@@ -95,7 +93,7 @@ impl LspAdapter for PythonLspAdapter {
         .boxed()
     }
 
-    fn label_for_completion(
+    async fn label_for_completion(
         &self,
         item: &lsp::CompletionItem,
         language: &language::Language,
@@ -116,7 +114,7 @@ impl LspAdapter for PythonLspAdapter {
         })
     }
 
-    fn label_for_symbol(
+    async fn label_for_symbol(
         &self,
         name: &str,
         kind: lsp::SymbolKind,

crates/zed/src/languages/rust.rs 🔗

@@ -20,14 +20,14 @@ use util::{ResultExt, TryFutureExt};
 pub struct RustLspAdapter;
 
 impl LspAdapter for RustLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("rust-analyzer".into())
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         http: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
+    ) -> Result<Box<dyn 'static + Send + Any>> {
         async move {
             let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?;
             let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
@@ -45,12 +45,12 @@ impl LspAdapter for RustLspAdapter {
         .boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         version: Box<dyn 'static + Send + Any>,
         http: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         async move {
             let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
             let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name));
@@ -86,9 +86,9 @@ impl LspAdapter for RustLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
+    async fn cached_server_binary(
         &self,
-        container_dir: Arc<Path>,
+        container_dir: PathBuf,
     ) -> BoxFuture<'static, Option<PathBuf>> {
         async move {
             let mut last = None;
@@ -102,15 +102,15 @@ impl LspAdapter for RustLspAdapter {
         .boxed()
     }
 
-    fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
-        &["rustc"]
+    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
+        vec!["rustc".into()]
     }
 
-    fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
-        Some("rustAnalyzer/cargo check")
+    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
+        Some("rustAnalyzer/cargo check".into())
     }
 
-    fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
+    async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
         lazy_static! {
             static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap();
         }
@@ -130,7 +130,7 @@ impl LspAdapter for RustLspAdapter {
         }
     }
 
-    fn label_for_completion(
+    async fn label_for_completion(
         &self,
         completion: &lsp::CompletionItem,
         language: &Language,
@@ -206,7 +206,7 @@ impl LspAdapter for RustLspAdapter {
         None
     }
 
-    fn label_for_symbol(
+    async fn label_for_symbol(
         &self,
         name: &str,
         kind: lsp::SymbolKind,

crates/zed/src/languages/typescript.rs 🔗

@@ -23,22 +23,23 @@ struct Versions {
     server_version: String,
 }
 
+#[async_trait]
 impl LspAdapter for TypeScriptLspAdapter {
-    fn name(&self) -> LanguageServerName {
+    async fn name(&self) -> LanguageServerName {
         LanguageServerName("typescript-language-server".into())
     }
 
-    fn server_args(&self) -> Vec<String> {
+    async fn server_args(&self) -> Vec<String> {
         ["--stdio", "--tsserver-path", "node_modules/typescript/lib"]
             .into_iter()
             .map(str::to_string)
             .collect()
     }
 
-    fn fetch_latest_server_version(
+    async fn fetch_latest_server_version(
         &self,
         _: Arc<dyn HttpClient>,
-    ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
+    ) -> Result<Box<dyn 'static + Send + Any>> {
         async move {
             Ok(Box::new(Versions {
                 typescript_version: npm_package_latest_version("typescript").await?,
@@ -48,12 +49,12 @@ impl LspAdapter for TypeScriptLspAdapter {
         .boxed()
     }
 
-    fn fetch_server_binary(
+    async fn fetch_server_binary(
         &self,
         versions: Box<dyn 'static + Send + Any>,
         _: Arc<dyn HttpClient>,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Result<PathBuf>> {
+        container_dir: PathBuf,
+    ) -> Result<PathBuf> {
         let versions = versions.downcast::<Versions>().unwrap();
         async move {
             let version_dir = container_dir.join(&format!(
@@ -95,10 +96,7 @@ impl LspAdapter for TypeScriptLspAdapter {
         .boxed()
     }
 
-    fn cached_server_binary(
-        &self,
-        container_dir: Arc<Path>,
-    ) -> BoxFuture<'static, Option<PathBuf>> {
+    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
         async move {
             let mut last_version_dir = None;
             let mut entries = fs::read_dir(&container_dir).await?;
@@ -123,7 +121,7 @@ impl LspAdapter for TypeScriptLspAdapter {
         .boxed()
     }
 
-    fn label_for_completion(
+    async fn label_for_completion(
         &self,
         item: &lsp::CompletionItem,
         language: &language::Language,
@@ -146,7 +144,7 @@ impl LspAdapter for TypeScriptLspAdapter {
         })
     }
 
-    fn initialization_options(&self) -> Option<serde_json::Value> {
+    async fn initialization_options(&self) -> Option<serde_json::Value> {
         Some(json!({
             "provideFormatter": true
         }))