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