1use std::fs;
2use zed::lsp::CompletionKind;
3use zed::{serde_json, CodeLabel, CodeLabelSpan, LanguageServerId};
4use zed_extension_api::{self as zed, Result};
5
6struct DenoExtension {
7 cached_binary_path: Option<String>,
8}
9
10impl DenoExtension {
11 fn language_server_binary_path(
12 &mut self,
13 language_server_id: &LanguageServerId,
14 worktree: &zed::Worktree,
15 ) -> Result<String> {
16 if let Some(path) = worktree.which("deno") {
17 return Ok(path);
18 }
19
20 if let Some(path) = &self.cached_binary_path {
21 if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
22 return Ok(path.clone());
23 }
24 }
25
26 zed::set_language_server_installation_status(
27 &language_server_id,
28 &zed::LanguageServerInstallationStatus::CheckingForUpdate,
29 );
30 let release = zed::latest_github_release(
31 "denoland/deno",
32 zed::GithubReleaseOptions {
33 require_assets: true,
34 pre_release: false,
35 },
36 )?;
37
38 let (platform, arch) = zed::current_platform();
39 let asset_name = format!(
40 "deno-{arch}-{os}.zip",
41 arch = match arch {
42 zed::Architecture::Aarch64 => "aarch64",
43 zed::Architecture::X8664 => "x86_64",
44 zed::Architecture::X86 =>
45 return Err(format!("unsupported architecture: {arch:?}")),
46 },
47 os = match platform {
48 zed::Os::Mac => "apple-darwin",
49 zed::Os::Linux => "unknown-linux-gnu",
50 zed::Os::Windows => "pc-windows-msvc",
51 },
52 );
53
54 let asset = release
55 .assets
56 .iter()
57 .find(|asset| asset.name == asset_name)
58 .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
59
60 let version_dir = format!("deno-{}", release.version);
61 let binary_path = format!("{version_dir}/deno");
62
63 if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
64 zed::set_language_server_installation_status(
65 &language_server_id,
66 &zed::LanguageServerInstallationStatus::Downloading,
67 );
68
69 zed::download_file(
70 &asset.download_url,
71 &version_dir,
72 zed::DownloadedFileType::Zip,
73 )
74 .map_err(|e| format!("failed to download file: {e}"))?;
75
76 let entries =
77 fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
78 for entry in entries {
79 let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
80 if entry.file_name().to_str() != Some(&version_dir) {
81 fs::remove_dir_all(&entry.path()).ok();
82 }
83 }
84 }
85
86 self.cached_binary_path = Some(binary_path.clone());
87 Ok(binary_path)
88 }
89}
90
91impl zed::Extension for DenoExtension {
92 fn new() -> Self {
93 Self {
94 cached_binary_path: None,
95 }
96 }
97
98 fn language_server_command(
99 &mut self,
100 language_server_id: &LanguageServerId,
101 worktree: &zed::Worktree,
102 ) -> Result<zed::Command> {
103 Ok(zed::Command {
104 command: self.language_server_binary_path(language_server_id, worktree)?,
105 args: vec!["lsp".to_string()],
106 env: Default::default(),
107 })
108 }
109
110 fn language_server_initialization_options(
111 &mut self,
112 _language_server_id: &zed::LanguageServerId,
113 _worktree: &zed::Worktree,
114 ) -> Result<Option<serde_json::Value>> {
115 Ok(Some(serde_json::json!({
116 "provideFormatter": true,
117 })))
118 }
119
120 fn label_for_completion(
121 &self,
122 _language_server_id: &LanguageServerId,
123 completion: zed::lsp::Completion,
124 ) -> Option<CodeLabel> {
125 let highlight_name = match completion.kind? {
126 CompletionKind::Class | CompletionKind::Interface | CompletionKind::Constructor => {
127 "type"
128 }
129 CompletionKind::Constant => "constant",
130 CompletionKind::Function | CompletionKind::Method => "function",
131 CompletionKind::Property | CompletionKind::Field => "property",
132 _ => return None,
133 };
134
135 let len = completion.label.len();
136 let name_span = CodeLabelSpan::literal(completion.label, Some(highlight_name.to_string()));
137
138 Some(zed::CodeLabel {
139 code: Default::default(),
140 spans: if let Some(detail) = completion.detail {
141 vec![
142 name_span,
143 CodeLabelSpan::literal(" ", None),
144 CodeLabelSpan::literal(detail, None),
145 ]
146 } else {
147 vec![name_span]
148 },
149 filter_range: (0..len).into(),
150 })
151 }
152}
153
154zed::register_extension!(DenoExtension);