1use std::fs;
2use zed::lsp::CompletionKind;
3use zed::{CodeLabel, CodeLabelSpan, LanguageServerId};
4use zed_extension_api::process::Command;
5use zed_extension_api::{self as zed, Result};
6
7struct TestExtension {
8 cached_binary_path: Option<String>,
9}
10
11impl TestExtension {
12 fn language_server_binary_path(
13 &mut self,
14 language_server_id: &LanguageServerId,
15 _worktree: &zed::Worktree,
16 ) -> Result<String> {
17 let echo_output = Command::new("echo").arg("hello!").output()?;
18
19 println!("{}", String::from_utf8_lossy(&echo_output.stdout));
20
21 if let Some(path) = &self.cached_binary_path
22 && fs::metadata(path).map_or(false, |stat| stat.is_file()) {
23 return Ok(path.clone());
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 TestExtension {
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!(TestExtension);
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}