gleam.rs

  1mod hexdocs;
  2
  3use std::{fs, io};
  4use zed::lsp::CompletionKind;
  5use zed::{
  6    CodeLabel, CodeLabelSpan, HttpMethod, HttpRequest, KeyValueStore, LanguageServerId,
  7    SlashCommand, SlashCommandArgumentCompletion, SlashCommandOutput, SlashCommandOutputSection,
  8};
  9use zed_extension_api::{self as zed, Result};
 10
 11use crate::hexdocs::convert_hexdocs_to_markdown;
 12
 13struct GleamExtension {
 14    cached_binary_path: Option<String>,
 15}
 16
 17impl GleamExtension {
 18    fn language_server_binary_path(
 19        &mut self,
 20        language_server_id: &LanguageServerId,
 21        worktree: &zed::Worktree,
 22    ) -> Result<String> {
 23        if let Some(path) = worktree.which("gleam") {
 24            return Ok(path);
 25        }
 26
 27        if let Some(path) = &self.cached_binary_path {
 28            if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
 29                return Ok(path.clone());
 30            }
 31        }
 32
 33        zed::set_language_server_installation_status(
 34            &language_server_id,
 35            &zed::LanguageServerInstallationStatus::CheckingForUpdate,
 36        );
 37        let release = zed::latest_github_release(
 38            "gleam-lang/gleam",
 39            zed::GithubReleaseOptions {
 40                require_assets: true,
 41                pre_release: false,
 42            },
 43        )?;
 44
 45        let (platform, arch) = zed::current_platform();
 46        let asset_name = format!(
 47            "gleam-{version}-{arch}-{os}.tar.gz",
 48            version = release.version,
 49            arch = match arch {
 50                zed::Architecture::Aarch64 => "aarch64",
 51                zed::Architecture::X86 => "x86",
 52                zed::Architecture::X8664 => "x86_64",
 53            },
 54            os = match platform {
 55                zed::Os::Mac => "apple-darwin",
 56                zed::Os::Linux => "unknown-linux-musl",
 57                zed::Os::Windows => "pc-windows-msvc",
 58            },
 59        );
 60
 61        let asset = release
 62            .assets
 63            .iter()
 64            .find(|asset| asset.name == asset_name)
 65            .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
 66
 67        let version_dir = format!("gleam-{}", release.version);
 68        let binary_path = format!("{version_dir}/gleam");
 69
 70        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
 71            zed::set_language_server_installation_status(
 72                &language_server_id,
 73                &zed::LanguageServerInstallationStatus::Downloading,
 74            );
 75
 76            zed::download_file(
 77                &asset.download_url,
 78                &version_dir,
 79                zed::DownloadedFileType::GzipTar,
 80            )
 81            .map_err(|e| format!("failed to download file: {e}"))?;
 82
 83            let entries =
 84                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
 85            for entry in entries {
 86                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
 87                if entry.file_name().to_str() != Some(&version_dir) {
 88                    fs::remove_dir_all(&entry.path()).ok();
 89                }
 90            }
 91        }
 92
 93        self.cached_binary_path = Some(binary_path.clone());
 94        Ok(binary_path)
 95    }
 96}
 97
 98impl zed::Extension for GleamExtension {
 99    fn new() -> Self {
100        Self {
101            cached_binary_path: None,
102        }
103    }
104
105    fn language_server_command(
106        &mut self,
107        language_server_id: &LanguageServerId,
108        worktree: &zed::Worktree,
109    ) -> Result<zed::Command> {
110        Ok(zed::Command {
111            command: self.language_server_binary_path(language_server_id, worktree)?,
112            args: vec!["lsp".to_string()],
113            env: Default::default(),
114        })
115    }
116
117    fn label_for_completion(
118        &self,
119        _language_server_id: &LanguageServerId,
120        completion: zed::lsp::Completion,
121    ) -> Option<zed::CodeLabel> {
122        let name = &completion.label;
123        let ty = strip_newlines_from_detail(&completion.detail?);
124        let let_binding = "let a";
125        let colon = ": ";
126        let assignment = " = ";
127        let call = match completion.kind? {
128            CompletionKind::Function | CompletionKind::Constructor => "()",
129            _ => "",
130        };
131        let code = format!("{let_binding}{colon}{ty}{assignment}{name}{call}");
132
133        Some(CodeLabel {
134            spans: vec![
135                CodeLabelSpan::code_range({
136                    let start = let_binding.len() + colon.len() + ty.len() + assignment.len();
137                    start..start + name.len()
138                }),
139                CodeLabelSpan::code_range({
140                    let start = let_binding.len();
141                    start..start + colon.len()
142                }),
143                CodeLabelSpan::code_range({
144                    let start = let_binding.len() + colon.len();
145                    start..start + ty.len()
146                }),
147            ],
148            filter_range: (0..name.len()).into(),
149            code,
150        })
151    }
152
153    fn complete_slash_command_argument(
154        &self,
155        command: SlashCommand,
156        _query: String,
157    ) -> Result<Vec<SlashCommandArgumentCompletion>, String> {
158        match command.name.as_str() {
159            "gleam-project" => Ok(vec![
160                SlashCommandArgumentCompletion {
161                    label: "apple".to_string(),
162                    new_text: "Apple".to_string(),
163                    run_command: false,
164                },
165                SlashCommandArgumentCompletion {
166                    label: "banana".to_string(),
167                    new_text: "Banana".to_string(),
168                    run_command: false,
169                },
170                SlashCommandArgumentCompletion {
171                    label: "cherry".to_string(),
172                    new_text: "Cherry".to_string(),
173                    run_command: true,
174                },
175            ]),
176            _ => Ok(Vec::new()),
177        }
178    }
179
180    fn run_slash_command(
181        &self,
182        command: SlashCommand,
183        argument: Option<String>,
184        worktree: Option<&zed::Worktree>,
185    ) -> Result<SlashCommandOutput, String> {
186        match command.name.as_str() {
187            "gleam-docs" => {
188                let argument = argument.ok_or_else(|| "missing argument".to_string())?;
189
190                let mut components = argument.split('/');
191                let package_name = components
192                    .next()
193                    .ok_or_else(|| "missing package name".to_string())?;
194                let module_path = components.map(ToString::to_string).collect::<Vec<_>>();
195
196                let response = zed::fetch(&HttpRequest {
197                    method: HttpMethod::Get,
198                    url: format!(
199                        "https://hexdocs.pm/{package_name}{maybe_path}",
200                        maybe_path = if !module_path.is_empty() {
201                            format!("/{}.html", module_path.join("/"))
202                        } else {
203                            String::new()
204                        }
205                    ),
206                    headers: vec![(
207                        "User-Agent".to_string(),
208                        "Zed (Gleam Extension)".to_string(),
209                    )],
210                    body: None,
211                })?;
212
213                let (markdown, _modules) =
214                    convert_hexdocs_to_markdown(&mut io::Cursor::new(response.body))?;
215
216                let mut text = String::new();
217                text.push_str(&markdown);
218
219                Ok(SlashCommandOutput {
220                    sections: vec![SlashCommandOutputSection {
221                        range: (0..text.len()).into(),
222                        label: format!("gleam-docs: {package_name} {}", module_path.join("/")),
223                    }],
224                    text,
225                })
226            }
227            "gleam-project" => {
228                let worktree = worktree.ok_or_else(|| "no worktree")?;
229
230                let mut text = String::new();
231                text.push_str("You are in a Gleam project.\n");
232
233                if let Some(gleam_toml) = worktree.read_text_file("gleam.toml").ok() {
234                    text.push_str("The `gleam.toml` is as follows:\n");
235                    text.push_str(&gleam_toml);
236                }
237
238                Ok(SlashCommandOutput {
239                    sections: vec![SlashCommandOutputSection {
240                        range: (0..text.len()).into(),
241                        label: "gleam-project".to_string(),
242                    }],
243                    text,
244                })
245            }
246            command => Err(format!("unknown slash command: \"{command}\"")),
247        }
248    }
249
250    fn index_docs(
251        &self,
252        provider: String,
253        package: String,
254        database: &KeyValueStore,
255    ) -> Result<(), String> {
256        match provider.as_str() {
257            "gleam-hexdocs" => hexdocs::index(package, database),
258            _ => Ok(()),
259        }
260    }
261}
262
263zed::register_extension!(GleamExtension);
264
265/// Removes newlines from the completion detail.
266///
267/// The Gleam LSP can return types containing newlines, which causes formatting
268/// issues within the Zed completions menu.
269fn strip_newlines_from_detail(detail: &str) -> String {
270    let without_newlines = detail
271        .replace("->\n  ", "-> ")
272        .replace("\n  ", "")
273        .replace(",\n", "");
274
275    let comma_delimited_parts = without_newlines.split(',');
276    comma_delimited_parts
277        .map(|part| part.trim())
278        .collect::<Vec<_>>()
279        .join(", ")
280}
281
282#[cfg(test)]
283mod tests {
284    use crate::strip_newlines_from_detail;
285
286    #[test]
287    fn test_strip_newlines_from_detail() {
288        let detail = "fn(\n  Selector(a),\n  b,\n  fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a,\n) -> Selector(a)";
289        let expected = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) -> Selector(a)";
290        assert_eq!(strip_newlines_from_detail(detail), expected);
291
292        let detail = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) ->\n  Selector(a)";
293        let expected = "fn(Selector(a), b, fn(Dynamic, Dynamic, Dynamic, Dynamic, Dynamic, Dynamic) -> a) -> Selector(a)";
294        assert_eq!(strip_newlines_from_detail(detail), expected);
295
296        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)";
297        let expected = "fn(Method, List(#(String, String)), a, Scheme, String, Option(Int), String, Option(String)) -> Request(a)";
298        assert_eq!(strip_newlines_from_detail(&detail), expected);
299    }
300}