diff --git a/crates/plugin_runtime/src/plugin.rs b/crates/plugin_runtime/src/plugin.rs index a1229f8d42..cbcd31ac3a 100644 --- a/crates/plugin_runtime/src/plugin.rs +++ b/crates/plugin_runtime/src/plugin.rs @@ -66,15 +66,11 @@ impl PluginBuilder { /// Create a new [`PluginBuilder`] with the given WASI context. /// Using the default context is a safe bet, see [`new_with_default_context`]. pub fn new(wasi_ctx: WasiCtx) -> Result { - dbg!("new plugin"); let mut config = Config::default(); config.async_support(true); - dbg!("Creating engine"); - let start = std::time::Instant::now(); + config.epoch_interruption(true); let engine = Engine::new(&config)?; - dbg!(start.elapsed()); let linker = Linker::new(&engine); - dbg!(start.elapsed()); Ok(PluginBuilder { // host_functions: HashMap::new(), @@ -317,6 +313,7 @@ impl Plugin { alloc: None, }, ); + // store.epoch_deadline_async_yield_and_update(todo!()); let module = Module::new(&engine, module)?; // load the provided module into the asynchronous runtime diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index eac616a874..47cb2b1e3f 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -31,8 +31,8 @@ use language::{ Transaction, }; use lsp::{ - DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, LanguageString, - MarkedString, + CompletionList, DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, + LanguageString, MarkedString, }; use lsp_command::*; use parking_lot::Mutex; @@ -708,7 +708,7 @@ impl Project { }) } - async fn on_settings_changed(&mut self, cx: &mut ModelContext<'_, Self>) { + fn on_settings_changed(&mut self, cx: &mut ModelContext<'_, Self>) { let settings = cx.global::(); let mut language_servers_to_start = Vec::new(); @@ -3302,15 +3302,15 @@ impl Project { path = relativize_path(&worktree_abs_path, &abs_path); } - let label = this - .languages - .select_language(&path) - .and_then(|language| { + let label = match this.languages.select_language(&path) { + Some(language) => { language .label_for_symbol(&lsp_symbol.name, lsp_symbol.kind) .await - }) - .unwrap_or_else(|| CodeLabel::plain(lsp_symbol.name.clone(), None)); + } + None => None, + } + .unwrap_or_else(|| CodeLabel::plain(lsp_symbol.name.clone(), None)); let signature = this.symbol_signature(worktree_id, &path); Some(Symbol { @@ -3550,15 +3550,18 @@ impl Project { Some(Completion { old_range, new_text, - label: language - .as_ref() - .and_then(|l| l.label_for_completion(&lsp_completion).await) + label: { + match language.as_ref() { + Some(l) => l.label_for_completion(&lsp_completion).await, + None => None, + } .unwrap_or_else(|| { CodeLabel::plain( lsp_completion.label.clone(), lsp_completion.filter_text.as_deref(), ) - }), + }) + }, lsp_completion, }) }) @@ -3582,13 +3585,14 @@ impl Project { }) .await; - response - .completions - .into_iter() - .map(|completion| { - language::proto::deserialize_completion(completion, language.as_ref()).await - }) - .collect() + let completions = Vec::new(); + for completion in response.completions.into_iter() { + completions.push( + language::proto::deserialize_completion(completion, language.as_ref()) + .await, + ); + } + completions.into_iter().collect() }) } else { Task::ready(Ok(Default::default())) @@ -5189,7 +5193,7 @@ impl Project { _: Arc, mut cx: AsyncAppContext, ) -> Result { - let apply_additional_edits = this.update(&mut cx, |this, cx| { + let (buffer, completion) = this.update(&mut cx, |this, cx| { let buffer = this .opened_buffers .get(&envelope.payload.buffer_id) @@ -5202,13 +5206,16 @@ impl Project { .completion .ok_or_else(|| anyhow!("invalid completion"))?, language, - ) - .await?; - Ok::<_, anyhow::Error>( - this.apply_additional_edits_for_completion(buffer, completion, false, cx), - ) + ); + Ok::<_, anyhow::Error>((buffer, completion)) })?; + let completion = completion.await?; + + let apply_additional_edits = this.update(&mut cx, |this, cx| { + this.apply_additional_edits_for_completion(buffer, completion, false, cx) + }); + Ok(proto::ApplyCompletionAdditionalEditsResponse { transaction: apply_additional_edits .await? @@ -5390,8 +5397,10 @@ impl Project { .payload .symbol .ok_or_else(|| anyhow!("invalid symbol"))?; + let symbol = this + .read_with(&cx, |this, _| this.deserialize_symbol(symbol)) + .await?; let symbol = this.read_with(&cx, |this, _| { - let symbol = this.deserialize_symbol(symbol).await?; let signature = this.symbol_signature(symbol.worktree_id, &symbol.path); if signature == symbol.signature { Ok(symbol) diff --git a/crates/zed/src/languages/c.rs b/crates/zed/src/languages/c.rs index 5dc36b1971..838db5ef9d 100644 --- a/crates/zed/src/languages/c.rs +++ b/crates/zed/src/languages/c.rs @@ -13,15 +13,16 @@ use util::{ResultExt, TryFutureExt}; pub struct CLspAdapter; +#[async_trait] impl super::LspAdapter for CLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("clangd".into()) } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, http: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { let release = latest_github_release("clangd/clangd", http).await?; let asset_name = format!("clangd-mac-{}.zip", release.name); @@ -39,12 +40,12 @@ impl super::LspAdapter for CLspAdapter { .boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, version: Box, http: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { let version = version.downcast::().unwrap(); async move { let zip_path = container_dir.join(format!("clangd_{}.zip", version.name)); @@ -92,10 +93,7 @@ impl super::LspAdapter for CLspAdapter { .boxed() } - fn cached_server_binary( - &self, - container_dir: Arc, - ) -> BoxFuture<'static, Option> { + async fn cached_server_binary(&self, container_dir: PathBuf) -> Option { async move { let mut last_clangd_dir = None; let mut entries = fs::read_dir(&container_dir).await?; @@ -120,7 +118,7 @@ impl super::LspAdapter for CLspAdapter { .boxed() } - fn label_for_completion( + async fn label_for_completion( &self, completion: &lsp::CompletionItem, language: &Language, @@ -197,7 +195,7 @@ impl super::LspAdapter for CLspAdapter { Some(CodeLabel::plain(label.to_string(), None)) } - fn label_for_symbol( + async fn label_for_symbol( &self, name: &str, kind: lsp::SymbolKind, diff --git a/crates/zed/src/languages/go.rs b/crates/zed/src/languages/go.rs index 978af215c1..eb88d7f887 100644 --- a/crates/zed/src/languages/go.rs +++ b/crates/zed/src/languages/go.rs @@ -22,19 +22,20 @@ lazy_static! { static ref GOPLS_VERSION_REGEX: Regex = Regex::new(r"\d+\.\d+\.\d+").unwrap(); } +#[async_trait] impl super::LspAdapter for GoLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("gopls".into()) } - fn server_args(&self) -> &[&str] { - &["-mode=stdio"] + async fn server_args(&self) -> Vec { + vec!["-mode=stdio".into()] } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, http: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { let release = latest_github_release("golang/tools", http).await?; let version: Option = release.name.strip_prefix("gopls/v").map(str::to_string); @@ -49,12 +50,12 @@ impl super::LspAdapter for GoLspAdapter { .boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, version: Box, _: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { let version = version.downcast::>().unwrap(); let this = *self; @@ -115,10 +116,7 @@ impl super::LspAdapter for GoLspAdapter { .boxed() } - fn cached_server_binary( - &self, - container_dir: Arc, - ) -> BoxFuture<'static, Option> { + async fn cached_server_binary(&self, container_dir: PathBuf) -> Option { async move { let mut last_binary_path = None; let mut entries = fs::read_dir(&container_dir).await?; @@ -144,7 +142,7 @@ impl super::LspAdapter for GoLspAdapter { .boxed() } - fn label_for_completion( + async fn label_for_completion( &self, completion: &lsp::CompletionItem, language: &Language, @@ -244,7 +242,7 @@ impl super::LspAdapter for GoLspAdapter { None } - fn label_for_symbol( + async fn label_for_symbol( &self, name: &str, kind: lsp::SymbolKind, diff --git a/crates/zed/src/languages/json.rs b/crates/zed/src/languages/json.rs index 7c500da682..6d05f0a63e 100644 --- a/crates/zed/src/languages/json.rs +++ b/crates/zed/src/languages/json.rs @@ -19,31 +19,32 @@ impl JsonLspAdapter { "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver"; } +#[async_trait] impl LspAdapter for JsonLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("vscode-json-languageserver".into()) } - fn server_args(&self) -> Vec { + async fn server_args(&self) -> Vec { vec!["--stdio".into()] } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, _: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { Ok(Box::new(npm_package_latest_version("vscode-json-languageserver").await?) as Box<_>) } .boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, version: Box, _: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { let version = version.downcast::().unwrap(); async move { let version_dir = container_dir.join(version.as_str()); @@ -76,10 +77,7 @@ impl LspAdapter for JsonLspAdapter { .boxed() } - fn cached_server_binary( - &self, - container_dir: Arc, - ) -> BoxFuture<'static, Option> { + async fn cached_server_binary(&self, container_dir: PathBuf) -> Option { async move { let mut last_version_dir = None; let mut entries = fs::read_dir(&container_dir).await?; @@ -104,13 +102,13 @@ impl LspAdapter for JsonLspAdapter { .boxed() } - fn initialization_options(&self) -> Option { + async fn initialization_options(&self) -> Option { Some(json!({ "provideFormatter": true })) } - fn id_for_language(&self, name: &str) -> Option { + async fn id_for_language(&self, name: &str) -> Option { if name == "JSON" { Some("jsonc".into()) } else { diff --git a/crates/zed/src/languages/python.rs b/crates/zed/src/languages/python.rs index 3c0acd0945..d5f857ae05 100644 --- a/crates/zed/src/languages/python.rs +++ b/crates/zed/src/languages/python.rs @@ -17,28 +17,29 @@ impl PythonLspAdapter { const BIN_PATH: &'static str = "node_modules/pyright/langserver.index.js"; } +#[async_trait] impl LspAdapter for PythonLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("pyright".into()) } - fn server_args(&self) -> &[&str] { - &["--stdio"] + async fn server_args(&self) -> Vec { + vec!["--stdio".into()] } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, _: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { Ok(Box::new(npm_package_latest_version("pyright").await?) as Box<_>) }.boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, version: Box, _: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { let version = version.downcast::().unwrap(); async move { let version_dir = container_dir.join(version.as_str()); @@ -67,10 +68,7 @@ impl LspAdapter for PythonLspAdapter { .boxed() } - fn cached_server_binary( - &self, - container_dir: Arc, - ) -> BoxFuture<'static, Option> { + async fn cached_server_binary(&self, container_dir: PathBuf) -> Option { async move { let mut last_version_dir = None; let mut entries = fs::read_dir(&container_dir).await?; @@ -95,7 +93,7 @@ impl LspAdapter for PythonLspAdapter { .boxed() } - fn label_for_completion( + async fn label_for_completion( &self, item: &lsp::CompletionItem, language: &language::Language, @@ -116,7 +114,7 @@ impl LspAdapter for PythonLspAdapter { }) } - fn label_for_symbol( + async fn label_for_symbol( &self, name: &str, kind: lsp::SymbolKind, diff --git a/crates/zed/src/languages/rust.rs b/crates/zed/src/languages/rust.rs index 97c1901a85..c82fc896eb 100644 --- a/crates/zed/src/languages/rust.rs +++ b/crates/zed/src/languages/rust.rs @@ -20,14 +20,14 @@ use util::{ResultExt, TryFutureExt}; pub struct RustLspAdapter; impl LspAdapter for RustLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("rust-analyzer".into()) } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, http: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?; let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH); @@ -45,12 +45,12 @@ impl LspAdapter for RustLspAdapter { .boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, version: Box, http: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { async move { let version = version.downcast::().unwrap(); let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name)); @@ -86,9 +86,9 @@ impl LspAdapter for RustLspAdapter { .boxed() } - fn cached_server_binary( + async fn cached_server_binary( &self, - container_dir: Arc, + container_dir: PathBuf, ) -> BoxFuture<'static, Option> { async move { let mut last = None; @@ -102,15 +102,15 @@ impl LspAdapter for RustLspAdapter { .boxed() } - fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] { - &["rustc"] + async fn disk_based_diagnostic_sources(&self) -> Vec { + vec!["rustc".into()] } - fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> { - Some("rustAnalyzer/cargo check") + async fn disk_based_diagnostics_progress_token(&self) -> Option { + Some("rustAnalyzer/cargo check".into()) } - fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) { + async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) { lazy_static! { static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap(); } @@ -130,7 +130,7 @@ impl LspAdapter for RustLspAdapter { } } - fn label_for_completion( + async fn label_for_completion( &self, completion: &lsp::CompletionItem, language: &Language, @@ -206,7 +206,7 @@ impl LspAdapter for RustLspAdapter { None } - fn label_for_symbol( + async fn label_for_symbol( &self, name: &str, kind: lsp::SymbolKind, diff --git a/crates/zed/src/languages/typescript.rs b/crates/zed/src/languages/typescript.rs index cd5309761d..0bd5ad44a9 100644 --- a/crates/zed/src/languages/typescript.rs +++ b/crates/zed/src/languages/typescript.rs @@ -23,22 +23,23 @@ struct Versions { server_version: String, } +#[async_trait] impl LspAdapter for TypeScriptLspAdapter { - fn name(&self) -> LanguageServerName { + async fn name(&self) -> LanguageServerName { LanguageServerName("typescript-language-server".into()) } - fn server_args(&self) -> Vec { + async fn server_args(&self) -> Vec { ["--stdio", "--tsserver-path", "node_modules/typescript/lib"] .into_iter() .map(str::to_string) .collect() } - fn fetch_latest_server_version( + async fn fetch_latest_server_version( &self, _: Arc, - ) -> BoxFuture<'static, Result>> { + ) -> Result> { async move { Ok(Box::new(Versions { typescript_version: npm_package_latest_version("typescript").await?, @@ -48,12 +49,12 @@ impl LspAdapter for TypeScriptLspAdapter { .boxed() } - fn fetch_server_binary( + async fn fetch_server_binary( &self, versions: Box, _: Arc, - container_dir: Arc, - ) -> BoxFuture<'static, Result> { + container_dir: PathBuf, + ) -> Result { let versions = versions.downcast::().unwrap(); async move { let version_dir = container_dir.join(&format!( @@ -95,10 +96,7 @@ impl LspAdapter for TypeScriptLspAdapter { .boxed() } - fn cached_server_binary( - &self, - container_dir: Arc, - ) -> BoxFuture<'static, Option> { + async fn cached_server_binary(&self, container_dir: PathBuf) -> Option { async move { let mut last_version_dir = None; let mut entries = fs::read_dir(&container_dir).await?; @@ -123,7 +121,7 @@ impl LspAdapter for TypeScriptLspAdapter { .boxed() } - fn label_for_completion( + async fn label_for_completion( &self, item: &lsp::CompletionItem, language: &language::Language, @@ -146,7 +144,7 @@ impl LspAdapter for TypeScriptLspAdapter { }) } - fn initialization_options(&self) -> Option { + async fn initialization_options(&self) -> Option { Some(json!({ "provideFormatter": true }))