This PR updates the Emmet extension to use the `index.js` file directly to launch the language server. This provides better cross-platform support, as we're not relying on platform-specific `.bin` wrappers. Release Notes: - N/A
107 lines
3.3 KiB
Rust
107 lines
3.3 KiB
Rust
use std::{env, fs};
|
|
use zed_extension_api::{self as zed, Result};
|
|
|
|
struct EmmetExtension {
|
|
did_find_server: bool,
|
|
}
|
|
|
|
const SERVER_PATH: &str = "node_modules/@olrtg/emmet-language-server/dist/index.js";
|
|
const PACKAGE_NAME: &str = "@olrtg/emmet-language-server";
|
|
|
|
impl EmmetExtension {
|
|
fn server_exists(&self) -> bool {
|
|
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
|
|
}
|
|
|
|
fn server_script_path(&mut self, language_server_id: &zed::LanguageServerId) -> 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(
|
|
language_server_id,
|
|
&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(
|
|
language_server_id,
|
|
&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 EmmetExtension {
|
|
fn new() -> Self {
|
|
Self {
|
|
did_find_server: false,
|
|
}
|
|
}
|
|
|
|
fn language_server_command(
|
|
&mut self,
|
|
language_server_id: &zed::LanguageServerId,
|
|
_worktree: &zed::Worktree,
|
|
) -> Result<zed::Command> {
|
|
let server_path = self.server_script_path(language_server_id)?;
|
|
Ok(zed::Command {
|
|
command: zed::node_binary_path()?,
|
|
args: vec![
|
|
zed_ext::sanitize_windows_path(env::current_dir().unwrap())
|
|
.join(&server_path)
|
|
.to_string_lossy()
|
|
.to_string(),
|
|
"--stdio".to_string(),
|
|
],
|
|
env: Default::default(),
|
|
})
|
|
}
|
|
}
|
|
|
|
zed::register_extension!(EmmetExtension);
|
|
|
|
/// Extensions to the Zed extension API that have not yet stabilized.
|
|
mod zed_ext {
|
|
/// Sanitizes the given path to remove the leading `/` on Windows.
|
|
///
|
|
/// On macOS and Linux this is a no-op.
|
|
///
|
|
/// This is a workaround for https://github.com/bytecodealliance/wasmtime/issues/10415.
|
|
pub fn sanitize_windows_path(path: std::path::PathBuf) -> std::path::PathBuf {
|
|
use zed_extension_api::{Os, current_platform};
|
|
|
|
let (os, _arch) = current_platform();
|
|
match os {
|
|
Os::Mac | Os::Linux => path,
|
|
Os::Windows => path
|
|
.to_string_lossy()
|
|
.to_string()
|
|
.trim_start_matches('/')
|
|
.into(),
|
|
}
|
|
}
|
|
}
|