Async runtime working but is blocking

Isaac Clayton created

Change summary

crates/zed/src/languages/language_plugin.rs | 78 ++++++++++------------
plugins/json_language/src/lib.rs            | 28 ++++----
2 files changed, 49 insertions(+), 57 deletions(-)

Detailed changes

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

@@ -61,21 +61,20 @@ impl LspAdapter for PluginLspAdapter {
         &self,
         _: Arc<dyn HttpClient>,
     ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
-        todo!()
-        // async move {
-        //     let versions: Result<String, String> = self
-        //         .runtime
-        //         .lock()
-        //         .call::<_, Option<String>>("fetch_latest_server_version", ())
-        //         .await?;
-        //     versions.map(|(language_version, server_version)| {
-        //         Box::new(Versions {
-        //             language_version,
-        //             server_version,
-        //         }) as Box<_>
-        //     })
-        // }
-        // .boxed()
+        let versions: Result<Option<String>> = call_block!(self, "fetch_latest_server_version", ());
+        async move {
+            // let versions: Result<Option<String>> = self
+            //     .runtime
+            //     .lock()
+            //     .call::<_, Option<String>>("fetch_latest_server_version", ())
+            //     .await;
+
+            versions
+                .map_err(|e| anyhow!("{}", e))?
+                .ok_or_else(|| anyhow!("Could not fetch latest server version"))
+                .map(|v| Box::new(v) as Box<_>)
+        }
+        .boxed()
     }
 
     fn fetch_server_binary(
@@ -84,37 +83,30 @@ impl LspAdapter for PluginLspAdapter {
         _: Arc<dyn HttpClient>,
         container_dir: PathBuf,
     ) -> BoxFuture<'static, Result<PathBuf>> {
-        todo!()
-        // let version = version.downcast::<String>().unwrap();
-
-        // async move {
-        //     let runtime = self.runtime.clone();
-        //     let handle = runtime.lock().attach_path(&container_dir).unwrap();
-        //     let result = runtime
-        //         .lock()
-        //         .call::<_, Option<PathBuf>>("fetch_server_binary", container_dir)
-        //         .await
-        //         .unwrap()
-        //         .ok_or_else(|| anyhow!("Could not load cached server binary"));
-        //     // runtime.remove_resource(handle).ok();
-        //     result
-        // }
-        // .boxed()
+        let version = version.downcast::<String>().unwrap();
+        let mut runtime = self.runtime.lock();
+        let result = (|| {
+            let handle = runtime.attach_path(&container_dir)?;
+            let result: Option<PathBuf> =
+                call_block!(self, "fetch_server_binary", (container_dir, version))?;
+            runtime.remove_resource(handle)?;
+            result.ok_or_else(|| anyhow!("Could not load cached server binary"))
+        })();
+
+        async move { result }.boxed()
     }
 
     fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
-        todo!()
-        // let runtime = self.runtime.clone();
-        // async move {
-        //     let handle = runtime.lock().attach_path(&container_dir).ok()?;
-        //     let result = runtime
-        //         .lock()
-        //         .call::<_, Option<PathBuf>>("cached_server_binary", container_dir);
-        //     let result = result.await;
-        //     runtime.lock().remove_resource(handle).ok()?;
-        //     result.ok()?
-        // }
-        // .boxed()
+        let mut runtime = self.runtime.lock();
+        let result: Option<PathBuf> = (|| {
+            let handle = runtime.attach_path(&container_dir).ok()?;
+            let result: Option<PathBuf> =
+                call_block!(self, "cached_server_binary", container_dir).ok()?;
+            runtime.remove_resource(handle).ok()?;
+            result
+        })();
+
+        async move { result }.boxed()
     }
 
     fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}

plugins/json_language/src/lib.rs 🔗

@@ -41,24 +41,24 @@ pub fn server_args() -> Vec<String> {
     vec!["--stdio".into()]
 }
 
-// #[bind]
-// pub fn fetch_latest_server_version() -> Option<String> {
-//     #[derive(Deserialize)]
-//     struct NpmInfo {
-//         versions: Vec<String>,
-//     }
+#[bind]
+pub fn fetch_latest_server_version() -> Option<String> {
+    #[derive(Deserialize)]
+    struct NpmInfo {
+        versions: Vec<String>,
+    }
 
-//     let output = command("npm info vscode-json-languageserver --json")?;
-//     if !output.status.success() {
-//         return None;
-//     }
+    let output = command("npm info vscode-json-languageserver --json")?;
+    if !output.status.success() {
+        return None;
+    }
 
-//     let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
-//     info.versions.pop()
-// }
+    let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
+    info.versions.pop()
+}
 
 // #[bind]
-// pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<PathBuf, String> {
+// pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Option<PathBuf> {
 //     let version_dir = container_dir.join(version.as_str());
 //     fs::create_dir_all(&version_dir)
 //         .or_or_else(|| "failed to create version directory".to_string())?;