html: Add support for autoclosing of tags (#11761)
Fixes #5267 TODO: - [x] Publish our fork of vscode-langservers-extracted on GH and wire that through as a language server of choice for HTML extension. - [x] Figure out how to prevent edits made by remote participants from moving the cursor of a host. Release Notes: - Added support for autoclosing of HTML tags in local projects.
This commit is contained in:
parent
097032327d
commit
0b8c1680fb
13 changed files with 173 additions and 75 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "zed_html"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "Apache-2.0"
|
||||
|
@ -13,4 +13,4 @@ path = "src/html.rs"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
zed_extension_api = "0.0.4"
|
||||
zed_extension_api = "0.0.6"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
id = "html"
|
||||
name = "HTML"
|
||||
description = "HTML support."
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
schema_version = 1
|
||||
authors = ["Isaac Clayton <slightknack@gmail.com>"]
|
||||
repository = "https://github.com/zed-industries/zed"
|
||||
|
@ -9,6 +9,15 @@ repository = "https://github.com/zed-industries/zed"
|
|||
[language_servers.vscode-html-language-server]
|
||||
name = "vscode-html-language-server"
|
||||
language = "HTML"
|
||||
languages = ["TypeScript", "HTML", "TSX", "JavaScript", "JSDoc"]
|
||||
|
||||
[language_servers.vscode-html-language-server.language_ids]
|
||||
"HTML" = "html"
|
||||
"PHP" = "php"
|
||||
"ERB" = "eruby"
|
||||
"JavaScript" = "javascriptreact"
|
||||
"TSX" = "typescriptreact"
|
||||
"CSS" = "css"
|
||||
|
||||
[grammars.html]
|
||||
repository = "https://github.com/tree-sitter/tree-sitter-html"
|
||||
|
|
|
@ -8,7 +8,7 @@ brackets = [
|
|||
{ start = "[", end = "]", close = true, newline = true },
|
||||
{ start = "(", end = ")", close = true, newline = true },
|
||||
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["comment", "string"] },
|
||||
{ start = "<", end = ">", close = true, newline = true, not_in = ["comment", "string"] },
|
||||
{ start = "<", end = ">", close = false, newline = true, not_in = ["comment", "string"] },
|
||||
{ start = "!--", end = " --", close = true, newline = false, not_in = ["comment", "string"] },
|
||||
]
|
||||
word_characters = ["-"]
|
||||
|
|
|
@ -1,79 +1,93 @@
|
|||
use std::{env, fs};
|
||||
use std::{env, fs, path::PathBuf};
|
||||
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";
|
||||
const PACKAGE_NAME: &str = "vscode-language-server";
|
||||
|
||||
struct HtmlExtension {
|
||||
did_find_server: bool,
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
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)?;
|
||||
}
|
||||
}
|
||||
fn server_script_path(&self, language_server_id: &zed::LanguageServerId) -> Result<PathBuf> {
|
||||
if let Some(path) = self.path.as_ref() {
|
||||
if fs::metadata(path).map_or(false, |stat| stat.is_dir()) {
|
||||
return Ok(path.clone());
|
||||
}
|
||||
}
|
||||
|
||||
self.did_find_server = true;
|
||||
Ok(SERVER_PATH.to_string())
|
||||
zed::set_language_server_installation_status(
|
||||
language_server_id,
|
||||
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
|
||||
);
|
||||
let release = zed::latest_github_release(
|
||||
"zed-industries/vscode-langservers-extracted",
|
||||
zed::GithubReleaseOptions {
|
||||
require_assets: true,
|
||||
pre_release: false,
|
||||
},
|
||||
)?;
|
||||
|
||||
let asset_name = "vscode-language-server.tar.gz";
|
||||
|
||||
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!("{}-{}", PACKAGE_NAME, release.version);
|
||||
if !fs::metadata(&version_dir).map_or(false, |stat| stat.is_dir()) {
|
||||
zed::set_language_server_installation_status(
|
||||
&language_server_id,
|
||||
&zed::LanguageServerInstallationStatus::Downloading,
|
||||
);
|
||||
|
||||
zed::download_file(
|
||||
&asset.download_url,
|
||||
&version_dir,
|
||||
zed::DownloadedFileType::GzipTar,
|
||||
)
|
||||
.map_err(|e| format!("failed to download file: {e}"))?;
|
||||
|
||||
let entries =
|
||||
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
|
||||
if entry.file_name().to_str() != Some(&version_dir) {
|
||||
fs::remove_dir_all(&entry.path()).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(PathBuf::from(version_dir)
|
||||
.join("bin")
|
||||
.join("vscode-html-language-server"))
|
||||
}
|
||||
}
|
||||
|
||||
impl zed::Extension for HtmlExtension {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
did_find_server: false,
|
||||
}
|
||||
Self { path: None }
|
||||
}
|
||||
|
||||
fn language_server_command(
|
||||
&mut self,
|
||||
config: zed::LanguageServerConfig,
|
||||
language_server_id: &zed::LanguageServerId,
|
||||
_worktree: &zed::Worktree,
|
||||
) -> Result<zed::Command> {
|
||||
let server_path = self.server_script_path(config)?;
|
||||
let path = match &self.path {
|
||||
Some(path) => path,
|
||||
None => {
|
||||
let path = self.server_script_path(language_server_id)?;
|
||||
self.path = Some(path);
|
||||
self.path.as_ref().unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
Ok(zed::Command {
|
||||
command: zed::node_binary_path()?,
|
||||
args: vec![
|
||||
env::current_dir()
|
||||
.unwrap()
|
||||
.join(&server_path)
|
||||
.join(path)
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
"--stdio".to_string(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue