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.
This commit is contained in:
Marshall Bowers 2024-03-28 18:40:12 -04:00 committed by GitHub
parent 90cf73b746
commit d074586fbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 156 additions and 142 deletions

17
Cargo.lock generated
View File

@ -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"

View File

@ -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" }

View File

@ -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};

View File

@ -46,6 +46,8 @@ pub fn suggested_extension(file_extension_or_name: &str) -> Option<Arc<str>> {
("sql", "sql"),
("svelte", "svelte"),
("swift", "swift"),
("toml", "Cargo.lock"),
("toml", "toml"),
("templ", "templ"),
("wgsl", "wgsl"),
("zig", "zig"),

View File

@ -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

View File

@ -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!(

View File

@ -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<Box<dyn 'static + Send + Any>> {
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<dyn 'static + Send + Any>,
container_dir: PathBuf,
delegate: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<GitHubLspBinaryVersion>().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,
<fs::Permissions as fs::unix::PermissionsExt>::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<LanguageServerBinary> {
get_cached_server_binary(container_dir).await
}
async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
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<LanguageServerBinary> {
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()
}

View File

@ -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"

View File

@ -0,0 +1 @@
../../LICENSE-APACHE

View File

@ -0,0 +1,18 @@
id = "toml"
name = "TOML"
description = "TOML support."
version = "0.0.2"
schema_version = 1
authors = [
"Max Brunsfeld <max@zed.dev>",
"Ammar Arif <evergreenkary@gmail.com>"
]
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"

110
extensions/toml/src/toml.rs Normal file
View File

@ -0,0 +1,110 @@
use std::fs;
use zed_extension_api::{self as zed, Result};
struct TomlExtension {
cached_binary_path: Option<String>,
}
impl TomlExtension {
fn language_server_binary_path(&mut self, config: zed::LanguageServerConfig) -> Result<String> {
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<zed::Command> {
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);