use anyhow::{anyhow, Result}; use client::http::HttpClient; use futures::lock::Mutex; use futures::Future; use futures::{future::BoxFuture, FutureExt}; use gpui::executor::Background; use language::{LanguageServerName, LspAdapter}; use plugin_runtime::{Plugin, PluginBuilder, WasiFn}; use std::task::Poll; use std::{any::Any, path::PathBuf, sync::Arc}; use util::ResultExt; use future_wrap::*; pub async fn new_json(executor: Arc) -> Result { let plugin = PluginBuilder::new_with_default_ctx()? .host_function_async("command", |command: String| async move { // TODO: actual thing dbg!(&command); let mut args = command.split(' '); let command = args.next().unwrap(); dbg!("Running command"); let future = smol::process::Command::new(command).args(args).output(); dbg!("Awaiting command"); #[no_mangle] fn heck_point() { dbg!("command awaited"); } let future = future.wrap(|fut, cx| { dbg!("Poll command!"); let res = fut.poll(cx); res }); let future = future.await; heck_point(); dbg!("blocked on future"); future.log_err().map(|output| { dbg!("done running command"); output.stdout }) })? .init(include_bytes!("../../../../plugins/bin/json_language.wasm")) .await?; PluginLspAdapter::new(plugin, executor).await } pub struct PluginLspAdapter { name: WasiFn<(), String>, server_args: WasiFn<(), Vec>, fetch_latest_server_version: WasiFn<(), Option>, fetch_server_binary: WasiFn<(PathBuf, String), Result>, cached_server_binary: WasiFn>, // label_for_completion: WasiFn>, initialization_options: WasiFn<(), String>, executor: Arc, runtime: Arc>, } impl PluginLspAdapter { pub async fn new(mut plugin: Plugin, executor: Arc) -> Result { Ok(Self { name: plugin.function("name")?, server_args: plugin.function("server_args")?, fetch_latest_server_version: plugin.function("fetch_latest_server_version")?, fetch_server_binary: plugin.function("fetch_server_binary")?, cached_server_binary: plugin.function("cached_server_binary")?, // label_for_completion: plugin.function("label_for_completion")?, initialization_options: plugin.function("initialization_options")?, executor, runtime: Arc::new(Mutex::new(plugin)), }) } } struct Versions { language_version: String, server_version: String, } // TODO: is this the root cause? // sketch: // - smol command is run, hits await, switches // - name is run, blocked on // - smol command is still pending // - name is stuck for some reason(?) // - no progress made... // - deadlock!!!? // I wish there was high-level instrumentation for this... // - it's totally a deadlock, the proof is in the pudding // macro_rules! call_block { // ($self:ident, $name:expr, $arg:expr) => { // $self.executor.block(async { // dbg!("starting to block on something"); // let locked = $self.runtime.lock(); // dbg!("locked runtime"); // // TODO: No blocking calls! // let mut awaited = locked.await; // dbg!("awaited lock"); // let called = awaited.call($name, $arg); // dbg!("called function"); // let result = called.await; // dbg!("awaited result"); // result // }) // }; // } // TODO: convert to async trait #[async_trait] impl LspAdapter for PluginLspAdapter { async fn name(&self) -> LanguageServerName { let name: String = call_block!(self, &self.name, ()).unwrap(); LanguageServerName(name.into()) } async fn server_args<'a>(&'a self) -> Vec { call_block!(self, &self.server_args, ()).unwrap() } async fn fetch_latest_server_version( &self, _: Arc, ) -> Result> { // let versions: Result> = call_block!(self, "fetch_latest_server_version", ()); let runtime = self.runtime.clone(); let function = self.fetch_latest_server_version; async move { let mut runtime = runtime.lock().await; let versions: Result> = runtime.call::<_, Option>(&function, ()).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( &self, version: Box, _: Arc, container_dir: PathBuf, ) -> BoxFuture<'static, Result> { let version = *version.downcast::().unwrap(); let runtime = self.runtime.clone(); let function = self.fetch_server_binary; async move { let mut runtime = runtime.lock().await; let handle = runtime.attach_path(&container_dir)?; let result: Result = runtime.call(&function, (container_dir, version)).await?; runtime.remove_resource(handle)?; result.map_err(|e| anyhow!("{}", e)) } .boxed() } fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option> { let runtime = self.runtime.clone(); let function = self.cached_server_binary; async move { let mut runtime = runtime.lock().await; let handle = runtime.attach_path(&container_dir).ok()?; let result: Option = runtime.call(&function, container_dir).await.ok()?; runtime.remove_resource(handle).ok()?; result } .boxed() } fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} // fn label_for_completion( // &self, // item: &lsp::CompletionItem, // language: &language::Language, // ) -> Option { // // TODO: Push more of this method down into the plugin. // use lsp::CompletionItemKind as Kind; // let len = item.label.len(); // let grammar = language.grammar()?; // let kind = format!("{:?}", item.kind?); // let name: String = call_block!(self, &self.label_for_completion, kind).log_err()??; // let highlight_id = grammar.highlight_id_for_name(&name)?; // Some(language::CodeLabel { // text: item.label.clone(), // runs: vec![(0..len, highlight_id)], // filter_range: 0..len, // }) // } fn initialization_options(&self) -> Option { let string: String = call_block!(self, &self.initialization_options, ()).log_err()?; serde_json::from_str(&string).ok() } }