clojure.rs

  1use std::fs;
  2use zed_extension_api::{self as zed, Result};
  3
  4struct ClojureExtension {
  5    cached_binary_path: Option<String>,
  6}
  7
  8impl ClojureExtension {
  9    fn language_server_binary_path(
 10        &mut self,
 11        config: zed::LanguageServerConfig,
 12        worktree: &zed::Worktree,
 13    ) -> Result<String> {
 14        if let Some(path) = &self.cached_binary_path {
 15            if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
 16                return Ok(path.clone());
 17            }
 18        }
 19
 20        if let Some(path) = worktree.which("clojure-lsp") {
 21            self.cached_binary_path = Some(path.clone());
 22            return Ok(path);
 23        }
 24
 25        zed::set_language_server_installation_status(
 26            &config.name,
 27            &zed::LanguageServerInstallationStatus::CheckingForUpdate,
 28        );
 29        let release = zed::latest_github_release(
 30            "clojure-lsp/clojure-lsp",
 31            zed::GithubReleaseOptions {
 32                require_assets: true,
 33                pre_release: false,
 34            },
 35        )?;
 36
 37        let (platform, arch) = zed::current_platform();
 38        let asset_name = format!(
 39            "clojure-lsp-native-{os}-{arch}.zip",
 40            os = match platform {
 41                zed::Os::Mac => "macos",
 42                zed::Os::Linux => "linux",
 43                zed::Os::Windows => "windows",
 44            },
 45            arch = match arch {
 46                zed::Architecture::Aarch64 => "aarch64",
 47                zed::Architecture::X8664 => "amd64",
 48                zed::Architecture::X86 =>
 49                    return Err(format!("unsupported architecture: {arch:?}")),
 50            },
 51        );
 52
 53        let asset = release
 54            .assets
 55            .iter()
 56            .find(|asset| asset.name == asset_name)
 57            .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
 58
 59        let version_dir = format!("clojure-lsp-{}", release.version);
 60        let binary_path = format!("{version_dir}/clojure-lsp");
 61
 62        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
 63            zed::set_language_server_installation_status(
 64                &config.name,
 65                &zed::LanguageServerInstallationStatus::Downloading,
 66            );
 67
 68            zed::download_file(
 69                &asset.download_url,
 70                &version_dir,
 71                zed::DownloadedFileType::Zip,
 72            )
 73            .map_err(|e| format!("failed to download file: {e}"))?;
 74
 75            let entries =
 76                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
 77            for entry in entries {
 78                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
 79                if entry.file_name().to_str() != Some(&version_dir) {
 80                    fs::remove_dir_all(&entry.path()).ok();
 81                }
 82            }
 83        }
 84
 85        self.cached_binary_path = Some(binary_path.clone());
 86        Ok(binary_path)
 87    }
 88}
 89
 90impl zed::Extension for ClojureExtension {
 91    fn new() -> Self {
 92        Self {
 93            cached_binary_path: None,
 94        }
 95    }
 96
 97    fn language_server_command(
 98        &mut self,
 99        config: zed::LanguageServerConfig,
100        worktree: &zed::Worktree,
101    ) -> Result<zed::Command> {
102        Ok(zed::Command {
103            command: self.language_server_binary_path(config, worktree)?,
104            args: Vec::new(),
105            env: Default::default(),
106        })
107    }
108}
109
110zed::register_extension!(ClojureExtension);