|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use zed_extension_api::{self as zed, Result}; |
| 4 | + |
| 5 | +// The bin at ~Library/Application Support/Zed/extensions/work/{NAME} |
| 6 | +// this does not need to be a constant, and may choose to resolve it in the #language_server_binary_path method |
| 7 | +const HDX_BIN_PATH: &str = "hdx"; |
| 8 | + |
| 9 | +struct HdxExtension; |
| 10 | + |
| 11 | +impl HdxExtension { |
| 12 | + fn language_server_binary_path( |
| 13 | + &mut self, |
| 14 | + language_server_id: &zed_extension_api::LanguageServerId, |
| 15 | + worktree: &zed::Worktree, |
| 16 | + ) -> Result<String> { |
| 17 | + if let Ok(path) = env::var("HDX_SERVER_PATH") { |
| 18 | + if fs::metadata(&path).map_or(false, |stat| stat.is_file()) { |
| 19 | + return Ok(path.to_string()); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + if let Some(path) = worktree.which("hdx") { |
| 24 | + return Ok(path); |
| 25 | + } |
| 26 | + |
| 27 | + zed::set_language_server_installation_status( |
| 28 | + language_server_id, |
| 29 | + &zed::LanguageServerInstallationStatus::CheckingForUpdate, |
| 30 | + ); |
| 31 | + |
| 32 | + let release = zed::github_release_by_tag_name("keithamus/hdx", "canary")?; |
| 33 | + |
| 34 | + let (platform, arch) = zed::current_platform(); |
| 35 | + let asset_name = format!( |
| 36 | + "hdx-{platform}-{arch}", |
| 37 | + platform = match platform { |
| 38 | + zed::Os::Mac => "darwin", |
| 39 | + zed::Os::Linux => "linux", |
| 40 | + zed::Os::Windows => "win32", |
| 41 | + }, |
| 42 | + arch = match arch { |
| 43 | + zed::Architecture::Aarch64 => "arm64", |
| 44 | + zed::Architecture::X8664 => "x64", |
| 45 | + _ => return Err(format!("unsupported architecture: {arch:?}")), |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + let asset = release |
| 50 | + .assets |
| 51 | + .iter() |
| 52 | + .find(|asset| asset.name == asset_name) |
| 53 | + .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?; |
| 54 | + |
| 55 | + if !fs::metadata(&asset_name).map_or(false, |stat| stat.is_file()) { |
| 56 | + zed::set_language_server_installation_status( |
| 57 | + language_server_id, |
| 58 | + &zed::LanguageServerInstallationStatus::Downloading, |
| 59 | + ); |
| 60 | + |
| 61 | + zed::download_file(&asset.download_url, HDX_BIN_PATH, zed::DownloadedFileType::Uncompressed) |
| 62 | + .map_err(|e| format!("failed to download file: {e}"))?; |
| 63 | + |
| 64 | + zed::make_file_executable(HDX_BIN_PATH).map_err(|e| format!("failed to make file executable: {e}"))?; |
| 65 | + } |
| 66 | + |
| 67 | + Ok(HDX_BIN_PATH.to_string()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl zed::Extension for HdxExtension { |
| 72 | + fn new() -> Self { |
| 73 | + Self |
| 74 | + } |
| 75 | + |
| 76 | + fn language_server_command( |
| 77 | + &mut self, |
| 78 | + language_server_id: &zed_extension_api::LanguageServerId, |
| 79 | + worktree: &zed_extension_api::Worktree, |
| 80 | + ) -> zed_extension_api::Result<zed_extension_api::Command> { |
| 81 | + let settings = zed_extension_api::settings::LspSettings::for_worktree(language_server_id.as_ref(), worktree)?; |
| 82 | + |
| 83 | + let mut args = vec![]; |
| 84 | + |
| 85 | + if let Some(settings) = settings.settings { |
| 86 | + let is_debug = settings.get("debug").and_then(|value| value.as_bool()).unwrap_or(false); |
| 87 | + |
| 88 | + if is_debug { |
| 89 | + args.push("--debug".to_string()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + args.push("lsp".to_string()); |
| 94 | + |
| 95 | + Ok(zed::Command { |
| 96 | + command: self.language_server_binary_path(language_server_id, worktree)?, |
| 97 | + args, |
| 98 | + env: Default::default(), |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +zed::register_extension!(HdxExtension); |
0 commit comments