Move Wasi to async, validate timeslicing, using async in traits still WIP
This commit is contained in:
parent
e9b87f3dc3
commit
8bce35d1e9
5 changed files with 144 additions and 115 deletions
|
@ -1,7 +1,11 @@
|
|||
use gpui::Task;
|
||||
use gpui::{
|
||||
executor::{self, Background},
|
||||
Task,
|
||||
};
|
||||
pub use language::*;
|
||||
use rust_embed::RustEmbed;
|
||||
use std::{borrow::Cow, str, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
mod c;
|
||||
mod go;
|
||||
|
@ -17,8 +21,7 @@ mod typescript;
|
|||
#[exclude = "*.rs"]
|
||||
struct LanguageDir;
|
||||
|
||||
pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegistry {
|
||||
let languages = LanguageRegistry::new(login_shell_env_loaded);
|
||||
pub async fn init(languages: Arc<LanguageRegistry>, executor: Arc<Background>) {
|
||||
for (name, grammar, lsp_adapter) in [
|
||||
(
|
||||
"c",
|
||||
|
@ -38,7 +41,10 @@ pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegi
|
|||
(
|
||||
"json",
|
||||
tree_sitter_json::language(),
|
||||
Some(Arc::new(language_plugin::new_json())),
|
||||
language_plugin::new_json(executor)
|
||||
.await
|
||||
.log_err()
|
||||
.map(|lang| Arc::new(lang) as Arc<_>),
|
||||
),
|
||||
(
|
||||
"markdown",
|
||||
|
@ -78,7 +84,6 @@ pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegi
|
|||
] {
|
||||
languages.add(Arc::new(language(name, grammar, lsp_adapter)));
|
||||
}
|
||||
languages
|
||||
}
|
||||
|
||||
pub(crate) fn language(
|
||||
|
|
|
@ -2,6 +2,7 @@ use super::installation::{npm_install_packages, npm_package_latest_version};
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use gpui::executor::{self, Background};
|
||||
use isahc::http::version;
|
||||
use language::{LanguageServerName, LspAdapter};
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
|
@ -11,23 +12,25 @@ use std::fs;
|
|||
use std::{any::Any, path::PathBuf, sync::Arc};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
|
||||
pub fn new_json() -> LanguagePluginLspAdapter {
|
||||
pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
|
||||
let plugin = WasiPlugin {
|
||||
module: include_bytes!("../../../../plugins/bin/json_language.wasm").to_vec(),
|
||||
wasi_ctx: Wasi::default_ctx(),
|
||||
};
|
||||
LanguagePluginLspAdapter::new(plugin)
|
||||
PluginLspAdapter::new(plugin, executor).await
|
||||
}
|
||||
|
||||
pub struct LanguagePluginLspAdapter {
|
||||
runtime: Mutex<Wasi>,
|
||||
pub struct PluginLspAdapter {
|
||||
runtime: Arc<Mutex<Wasi>>,
|
||||
executor: Arc<Background>,
|
||||
}
|
||||
|
||||
impl LanguagePluginLspAdapter {
|
||||
pub fn new(plugin: WasiPlugin) -> Self {
|
||||
Self {
|
||||
runtime: Mutex::new(Wasi::init(plugin).unwrap()),
|
||||
}
|
||||
impl PluginLspAdapter {
|
||||
pub async fn new(plugin: WasiPlugin, executor: Arc<Background>) -> Result<Self> {
|
||||
Ok(Self {
|
||||
runtime: Arc::new(Mutex::new(Wasi::init(plugin).await?)),
|
||||
executor,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,32 +39,43 @@ struct Versions {
|
|||
server_version: String,
|
||||
}
|
||||
|
||||
impl LspAdapter for LanguagePluginLspAdapter {
|
||||
macro_rules! call_block {
|
||||
($self:ident, $name:expr, $arg:expr) => {
|
||||
$self
|
||||
.executor
|
||||
.block(async { $self.runtime.lock().call($name, $arg).await })
|
||||
};
|
||||
}
|
||||
|
||||
impl LspAdapter for PluginLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
let name: String = self.runtime.lock().call("name", ()).unwrap();
|
||||
let name: String = call_block!(self, "name", ()).unwrap();
|
||||
LanguageServerName(name.into())
|
||||
}
|
||||
|
||||
fn server_args<'a>(&'a self) -> Vec<String> {
|
||||
self.runtime.lock().call("server_args", ()).unwrap()
|
||||
call_block!(self, "server_args", ()).unwrap()
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
&self,
|
||||
_: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||
let versions: Result<(String, String)> =
|
||||
self.runtime.lock().call("fetch_latest_server_version", ());
|
||||
|
||||
async move {
|
||||
versions.map(|(language_version, server_version)| {
|
||||
Box::new(Versions {
|
||||
language_version,
|
||||
server_version,
|
||||
}) as Box<_>
|
||||
})
|
||||
}
|
||||
.boxed()
|
||||
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()
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
|
@ -70,34 +84,37 @@ impl LspAdapter for LanguagePluginLspAdapter {
|
|||
_: Arc<dyn HttpClient>,
|
||||
container_dir: PathBuf,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
let version = version.downcast::<String>().unwrap();
|
||||
let mut runtime = self.runtime.lock();
|
||||
todo!()
|
||||
// let version = version.downcast::<String>().unwrap();
|
||||
|
||||
let result: Result<PathBuf, _> = (|| {
|
||||
let handle = runtime.attach_path(&container_dir)?;
|
||||
let result = runtime
|
||||
.call::<_, Option<PathBuf>>("fetch_server_binary", container_dir)?
|
||||
.ok_or_else(|| anyhow!("Could not load cached server binary"));
|
||||
runtime.remove_resource(handle)?;
|
||||
result
|
||||
})();
|
||||
|
||||
async move { result }.boxed()
|
||||
// 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()
|
||||
}
|
||||
|
||||
fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
let mut runtime = self.runtime.lock();
|
||||
|
||||
let result: Option<PathBuf> = (|| {
|
||||
let handle = runtime.attach_path(&container_dir).ok()?;
|
||||
let result = runtime
|
||||
.call::<_, Option<PathBuf>>("cached_server_binary", container_dir)
|
||||
.ok()?;
|
||||
runtime.remove_resource(handle).ok()?;
|
||||
result
|
||||
})();
|
||||
|
||||
async move { result }.boxed()
|
||||
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()
|
||||
}
|
||||
|
||||
fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
|
||||
|
@ -111,11 +128,7 @@ impl LspAdapter for LanguagePluginLspAdapter {
|
|||
let len = item.label.len();
|
||||
let grammar = language.grammar()?;
|
||||
let kind = format!("{:?}", item.kind?);
|
||||
let name: String = self
|
||||
.runtime
|
||||
.lock()
|
||||
.call("label_for_completion", kind)
|
||||
.ok()?;
|
||||
let name: String = call_block!(self, "label_for_completion", kind).log_err()?;
|
||||
let highlight_id = grammar.highlight_id_for_name(&name)?;
|
||||
Some(language::CodeLabel {
|
||||
text: item.label.clone(),
|
||||
|
@ -125,11 +138,7 @@ impl LspAdapter for LanguagePluginLspAdapter {
|
|||
}
|
||||
|
||||
fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||
let string = self
|
||||
.runtime
|
||||
.lock()
|
||||
.call::<_, Option<String>>("initialization_options", ())
|
||||
.unwrap()?;
|
||||
let string: String = call_block!(self, "initialization_options", ()).log_err()?;
|
||||
|
||||
serde_json::from_str(&string).ok()
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ use futures::{
|
|||
};
|
||||
use gpui::{executor::Background, App, AssetSource, AsyncAppContext, Task};
|
||||
use isahc::{config::Configurable, AsyncBody, Request};
|
||||
use language::LanguageRegistry;
|
||||
use log::LevelFilter;
|
||||
use parking_lot::Mutex;
|
||||
use project::{Fs, ProjectStore};
|
||||
|
@ -163,7 +164,7 @@ fn main() {
|
|||
|
||||
app.run(move |cx| {
|
||||
let client = client::Client::new(http.clone());
|
||||
let mut languages = languages::build_language_registry(login_shell_env_loaded);
|
||||
let mut languages = LanguageRegistry::new(login_shell_env_loaded);
|
||||
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
|
||||
|
||||
context_menu::init(cx);
|
||||
|
@ -210,6 +211,9 @@ fn main() {
|
|||
|
||||
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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue