Add JSON LSP plugin

This commit is contained in:
Isaac Clayton 2022-06-08 11:34:20 +02:00
parent 923f093aca
commit 71e0555763
6 changed files with 98 additions and 36 deletions

View File

@ -286,7 +286,7 @@ impl LanguageRegistry {
.config
.path_suffixes
.iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
.any(|suffix| dbg!(path_suffixes.contains(&Some(dbg!(suffix.as_str())))))
})
.cloned()
}

View File

@ -1,4 +1,4 @@
use std::{fs::File, os::unix::prelude::AsRawFd, path::Path};
use std::{fs::File, marker::PhantomData, path::Path};
use anyhow::{anyhow, Error};
use serde::{de::DeserializeOwned, Serialize};
@ -9,6 +9,29 @@ use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
pub struct WasiResource(u32);
pub struct WasiFn<A: Serialize, R: DeserializeOwned> {
function: TypedFunc<(u32, u32), u32>,
_function_type: PhantomData<fn(A) -> R>,
}
impl<A: Serialize, R: DeserializeOwned> Copy for WasiFn<A, R> {}
impl<A: Serialize, R: DeserializeOwned> Clone for WasiFn<A, R> {
fn clone(&self) -> Self {
Self {
function: self.function,
_function_type: PhantomData,
}
}
}
// impl<A: Serialize, R: DeserializeOwned> WasiFn<A, R> {
// #[inline(always)]
// pub async fn call(&self, runtime: &mut Wasi, arg: A) -> Result<R, Error> {
// runtime.call(self, arg).await
// }
// }
pub struct Wasi {
engine: Engine,
module: Module,
@ -216,13 +239,27 @@ impl Wasi {
Ok(result)
}
pub fn function<A: Serialize, R: DeserializeOwned, T: AsRef<str>>(
&mut self,
name: T,
) -> Result<WasiFn<A, R>, Error> {
let fun_name = format!("__{}", name.as_ref());
let fun = self
.instance
.get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
Ok(WasiFn {
function: fun,
_function_type: PhantomData,
})
}
// TODO: dont' use as for conversions
pub async fn call<A: Serialize, R: DeserializeOwned>(
&mut self,
handle: &str,
handle: &WasiFn<A, R>,
arg: A,
) -> Result<R, Error> {
dbg!(&handle);
// dbg!(&handle.name);
// dbg!(serde_json::to_string(&arg)).unwrap();
// write the argument to linear memory
@ -231,10 +268,11 @@ impl Wasi {
// get the webassembly function we want to actually call
// TODO: precompute handle
let fun_name = format!("__{}", handle);
let fun = self
.instance
.get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
// let fun_name = format!("__{}", handle);
// let fun = self
// .instance
// .get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?;
let fun = handle.function;
// call the function, passing in the buffer and its length
// this returns a ptr to a (ptr, lentgh) pair

View File

@ -10,11 +10,10 @@ use util::ResultExt;
mod c;
mod go;
mod installation;
mod python;
mod language_plugin;
mod python;
mod rust;
mod typescript;
// mod json;
#[derive(RustEmbed)]
#[folder = "src/languages"]
@ -41,6 +40,7 @@ pub async fn init(languages: Arc<LanguageRegistry>, executor: Arc<Background>) {
(
"json",
tree_sitter_json::language(),
// Some(Arc::new(json::JsonLspAdapter)),
language_plugin::new_json(executor)
.await
.log_err()

View File

@ -6,7 +6,7 @@ use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::executor::{self, Background};
use isahc::http::version;
use language::{LanguageServerName, LspAdapter};
use plugin_runtime::{Wasi, WasiPlugin};
use plugin_runtime::{Wasi, WasiFn, WasiPlugin};
use serde_json::json;
use std::fs;
use std::{any::Any, path::PathBuf, sync::Arc};
@ -21,15 +21,30 @@ pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
}
pub struct PluginLspAdapter {
runtime: Arc<Mutex<Wasi>>,
name: WasiFn<(), String>,
server_args: WasiFn<(), Vec<String>>,
fetch_latest_server_version: WasiFn<(), Option<String>>,
fetch_server_binary: WasiFn<(PathBuf, String), Option<PathBuf>>,
cached_server_binary: WasiFn<PathBuf, Option<PathBuf>>,
label_for_completion: WasiFn<String, Option<String>>,
initialization_options: WasiFn<(), String>,
executor: Arc<Background>,
runtime: Arc<Mutex<Wasi>>,
}
impl PluginLspAdapter {
pub async fn new(plugin: WasiPlugin, executor: Arc<Background>) -> Result<Self> {
let mut plugin = Wasi::init(plugin).await?;
Ok(Self {
runtime: Arc::new(Mutex::new(Wasi::init(plugin).await?)),
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)),
})
}
}
@ -49,12 +64,12 @@ macro_rules! call_block {
impl LspAdapter for PluginLspAdapter {
fn name(&self) -> LanguageServerName {
let name: String = call_block!(self, "name", ()).unwrap();
let name: String = call_block!(self, &self.name, ()).unwrap();
LanguageServerName(name.into())
}
fn server_args<'a>(&'a self) -> Vec<String> {
call_block!(self, "server_args", ()).unwrap()
call_block!(self, &self.server_args, ()).unwrap()
}
fn fetch_latest_server_version(
@ -63,11 +78,11 @@ impl LspAdapter for PluginLspAdapter {
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
// let versions: Result<Option<String>> = 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<Option<String>> = runtime
.call::<_, Option<String>>("fetch_latest_server_version", ())
.await;
let versions: Result<Option<String>> =
runtime.call::<_, Option<String>>(&function, ()).await;
versions
.map_err(|e| anyhow!("{}", e))?
.ok_or_else(|| anyhow!("Could not fetch latest server version"))
@ -82,15 +97,13 @@ impl LspAdapter for PluginLspAdapter {
_: Arc<dyn HttpClient>,
container_dir: PathBuf,
) -> BoxFuture<'static, Result<PathBuf>> {
let version = version.downcast::<String>().unwrap();
let version = *version.downcast::<String>().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: Option<PathBuf> = runtime
.call("fetch_server_binary", (container_dir, version))
.await?;
let result: Option<PathBuf> = runtime.call(&function, (container_dir, version)).await?;
runtime.remove_resource(handle)?;
result.ok_or_else(|| anyhow!("Could not load cached server binary"))
}
@ -99,14 +112,12 @@ impl LspAdapter for PluginLspAdapter {
fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
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<PathBuf> = runtime
.call("cached_server_binary", container_dir)
.await
.ok()?;
let result: Option<PathBuf> = runtime.call(&function, container_dir).await.ok()?;
runtime.remove_resource(handle).ok()?;
result
}
@ -120,11 +131,12 @@ impl LspAdapter for PluginLspAdapter {
item: &lsp::CompletionItem,
language: &language::Language,
) -> Option<language::CodeLabel> {
// 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, "label_for_completion", kind).log_err()?;
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(),
@ -134,7 +146,7 @@ impl LspAdapter for PluginLspAdapter {
}
fn initialization_options(&self) -> Option<serde_json::Value> {
let string: String = call_block!(self, "initialization_options", ()).log_err()?;
let string: String = call_block!(self, &self.initialization_options, ()).log_err()?;
serde_json::from_str(&string).ok()
}

View File

@ -165,6 +165,11 @@ fn main() {
app.run(move |cx| {
let client = client::Client::new(http.clone());
let mut languages = LanguageRegistry::new(login_shell_env_loaded);
languages.set_language_server_download_dir(zed::ROOT_PATH.clone());
let languages = Arc::new(languages);
let init_languages = cx
.background()
.spawn(languages::init(languages.clone(), cx.background().clone()));
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
context_menu::init(cx);
@ -209,12 +214,6 @@ fn main() {
})
.detach();
languages.set_language_server_download_dir(zed::ROOT_PATH.clone());
let languages = Arc::new(languages);
cx.background()
.spawn(languages::init(languages.clone(), cx.background().clone()))
.detach();
cx.observe_global::<Settings, _>({
let languages = languages.clone();
move |cx| {
@ -223,6 +222,14 @@ fn main() {
})
.detach();
cx.set_global(settings);
cx.spawn({
let languages = languages.clone();
|cx| async move {
init_languages.await;
cx.read(|cx| languages.set_theme(&cx.global::<Settings>().theme.editor.syntax));
}
})
.detach();
let project_store = cx.add_model(|_| ProjectStore::new(db.clone()));
let app_state = Arc::new(AppState {

View File

@ -6,7 +6,7 @@ use std::path::PathBuf;
// #[import]
fn command(string: &str) -> Option<String> {
todo!()
None
}
// TODO: some sort of macro to generate ABI bindings
@ -113,6 +113,11 @@ pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
}
}
#[bind]
pub fn label_for_completion(label: String) -> Option<String> {
None
}
#[bind]
pub fn initialization_options() -> Option<String> {
Some("{ \"provideFormatter\": true }".to_string())