From d074586fbff00da025f05bcff83073a7716cbac2 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Thu, 28 Mar 2024 18:40:12 -0400 Subject: [PATCH] Extract TOML support into an extension (#9940) This PR extracts TOML support into an extension and removes the built-in TOML support from Zed. There's a small workaround necessary in order for us to set the file permissions on the `taplo` binary so that it can be run. Eventually we'll want to build this into the extension API, but for now we're just hard-coding it on the host side. Release Notes: - Removed built-in support for TOML, in favor of making it available as an extension. The TOML extension will be suggested for download when you open a `.toml` or `Cargo.lock` file. --- Cargo.lock | 17 +-- Cargo.toml | 2 +- crates/extension/src/extension_lsp_adapter.rs | 2 +- crates/extensions_ui/src/extension_suggest.rs | 2 + crates/languages/Cargo.toml | 1 - crates/languages/src/lib.rs | 3 - crates/languages/src/toml.rs | 126 ------------------ extensions/toml/Cargo.toml | 16 +++ extensions/toml/LICENSE-APACHE | 1 + extensions/toml/extension.toml | 18 +++ .../toml/languages}/toml/brackets.scm | 0 .../toml/languages}/toml/config.toml | 0 .../toml/languages}/toml/highlights.scm | 0 .../toml/languages}/toml/indents.scm | 0 .../toml/languages}/toml/outline.scm | 0 .../toml/languages}/toml/overrides.scm | 0 .../toml/languages}/toml/redactions.scm | 0 extensions/toml/src/toml.rs | 110 +++++++++++++++ 18 files changed, 156 insertions(+), 142 deletions(-) delete mode 100644 crates/languages/src/toml.rs create mode 100644 extensions/toml/Cargo.toml create mode 120000 extensions/toml/LICENSE-APACHE create mode 100644 extensions/toml/extension.toml rename {crates/languages/src => extensions/toml/languages}/toml/brackets.scm (100%) rename {crates/languages/src => extensions/toml/languages}/toml/config.toml (100%) rename {crates/languages/src => extensions/toml/languages}/toml/highlights.scm (100%) rename {crates/languages/src => extensions/toml/languages}/toml/indents.scm (100%) rename {crates/languages/src => extensions/toml/languages}/toml/outline.scm (100%) rename {crates/languages/src => extensions/toml/languages}/toml/overrides.scm (100%) rename {crates/languages/src => extensions/toml/languages}/toml/redactions.scm (100%) create mode 100644 extensions/toml/src/toml.rs diff --git a/Cargo.lock b/Cargo.lock index a3883f4d7a..7253bd082a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5462,7 +5462,6 @@ dependencies = [ "tree-sitter-ruby", "tree-sitter-rust", "tree-sitter-scheme", - "tree-sitter-toml", "tree-sitter-typescript", "tree-sitter-vue", "tree-sitter-yaml", @@ -10651,15 +10650,6 @@ dependencies = [ "tree-sitter", ] -[[package]] -name = "tree-sitter-toml" -version = "0.5.1" -source = "git+https://github.com/tree-sitter/tree-sitter-toml?rev=342d9be207c2dba869b9967124c679b5e6fd0ebe#342d9be207c2dba869b9967124c679b5e6fd0ebe" -dependencies = [ - "cc", - "tree-sitter", -] - [[package]] name = "tree-sitter-typescript" version = "0.20.2" @@ -12725,6 +12715,13 @@ dependencies = [ "zed_extension_api 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "zed_toml" +version = "0.0.1" +dependencies = [ + "zed_extension_api 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "zed_uiua" version = "0.0.1" diff --git a/Cargo.toml b/Cargo.toml index 5859c994ec..54a1ef9cac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,6 +102,7 @@ members = [ "extensions/prisma", "extensions/purescript", "extensions/svelte", + "extensions/toml", "extensions/uiua", "extensions/zig", @@ -324,7 +325,6 @@ tree-sitter-regex = "0.20.0" tree-sitter-ruby = "0.20.0" tree-sitter-rust = "0.20.3" tree-sitter-scheme = { git = "https://github.com/6cdh/tree-sitter-scheme", rev = "af0fd1fa452cb2562dc7b5c8a8c55551c39273b9" } -tree-sitter-toml = { git = "https://github.com/tree-sitter/tree-sitter-toml", rev = "342d9be207c2dba869b9967124c679b5e6fd0ebe" } tree-sitter-typescript = { git = "https://github.com/tree-sitter/tree-sitter-typescript", rev = "5d20856f34315b068c41edaee2ac8a100081d259" } tree-sitter-vue = { git = "https://github.com/zed-industries/tree-sitter-vue", rev = "6608d9d60c386f19d80af7d8132322fa11199c42" } tree-sitter-yaml = { git = "https://github.com/zed-industries/tree-sitter-yaml", rev = "f545a41f57502e1b5ddf2a6668896c1b0620f930" } diff --git a/crates/extension/src/extension_lsp_adapter.rs b/crates/extension/src/extension_lsp_adapter.rs index 4e7a949044..19a6b75ec9 100644 --- a/crates/extension/src/extension_lsp_adapter.rs +++ b/crates/extension/src/extension_lsp_adapter.rs @@ -58,7 +58,7 @@ impl LspAdapter for ExtensionLspAdapter { // TODO: Eventually we'll want to expose an extension API for doing this, but for // now we just manually set the file permissions for extensions that we know need it. - if self.extension.manifest.id.as_ref() == "zig" { + if ["toml", "zig"].contains(&self.extension.manifest.id.as_ref()) { #[cfg(not(windows))] { use std::fs::{self, Permissions}; diff --git a/crates/extensions_ui/src/extension_suggest.rs b/crates/extensions_ui/src/extension_suggest.rs index 326315dc69..eb1c941eb6 100644 --- a/crates/extensions_ui/src/extension_suggest.rs +++ b/crates/extensions_ui/src/extension_suggest.rs @@ -46,6 +46,8 @@ pub fn suggested_extension(file_extension_or_name: &str) -> Option> { ("sql", "sql"), ("svelte", "svelte"), ("swift", "swift"), + ("toml", "Cargo.lock"), + ("toml", "toml"), ("templ", "templ"), ("wgsl", "wgsl"), ("zig", "zig"), diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml index f9f36f4621..78afaf6559 100644 --- a/crates/languages/Cargo.toml +++ b/crates/languages/Cargo.toml @@ -69,7 +69,6 @@ tree-sitter-regex.workspace = true tree-sitter-ruby.workspace = true tree-sitter-rust.workspace = true tree-sitter-scheme.workspace = true -tree-sitter-toml.workspace = true tree-sitter-typescript.workspace = true tree-sitter-vue.workspace = true tree-sitter-yaml.workspace = true diff --git a/crates/languages/src/lib.rs b/crates/languages/src/lib.rs index 23471528af..ce80bfa9de 100644 --- a/crates/languages/src/lib.rs +++ b/crates/languages/src/lib.rs @@ -32,7 +32,6 @@ mod ruby; mod rust; mod tailwind; mod terraform; -mod toml; mod typescript; mod vue; mod yaml; @@ -99,7 +98,6 @@ pub fn init( ("ruby", tree_sitter_ruby::language()), ("rust", tree_sitter_rust::language()), ("scheme", tree_sitter_scheme::language()), - ("toml", tree_sitter_toml::language()), ("tsx", tree_sitter_typescript::language_tsx()), ("typescript", tree_sitter_typescript::language_typescript()), ("vue", tree_sitter_vue::language()), @@ -236,7 +234,6 @@ pub fn init( vec![Arc::new(rust::RustLspAdapter)], RustContextProvider ); - language!("toml", vec![Arc::new(toml::TaploLspAdapter)]); match &DenoSettings::get(None, cx).enable { true => { language!( diff --git a/crates/languages/src/toml.rs b/crates/languages/src/toml.rs deleted file mode 100644 index a488064f4d..0000000000 --- a/crates/languages/src/toml.rs +++ /dev/null @@ -1,126 +0,0 @@ -use anyhow::{bail, Context, Result}; -use async_compression::futures::bufread::GzipDecoder; -use async_trait::async_trait; -use futures::{io::BufReader, StreamExt}; -use language::{LanguageServerName, LspAdapter, LspAdapterDelegate}; -use lsp::LanguageServerBinary; -use smol::fs::{self, File}; -use std::{any::Any, path::PathBuf}; -use util::github::latest_github_release; -use util::maybe; -use util::{github::GitHubLspBinaryVersion, ResultExt}; - -pub struct TaploLspAdapter; - -#[async_trait(?Send)] -impl LspAdapter for TaploLspAdapter { - fn name(&self) -> LanguageServerName { - LanguageServerName("taplo-ls".into()) - } - - async fn fetch_latest_server_version( - &self, - delegate: &dyn LspAdapterDelegate, - ) -> Result> { - let release = - latest_github_release("tamasfe/taplo", true, false, delegate.http_client()).await?; - let asset_name = format!( - "taplo-full-{os}-{arch}.gz", - os = match std::env::consts::OS { - "macos" => "darwin", - "linux" => "linux", - "windows" => "windows", - other => bail!("Running on unsupported os: {other}"), - }, - arch = std::env::consts::ARCH - ); - - let asset = release - .assets - .iter() - .find(|asset| asset.name == asset_name) - .context(format!("no asset found matching {asset_name:?}"))?; - - Ok(Box::new(GitHubLspBinaryVersion { - name: release.tag_name, - url: asset.browser_download_url.clone(), - })) - } - - async fn fetch_server_binary( - &self, - version: Box, - container_dir: PathBuf, - delegate: &dyn LspAdapterDelegate, - ) -> Result { - let version = version.downcast::().unwrap(); - let binary_path = container_dir.join("taplo"); - - if fs::metadata(&binary_path).await.is_err() { - let mut response = delegate - .http_client() - .get(&version.url, Default::default(), true) - .await - .context("error downloading release")?; - - let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut())); - let mut file = File::create(&binary_path).await?; - - futures::io::copy(decompressed_bytes, &mut file).await?; - - // todo("windows") - #[cfg(not(windows))] - { - fs::set_permissions( - &binary_path, - ::from_mode(0o755), - ) - .await?; - } - } - - Ok(LanguageServerBinary { - path: binary_path, - env: None, - arguments: vec!["lsp".into(), "stdio".into()], - }) - } - - async fn cached_server_binary( - &self, - container_dir: PathBuf, - _: &dyn LspAdapterDelegate, - ) -> Option { - get_cached_server_binary(container_dir).await - } - - async fn installation_test_binary( - &self, - container_dir: PathBuf, - ) -> Option { - get_cached_server_binary(container_dir) - .await - .map(|mut binary| { - binary.arguments = vec!["--help".into()]; - binary - }) - } -} - -async fn get_cached_server_binary(container_dir: PathBuf) -> Option { - maybe!(async { - let mut last = None; - let mut entries = fs::read_dir(&container_dir).await?; - while let Some(entry) = entries.next().await { - last = Some(entry?.path()); - } - - anyhow::Ok(LanguageServerBinary { - path: last.context("no cached binary")?, - env: None, - arguments: Default::default(), - }) - }) - .await - .log_err() -} diff --git a/extensions/toml/Cargo.toml b/extensions/toml/Cargo.toml new file mode 100644 index 0000000000..2a9fa721f1 --- /dev/null +++ b/extensions/toml/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "zed_toml" +version = "0.0.1" +edition = "2021" +publish = false +license = "Apache-2.0" + +[lints] +workspace = true + +[lib] +path = "src/toml.rs" +crate-type = ["cdylib"] + +[dependencies] +zed_extension_api = "0.0.4" diff --git a/extensions/toml/LICENSE-APACHE b/extensions/toml/LICENSE-APACHE new file mode 120000 index 0000000000..1cd601d0a3 --- /dev/null +++ b/extensions/toml/LICENSE-APACHE @@ -0,0 +1 @@ +../../LICENSE-APACHE \ No newline at end of file diff --git a/extensions/toml/extension.toml b/extensions/toml/extension.toml new file mode 100644 index 0000000000..b14319a36e --- /dev/null +++ b/extensions/toml/extension.toml @@ -0,0 +1,18 @@ +id = "toml" +name = "TOML" +description = "TOML support." +version = "0.0.2" +schema_version = 1 +authors = [ + "Max Brunsfeld ", + "Ammar Arif " +] +repository = "https://github.com/zed-industries/zed" + +[language_servers.taplo] +name = "Taplo" +language = "TOML" + +[grammars.toml] +repository = "https://github.com/tree-sitter/tree-sitter-toml" +commit = "342d9be207c2dba869b9967124c679b5e6fd0ebe" diff --git a/crates/languages/src/toml/brackets.scm b/extensions/toml/languages/toml/brackets.scm similarity index 100% rename from crates/languages/src/toml/brackets.scm rename to extensions/toml/languages/toml/brackets.scm diff --git a/crates/languages/src/toml/config.toml b/extensions/toml/languages/toml/config.toml similarity index 100% rename from crates/languages/src/toml/config.toml rename to extensions/toml/languages/toml/config.toml diff --git a/crates/languages/src/toml/highlights.scm b/extensions/toml/languages/toml/highlights.scm similarity index 100% rename from crates/languages/src/toml/highlights.scm rename to extensions/toml/languages/toml/highlights.scm diff --git a/crates/languages/src/toml/indents.scm b/extensions/toml/languages/toml/indents.scm similarity index 100% rename from crates/languages/src/toml/indents.scm rename to extensions/toml/languages/toml/indents.scm diff --git a/crates/languages/src/toml/outline.scm b/extensions/toml/languages/toml/outline.scm similarity index 100% rename from crates/languages/src/toml/outline.scm rename to extensions/toml/languages/toml/outline.scm diff --git a/crates/languages/src/toml/overrides.scm b/extensions/toml/languages/toml/overrides.scm similarity index 100% rename from crates/languages/src/toml/overrides.scm rename to extensions/toml/languages/toml/overrides.scm diff --git a/crates/languages/src/toml/redactions.scm b/extensions/toml/languages/toml/redactions.scm similarity index 100% rename from crates/languages/src/toml/redactions.scm rename to extensions/toml/languages/toml/redactions.scm diff --git a/extensions/toml/src/toml.rs b/extensions/toml/src/toml.rs new file mode 100644 index 0000000000..a4fb1c8d4d --- /dev/null +++ b/extensions/toml/src/toml.rs @@ -0,0 +1,110 @@ +use std::fs; +use zed_extension_api::{self as zed, Result}; + +struct TomlExtension { + cached_binary_path: Option, +} + +impl TomlExtension { + fn language_server_binary_path(&mut self, config: zed::LanguageServerConfig) -> Result { + if let Some(path) = &self.cached_binary_path { + if fs::metadata(path).map_or(false, |stat| stat.is_file()) { + return Ok(path.clone()); + } + } + + zed::set_language_server_installation_status( + &config.name, + &zed::LanguageServerInstallationStatus::CheckingForUpdate, + ); + let release = zed::latest_github_release( + "tamasfe/taplo", + zed::GithubReleaseOptions { + require_assets: true, + pre_release: false, + }, + )?; + + let (platform, arch) = zed::current_platform(); + let asset_name = format!( + "taplo-full-{os}-{arch}.{extension}", + arch = match arch { + zed::Architecture::Aarch64 => "aarch64", + zed::Architecture::X86 => "x86", + zed::Architecture::X8664 => "x86_64", + }, + os = match platform { + zed::Os::Mac => "darwin", + zed::Os::Linux => "linux", + zed::Os::Windows => "windows", + }, + extension = match platform { + zed::Os::Mac | zed::Os::Linux => "gz", + zed::Os::Windows => "zip", + } + ); + + let asset = release + .assets + .iter() + .find(|asset| asset.name == asset_name) + .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?; + + let version_dir = format!("taplo-{}", release.version); + fs::create_dir_all(&version_dir) + .map_err(|err| format!("failed to create directory '{version_dir}': {err}"))?; + + let binary_path = format!("{version_dir}/taplo"); + + if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) { + zed::set_language_server_installation_status( + &config.name, + &zed::LanguageServerInstallationStatus::Downloading, + ); + + zed::download_file( + &asset.download_url, + &binary_path, + match platform { + zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::Gzip, + zed::Os::Windows => zed::DownloadedFileType::Zip, + }, + ) + .map_err(|err| format!("failed to download file: {err}"))?; + + let entries = fs::read_dir(".") + .map_err(|err| format!("failed to list working directory {err}"))?; + for entry in entries { + let entry = entry.map_err(|err| format!("failed to load directory entry {err}"))?; + if entry.file_name().to_str() != Some(&version_dir) { + fs::remove_dir_all(&entry.path()).ok(); + } + } + } + + self.cached_binary_path = Some(binary_path.clone()); + Ok(binary_path) + } +} + +impl zed::Extension for TomlExtension { + fn new() -> Self { + Self { + cached_binary_path: None, + } + } + + fn language_server_command( + &mut self, + config: zed::LanguageServerConfig, + _worktree: &zed::Worktree, + ) -> Result { + Ok(zed::Command { + command: self.language_server_binary_path(config)?, + args: vec!["lsp".to_string(), "stdio".to_string()], + env: Default::default(), + }) + } +} + +zed::register_extension!(TomlExtension);