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