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