First pass of plugin side of things complete

This commit is contained in:
Isaac Clayton 2022-06-08 10:32:19 +02:00
parent d7b97b25b8
commit 923f093aca
4 changed files with 78 additions and 56 deletions

View file

@ -1,11 +1,11 @@
use super::installation::{npm_install_packages, npm_package_latest_version}; use super::installation::{npm_install_packages, npm_package_latest_version};
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use client::http::HttpClient; use client::http::HttpClient;
use futures::lock::Mutex;
use futures::{future::BoxFuture, FutureExt, StreamExt}; use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::executor::{self, Background}; use gpui::executor::{self, Background};
use isahc::http::version; use isahc::http::version;
use language::{LanguageServerName, LspAdapter}; use language::{LanguageServerName, LspAdapter};
use parking_lot::{Mutex, RwLock};
use plugin_runtime::{Wasi, WasiPlugin}; use plugin_runtime::{Wasi, WasiPlugin};
use serde_json::json; use serde_json::json;
use std::fs; use std::fs;
@ -43,7 +43,7 @@ macro_rules! call_block {
($self:ident, $name:expr, $arg:expr) => { ($self:ident, $name:expr, $arg:expr) => {
$self $self
.executor .executor
.block(async { $self.runtime.lock().call($name, $arg).await }) .block(async { $self.runtime.lock().await.call($name, $arg).await })
}; };
} }
@ -61,14 +61,13 @@ impl LspAdapter for PluginLspAdapter {
&self, &self,
_: Arc<dyn HttpClient>, _: Arc<dyn HttpClient>,
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> { ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
let versions: Result<Option<String>> = call_block!(self, "fetch_latest_server_version", ()); // let versions: Result<Option<String>> = call_block!(self, "fetch_latest_server_version", ());
let runtime = self.runtime.clone();
async move { async move {
// let versions: Result<Option<String>> = self let mut runtime = runtime.lock().await;
// .runtime let versions: Result<Option<String>> = runtime
// .lock() .call::<_, Option<String>>("fetch_latest_server_version", ())
// .call::<_, Option<String>>("fetch_latest_server_version", ()) .await;
// .await;
versions versions
.map_err(|e| anyhow!("{}", e))? .map_err(|e| anyhow!("{}", e))?
.ok_or_else(|| anyhow!("Could not fetch latest server version")) .ok_or_else(|| anyhow!("Could not fetch latest server version"))
@ -84,29 +83,34 @@ impl LspAdapter for PluginLspAdapter {
container_dir: PathBuf, container_dir: PathBuf,
) -> BoxFuture<'static, Result<PathBuf>> { ) -> BoxFuture<'static, Result<PathBuf>> {
let version = version.downcast::<String>().unwrap(); let version = version.downcast::<String>().unwrap();
let mut runtime = self.runtime.lock(); let runtime = self.runtime.clone();
let result = (|| {
async move {
let mut runtime = runtime.lock().await;
let handle = runtime.attach_path(&container_dir)?; let handle = runtime.attach_path(&container_dir)?;
let result: Option<PathBuf> = let result: Option<PathBuf> = runtime
call_block!(self, "fetch_server_binary", (container_dir, version))?; .call("fetch_server_binary", (container_dir, version))
.await?;
runtime.remove_resource(handle)?; runtime.remove_resource(handle)?;
result.ok_or_else(|| anyhow!("Could not load cached server binary")) result.ok_or_else(|| anyhow!("Could not load cached server binary"))
})(); }
.boxed()
async move { result }.boxed()
} }
fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> { fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
let mut runtime = self.runtime.lock(); let runtime = self.runtime.clone();
let result: Option<PathBuf> = (|| {
async move {
let mut runtime = runtime.lock().await;
let handle = runtime.attach_path(&container_dir).ok()?; let handle = runtime.attach_path(&container_dir).ok()?;
let result: Option<PathBuf> = let result: Option<PathBuf> = runtime
call_block!(self, "cached_server_binary", container_dir).ok()?; .call("cached_server_binary", container_dir)
.await
.ok()?;
runtime.remove_resource(handle).ok()?; runtime.remove_resource(handle).ok()?;
result result
})(); }
.boxed()
async move { result }.boxed()
} }
fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}

14
plugins/Cargo.lock generated
View file

@ -75,6 +75,20 @@ name = "serde"
version = "1.0.137" version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
plugin = { path = "../../crates/plugin" } plugin = { path = "../../crates/plugin" }
serde = "1.0" serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
[lib] [lib]

View file

@ -1,10 +1,13 @@
use plugin::prelude::*; use plugin::prelude::*;
use serde::Deserialize;
use serde_json::json; use serde_json::json;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
// #[import] // #[import]
// fn command(string: String) -> Option<String>; fn command(string: &str) -> Option<String> {
todo!()
}
// TODO: some sort of macro to generate ABI bindings // TODO: some sort of macro to generate ABI bindings
extern "C" { extern "C" {
@ -48,45 +51,46 @@ pub fn fetch_latest_server_version() -> Option<String> {
versions: Vec<String>, versions: Vec<String>,
} }
// TODO: command returns error code
let output = command("npm info vscode-json-languageserver --json")?; let output = command("npm info vscode-json-languageserver --json")?;
if !output.status.success() { // if !output.is_ok() {
return None; // return None;
} // }
let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?; let mut info: NpmInfo = serde_json::from_str(&output).ok()?;
info.versions.pop() info.versions.pop()
} }
// #[bind] #[bind]
// pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Option<PathBuf> { pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<PathBuf, String> {
// let version_dir = container_dir.join(version.as_str()); let version_dir = container_dir.join(version.as_str());
// fs::create_dir_all(&version_dir) fs::create_dir_all(&version_dir)
// .or_or_else(|| "failed to create version directory".to_string())?; .map_err(|_| "failed to create version directory".to_string())?;
// let binary_path = version_dir.join(Self::BIN_PATH); let binary_path = version_dir.join(BIN_PATH);
// if fs::metadata(&binary_path).await.is_err() { if fs::metadata(&binary_path).is_err() {
// let output = command(format!( let output = command(&format!(
// "npm install vscode-json-languageserver@{}", "npm install vscode-json-languageserver@{}",
// version version
// )); ));
// if !output.status.success() { if output.is_none() {
// Err(anyhow!("failed to install vscode-json-languageserver"))?; return Err("failed to install vscode-json-languageserver".to_string());
// } }
// if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() { if let Some(mut entries) = fs::read_dir(&container_dir).ok() {
// while let Some(entry) = entries.next().await { while let Some(entry) = entries.next() {
// if let Some(entry) = entry.log_err() { if let Some(entry) = entry.ok() {
// let entry_path = entry.path(); let entry_path = entry.path();
// if entry_path.as_path() != version_dir { if entry_path.as_path() != version_dir {
// fs::remove_dir_all(&entry_path).await.log_err(); fs::remove_dir_all(&entry_path).ok();
// } }
// } }
// } }
// } }
// } }
// Ok(binary_path) Ok(binary_path)
// } }
#[bind] #[bind]
pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> { pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {