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 .config
.path_suffixes .path_suffixes
.iter() .iter()
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str()))) .any(|suffix| dbg!(path_suffixes.contains(&Some(dbg!(suffix.as_str())))))
}) })
.cloned() .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 anyhow::{anyhow, Error};
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
@ -9,6 +9,29 @@ use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
pub struct WasiResource(u32); 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 { pub struct Wasi {
engine: Engine, engine: Engine,
module: Module, module: Module,
@ -216,13 +239,27 @@ impl Wasi {
Ok(result) 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 // TODO: dont' use as for conversions
pub async fn call<A: Serialize, R: DeserializeOwned>( pub async fn call<A: Serialize, R: DeserializeOwned>(
&mut self, &mut self,
handle: &str, handle: &WasiFn<A, R>,
arg: A, arg: A,
) -> Result<R, Error> { ) -> Result<R, Error> {
dbg!(&handle); // dbg!(&handle.name);
// dbg!(serde_json::to_string(&arg)).unwrap(); // dbg!(serde_json::to_string(&arg)).unwrap();
// write the argument to linear memory // write the argument to linear memory
@ -231,10 +268,11 @@ impl Wasi {
// get the webassembly function we want to actually call // get the webassembly function we want to actually call
// TODO: precompute handle // TODO: precompute handle
let fun_name = format!("__{}", handle); // let fun_name = format!("__{}", handle);
let fun = self // let fun = self
.instance // .instance
.get_typed_func::<(u32, u32), u32, _>(&mut self.store, &fun_name)?; // .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 // call the function, passing in the buffer and its length
// this returns a ptr to a (ptr, lentgh) pair // this returns a ptr to a (ptr, lentgh) pair

View file

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

View file

@ -6,7 +6,7 @@ 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 plugin_runtime::{Wasi, WasiPlugin}; use plugin_runtime::{Wasi, WasiFn, WasiPlugin};
use serde_json::json; use serde_json::json;
use std::fs; use std::fs;
use std::{any::Any, path::PathBuf, sync::Arc}; 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 { 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>, executor: Arc<Background>,
runtime: Arc<Mutex<Wasi>>,
} }
impl PluginLspAdapter { impl PluginLspAdapter {
pub async fn new(plugin: WasiPlugin, executor: Arc<Background>) -> Result<Self> { pub async fn new(plugin: WasiPlugin, executor: Arc<Background>) -> Result<Self> {
let mut plugin = Wasi::init(plugin).await?;
Ok(Self { 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, executor,
runtime: Arc::new(Mutex::new(plugin)),
}) })
} }
} }
@ -49,12 +64,12 @@ macro_rules! call_block {
impl LspAdapter for PluginLspAdapter { impl LspAdapter for PluginLspAdapter {
fn name(&self) -> LanguageServerName { fn name(&self) -> LanguageServerName {
let name: String = call_block!(self, "name", ()).unwrap(); let name: String = call_block!(self, &self.name, ()).unwrap();
LanguageServerName(name.into()) LanguageServerName(name.into())
} }
fn server_args<'a>(&'a self) -> Vec<String> { 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( fn fetch_latest_server_version(
@ -63,11 +78,11 @@ impl LspAdapter for PluginLspAdapter {
) -> 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(); let runtime = self.runtime.clone();
let function = self.fetch_latest_server_version;
async move { async move {
let mut runtime = runtime.lock().await; let mut runtime = runtime.lock().await;
let versions: Result<Option<String>> = runtime let versions: Result<Option<String>> =
.call::<_, Option<String>>("fetch_latest_server_version", ()) runtime.call::<_, Option<String>>(&function, ()).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"))
@ -82,15 +97,13 @@ impl LspAdapter for PluginLspAdapter {
_: Arc<dyn HttpClient>, _: Arc<dyn HttpClient>,
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 runtime = self.runtime.clone(); let runtime = self.runtime.clone();
let function = self.fetch_server_binary;
async move { async move {
let mut runtime = runtime.lock().await; let mut runtime = runtime.lock().await;
let handle = runtime.attach_path(&container_dir)?; let handle = runtime.attach_path(&container_dir)?;
let result: Option<PathBuf> = runtime let result: Option<PathBuf> = runtime.call(&function, (container_dir, version)).await?;
.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"))
} }
@ -99,14 +112,12 @@ impl LspAdapter for PluginLspAdapter {
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 runtime = self.runtime.clone(); let runtime = self.runtime.clone();
let function = self.cached_server_binary;
async move { async move {
let mut runtime = runtime.lock().await; 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> = runtime let result: Option<PathBuf> = runtime.call(&function, container_dir).await.ok()?;
.call("cached_server_binary", container_dir)
.await
.ok()?;
runtime.remove_resource(handle).ok()?; runtime.remove_resource(handle).ok()?;
result result
} }
@ -120,11 +131,12 @@ impl LspAdapter for PluginLspAdapter {
item: &lsp::CompletionItem, item: &lsp::CompletionItem,
language: &language::Language, language: &language::Language,
) -> Option<language::CodeLabel> { ) -> Option<language::CodeLabel> {
// TODO: Push more of this method down into the plugin.
use lsp::CompletionItemKind as Kind; use lsp::CompletionItemKind as Kind;
let len = item.label.len(); let len = item.label.len();
let grammar = language.grammar()?; let grammar = language.grammar()?;
let kind = format!("{:?}", item.kind?); 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)?; let highlight_id = grammar.highlight_id_for_name(&name)?;
Some(language::CodeLabel { Some(language::CodeLabel {
text: item.label.clone(), text: item.label.clone(),
@ -134,7 +146,7 @@ impl LspAdapter for PluginLspAdapter {
} }
fn initialization_options(&self) -> Option<serde_json::Value> { 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() serde_json::from_str(&string).ok()
} }

View file

@ -165,6 +165,11 @@ fn main() {
app.run(move |cx| { app.run(move |cx| {
let client = client::Client::new(http.clone()); let client = client::Client::new(http.clone());
let mut languages = LanguageRegistry::new(login_shell_env_loaded); 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)); let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
context_menu::init(cx); context_menu::init(cx);
@ -209,12 +214,6 @@ fn main() {
}) })
.detach(); .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, _>({ cx.observe_global::<Settings, _>({
let languages = languages.clone(); let languages = languages.clone();
move |cx| { move |cx| {
@ -223,6 +222,14 @@ fn main() {
}) })
.detach(); .detach();
cx.set_global(settings); 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 project_store = cx.add_model(|_| ProjectStore::new(db.clone()));
let app_state = Arc::new(AppState { let app_state = Arc::new(AppState {

View file

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