1use std::fs;
2use zed::lsp::CompletionKind;
3use zed::{CodeLabel, CodeLabelSpan, LanguageServerId};
4use zed_extension_api::{self as zed, Result};
5
6struct GleamExtension {
7 cached_binary_path: Option<String>,
8}
9
10impl GleamExtension {
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("gleam") {
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 "gleam-lang/gleam",
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 "gleam-{version}-{arch}-{os}.tar.gz",
41 version = release.version,
42 arch = match arch {
43 zed::Architecture::Aarch64 => "aarch64",
44 zed::Architecture::X86 => "x86",
45 zed::Architecture::X8664 => "x86_64",
46 },
47 os = match platform {
48 zed::Os::Mac => "apple-darwin",
49 zed::Os::Linux => "unknown-linux-musl",
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!("gleam-{}", release.version);
61 let binary_path = format!("{version_dir}/gleam");
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::GzipTar,
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 GleamExtension {
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 label_for_completion(
111 &self,
112 _language_server_id: &LanguageServerId,
113 completion: zed::lsp::Completion,
114 ) -> Option<zed::CodeLabel> {
115 let name = &completion.label;
116 let ty = strip_newlines_from_detail(&completion.detail?);
117 let let_binding = "let a";
118 let colon = ": ";
119 let assignment = " = ";
120 let call = match completion.kind? {
121 CompletionKind::Function | CompletionKind::Constructor => "()",
122 _ => "",
123 };
124 let code = format!("{let_binding}{colon}{ty}{assignment}{name}{call}");
125
126 Some(CodeLabel {
127 spans: vec![
128 CodeLabelSpan::code_range({
129 let start = let_binding.len() + colon.len() + ty.len() + assignment.len();
130 start..start + name.len()
131 }),
132 CodeLabelSpan::code_range({
133 let start = let_binding.len();
134 start..start + colon.len()
135 }),
136 CodeLabelSpan::code_range({
137 let start = let_binding.len() + colon.len();
138 start..start + ty.len()
139 }),
140 ],
141 filter_range: (0..name.len()).into(),
142 code,
143 })
144 }
145}
146
147zed::register_extension!(GleamExtension);
148
149/// Removes newlines from the completion detail.
150///
151/// The Gleam LSP can return types containing newlines, which causes formatting
152/// issues within the Zed completions menu.
153fn strip_newlines_from_detail(detail: &str) -> String {
154 let without_newlines = detail
155 .replace("->\n ", "-> ")
156 .replace("\n ", "")
157 .replace(",\n", "");
158
159 let comma_delimited_parts = without_newlines.split(',');
160 comma_delimited_parts
161 .map(|part| part.trim())
162 .collect::<Vec<_>>()
163 .join(", ")
164}
165
166#[cfg(test)]
167mod tests {
168 use crate::strip_newlines_from_detail;
169
170 #[test]
171 fn test_strip_newlines_from_detail() {
172 let detail = "fn(\n Selector(a),\n b,\n fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a,\n) -> Selector(a)";
173 let expected = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) -> Selector(a)";
174 assert_eq!(strip_newlines_from_detail(detail), expected);
175
176 let detail = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) ->\n Selector(a)";
177 let expected = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) -> Selector(a)";
178 assert_eq!(strip_newlines_from_detail(detail), expected);
179
180 let detail = "fn(\n Method,\n List(#(String, String)),\n a,\n Scheme,\n String,\n Option(Int),\n String,\n Option(String),\n) -> Request(a)";
181 let expected = "fn(Method, List(#(String, String)), a, Scheme, String, Option(Int), String, Option(String)) -> Request(a)";
182 assert_eq!(strip_newlines_from_detail(&detail), expected);
183 }
184}