1use super::latest;
2use crate::wasm_host::WasmState;
3use anyhow::Result;
4use async_trait::async_trait;
5use language::LspAdapterDelegate;
6use semantic_version::SemanticVersion;
7use std::sync::{Arc, OnceLock};
8use wasmtime::component::{Linker, Resource};
9
10pub const MIN_VERSION: SemanticVersion = SemanticVersion::new(0, 0, 6);
11pub const MAX_VERSION: SemanticVersion = SemanticVersion::new(0, 0, 6);
12
13wasmtime::component::bindgen!({
14 async: true,
15 trappable_imports: true,
16 path: "../extension_api/wit/since_v0.0.6",
17 with: {
18 "worktree": ExtensionWorktree,
19 "zed:extension/github": latest::zed::extension::github,
20 "zed:extension/lsp": latest::zed::extension::lsp,
21 "zed:extension/nodejs": latest::zed::extension::nodejs,
22 "zed:extension/platform": latest::zed::extension::platform,
23 },
24});
25
26mod settings {
27 include!("../../../../extension_api/wit/since_v0.0.6/settings.rs");
28}
29
30pub type ExtensionWorktree = Arc<dyn LspAdapterDelegate>;
31
32pub fn linker() -> &'static Linker<WasmState> {
33 static LINKER: OnceLock<Linker<WasmState>> = OnceLock::new();
34 LINKER.get_or_init(|| {
35 super::new_linker(|linker, f| {
36 Extension::add_to_linker(linker, f)?;
37 latest::zed::extension::github::add_to_linker(linker, f)?;
38 latest::zed::extension::nodejs::add_to_linker(linker, f)?;
39 latest::zed::extension::platform::add_to_linker(linker, f)?;
40 Ok(())
41 })
42 })
43}
44
45impl From<Command> for latest::Command {
46 fn from(value: Command) -> Self {
47 Self {
48 command: value.command,
49 args: value.args,
50 env: value.env,
51 }
52 }
53}
54
55impl From<SettingsLocation> for latest::SettingsLocation {
56 fn from(value: SettingsLocation) -> Self {
57 Self {
58 worktree_id: value.worktree_id,
59 path: value.path,
60 }
61 }
62}
63
64impl From<LanguageServerInstallationStatus> for latest::LanguageServerInstallationStatus {
65 fn from(value: LanguageServerInstallationStatus) -> Self {
66 match value {
67 LanguageServerInstallationStatus::None => Self::None,
68 LanguageServerInstallationStatus::Downloading => Self::Downloading,
69 LanguageServerInstallationStatus::CheckingForUpdate => Self::CheckingForUpdate,
70 LanguageServerInstallationStatus::Failed(message) => Self::Failed(message),
71 }
72 }
73}
74
75impl From<DownloadedFileType> for latest::DownloadedFileType {
76 fn from(value: DownloadedFileType) -> Self {
77 match value {
78 DownloadedFileType::Gzip => Self::Gzip,
79 DownloadedFileType::GzipTar => Self::GzipTar,
80 DownloadedFileType::Zip => Self::Zip,
81 DownloadedFileType::Uncompressed => Self::Uncompressed,
82 }
83 }
84}
85
86impl From<Range> for latest::Range {
87 fn from(value: Range) -> Self {
88 Self {
89 start: value.start,
90 end: value.end,
91 }
92 }
93}
94
95impl From<CodeLabelSpan> for latest::CodeLabelSpan {
96 fn from(value: CodeLabelSpan) -> Self {
97 match value {
98 CodeLabelSpan::CodeRange(range) => Self::CodeRange(range.into()),
99 CodeLabelSpan::Literal(literal) => Self::Literal(literal.into()),
100 }
101 }
102}
103
104impl From<CodeLabelSpanLiteral> for latest::CodeLabelSpanLiteral {
105 fn from(value: CodeLabelSpanLiteral) -> Self {
106 Self {
107 text: value.text,
108 highlight_name: value.highlight_name,
109 }
110 }
111}
112
113impl From<CodeLabel> for latest::CodeLabel {
114 fn from(value: CodeLabel) -> Self {
115 Self {
116 code: value.code,
117 spans: value.spans.into_iter().map(Into::into).collect(),
118 filter_range: value.filter_range.into(),
119 }
120 }
121}
122
123#[async_trait]
124impl HostWorktree for WasmState {
125 async fn id(
126 &mut self,
127 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
128 ) -> wasmtime::Result<u64> {
129 latest::HostWorktree::id(self, delegate).await
130 }
131
132 async fn root_path(
133 &mut self,
134 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
135 ) -> wasmtime::Result<String> {
136 latest::HostWorktree::root_path(self, delegate).await
137 }
138
139 async fn read_text_file(
140 &mut self,
141 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
142 path: String,
143 ) -> wasmtime::Result<Result<String, String>> {
144 latest::HostWorktree::read_text_file(self, delegate, path).await
145 }
146
147 async fn shell_env(
148 &mut self,
149 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
150 ) -> wasmtime::Result<EnvVars> {
151 latest::HostWorktree::shell_env(self, delegate).await
152 }
153
154 async fn which(
155 &mut self,
156 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
157 binary_name: String,
158 ) -> wasmtime::Result<Option<String>> {
159 latest::HostWorktree::which(self, delegate, binary_name).await
160 }
161
162 fn drop(&mut self, _worktree: Resource<Worktree>) -> Result<()> {
163 // We only ever hand out borrows of worktrees.
164 Ok(())
165 }
166}
167
168#[async_trait]
169impl ExtensionImports for WasmState {
170 async fn get_settings(
171 &mut self,
172 location: Option<self::SettingsLocation>,
173 category: String,
174 key: Option<String>,
175 ) -> wasmtime::Result<Result<String, String>> {
176 latest::ExtensionImports::get_settings(
177 self,
178 location.map(|location| location.into()),
179 category,
180 key,
181 )
182 .await
183 }
184
185 async fn set_language_server_installation_status(
186 &mut self,
187 server_name: String,
188 status: LanguageServerInstallationStatus,
189 ) -> wasmtime::Result<()> {
190 latest::ExtensionImports::set_language_server_installation_status(
191 self,
192 server_name,
193 status.into(),
194 )
195 .await
196 }
197
198 async fn download_file(
199 &mut self,
200 url: String,
201 path: String,
202 file_type: DownloadedFileType,
203 ) -> wasmtime::Result<Result<(), String>> {
204 latest::ExtensionImports::download_file(self, url, path, file_type.into()).await
205 }
206
207 async fn make_file_executable(&mut self, path: String) -> wasmtime::Result<Result<(), String>> {
208 latest::ExtensionImports::make_file_executable(self, path).await
209 }
210}