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