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