Extract HTML support into an extension (#10130)

This PR extracts HTML support into an extension and removes the built-in
HTML support from Zed.

Release Notes:

- Removed built-in support for HTML, in favor of making it available as
an extension. The HTML extension will be suggested for download when you
open a `.html`, `.htm`, or `.shtml` file.
This commit is contained in:
Marshall Bowers 2024-04-03 12:42:36 -04:00 committed by GitHub
parent 256b446bdf
commit 49c53bc0ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 133 additions and 145 deletions

8
Cargo.lock generated
View file

@ -5348,7 +5348,6 @@ dependencies = [
"tree-sitter-gowork",
"tree-sitter-hcl",
"tree-sitter-heex",
"tree-sitter-html",
"tree-sitter-jsdoc",
"tree-sitter-json 0.20.0",
"tree-sitter-lua",
@ -12537,6 +12536,13 @@ dependencies = [
"zed_extension_api 0.0.4",
]
[[package]]
name = "zed_html"
version = "0.0.1"
dependencies = [
"zed_extension_api 0.0.4",
]
[[package]]
name = "zed_php"
version = "0.0.1"

View file

@ -105,6 +105,7 @@ members = [
"extensions/erlang",
"extensions/gleam",
"extensions/haskell",
"extensions/html",
"extensions/php",
"extensions/prisma",
"extensions/purescript",

View file

@ -39,6 +39,9 @@ fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {
("graphql", "gql"),
("graphql", "graphql"),
("haskell", "hs"),
("html", "htm"),
("html", "html"),
("html", "shtml"),
("java", "java"),
("kotlin", "kt"),
("latex", "tex"),

View file

@ -50,7 +50,6 @@ tree-sitter-gomod.workspace = true
tree-sitter-gowork.workspace = true
tree-sitter-hcl.workspace = true
tree-sitter-heex.workspace = true
tree-sitter-html.workspace = true
tree-sitter-jsdoc.workspace = true
tree-sitter-json.workspace = true
tree-sitter-lua.workspace = true

View file

@ -1,134 +0,0 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures::StreamExt;
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use serde_json::json;
use smol::fs;
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::{maybe, ResultExt};
const SERVER_PATH: &str =
"node_modules/vscode-langservers-extracted/bin/vscode-html-language-server";
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
pub struct HtmlLspAdapter {
node: Arc<dyn NodeRuntime>,
}
impl HtmlLspAdapter {
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
HtmlLspAdapter { node }
}
}
#[async_trait(?Send)]
impl LspAdapter for HtmlLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("vscode-html-language-server".into())
}
async fn fetch_latest_server_version(
&self,
_: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Any + Send>> {
Ok(Box::new(
self.node
.npm_package_latest_version("vscode-langservers-extracted")
.await?,
) as Box<_>)
}
async fn fetch_server_binary(
&self,
latest_version: Box<dyn 'static + Send + Any>,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let latest_version = latest_version.downcast::<String>().unwrap();
let server_path = container_dir.join(SERVER_PATH);
let package_name = "vscode-langservers-extracted";
let should_install_language_server = self
.node
.should_install_npm_package(package_name, &server_path, &container_dir, &latest_version)
.await;
if should_install_language_server {
self.node
.npm_install_packages(&container_dir, &[(package_name, latest_version.as_str())])
.await?;
}
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
env: None,
arguments: server_binary_arguments(&server_path),
})
}
async fn cached_server_binary(
&self,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}
async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}
async fn initialization_options(
self: Arc<Self>,
_: &Arc<dyn LspAdapterDelegate>,
) -> Result<Option<serde_json::Value>> {
Ok(Some(json!({
"provideFormatter": true
})))
}
}
async fn get_cached_server_binary(
container_dir: PathBuf,
node: &dyn NodeRuntime,
) -> Option<LanguageServerBinary> {
maybe!(async {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.file_type().await?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let server_path = last_version_dir.join(SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: node.binary_path().await?,
env: None,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
))
}
})
.await
.log_err()
}

View file

@ -18,7 +18,6 @@ mod deno;
mod elixir;
mod elm;
mod go;
mod html;
mod json;
mod lua;
mod nu;
@ -71,7 +70,6 @@ pub fn init(
("gowork", tree_sitter_gowork::language()),
("hcl", tree_sitter_hcl::language()),
("heex", tree_sitter_heex::language()),
("html", tree_sitter_html::language()),
("jsdoc", tree_sitter_jsdoc::language()),
("json", tree_sitter_json::language()),
("lua", tree_sitter_lua::language()),
@ -275,13 +273,6 @@ pub fn init(
}
}
language!(
"html",
vec![
Arc::new(html::HtmlLspAdapter::new(node_runtime.clone())),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
]
);
language!("ruby", vec![Arc::new(ruby::RubyLanguageServer)]);
language!(
"erb",
@ -327,6 +318,10 @@ pub fn init(
"Astro".into(),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
);
languages.register_secondary_lsp_adapter(
"HTML".into(),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),
);
languages.register_secondary_lsp_adapter(
"PHP".into(),
Arc::new(tailwind::TailwindLspAdapter::new(node_runtime.clone())),

View file

@ -0,0 +1,16 @@
[package]
name = "zed_html"
version = "0.0.1"
edition = "2021"
publish = false
license = "Apache-2.0"
[lints]
workspace = true
[lib]
path = "src/html.rs"
crate-type = ["cdylib"]
[dependencies]
zed_extension_api = "0.0.4"

View file

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

View file

@ -0,0 +1,15 @@
id = "html"
name = "HTML"
description = "HTML support."
version = "0.0.1"
schema_version = 1
authors = ["Isaac Clayton <slightknack@gmail.com>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.vscode-html-language-server]
name = "vscode-html-language-server"
language = "HTML"
[grammars.html]
repository = "https://github.com/tree-sitter/tree-sitter-html"
commit = "bfa075d83c6b97cd48440b3829ab8d24a2319809"

View file

@ -0,0 +1,86 @@
use std::{env, fs};
use zed_extension_api::{self as zed, Result};
const SERVER_PATH: &str =
"node_modules/vscode-langservers-extracted/bin/vscode-html-language-server";
const PACKAGE_NAME: &str = "vscode-langservers-extracted";
struct HtmlExtension {
did_find_server: bool,
}
impl HtmlExtension {
fn server_exists(&self) -> bool {
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
}
fn server_script_path(&mut self, config: zed::LanguageServerConfig) -> Result<String> {
let server_exists = self.server_exists();
if self.did_find_server && server_exists {
return Ok(SERVER_PATH.to_string());
}
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;
if !server_exists
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
{
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::Downloading,
);
let result = zed::npm_install_package(PACKAGE_NAME, &version);
match result {
Ok(()) => {
if !self.server_exists() {
Err(format!(
"installed package '{PACKAGE_NAME}' did not contain expected path '{SERVER_PATH}'",
))?;
}
}
Err(error) => {
if !self.server_exists() {
Err(error)?;
}
}
}
}
self.did_find_server = true;
Ok(SERVER_PATH.to_string())
}
}
impl zed::Extension for HtmlExtension {
fn new() -> Self {
Self {
did_find_server: false,
}
}
fn language_server_command(
&mut self,
config: zed::LanguageServerConfig,
_worktree: &zed::Worktree,
) -> Result<zed::Command> {
let server_path = self.server_script_path(config)?;
Ok(zed::Command {
command: zed::node_binary_path()?,
args: vec![
env::current_dir()
.unwrap()
.join(&server_path)
.to_string_lossy()
.to_string(),
"--stdio".to_string(),
],
env: Default::default(),
})
}
}
zed::register_extension!(HtmlExtension);