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