1use super::latest;
2use crate::wasm_host::WasmState;
3use anyhow::Result;
4use async_trait::async_trait;
5use language::LspAdapterDelegate;
6use semantic_version::SemanticVersion;
7use std::sync::{Arc, OnceLock};
8use wasmtime::component::{Linker, Resource};
9
10pub const MIN_VERSION: SemanticVersion = SemanticVersion::new(0, 0, 4);
11
12wasmtime::component::bindgen!({
13 async: true,
14 path: "../extension_api/wit/since_v0.0.4",
15 with: {
16 "worktree": ExtensionWorktree,
17 "zed:extension/github": latest::zed::extension::github,
18 "zed:extension/platform": latest::zed::extension::platform,
19 },
20});
21
22pub type ExtensionWorktree = Arc<dyn LspAdapterDelegate>;
23
24pub fn linker() -> &'static Linker<WasmState> {
25 static LINKER: OnceLock<Linker<WasmState>> = OnceLock::new();
26 LINKER.get_or_init(|| super::new_linker(Extension::add_to_linker))
27}
28
29impl From<DownloadedFileType> for latest::DownloadedFileType {
30 fn from(value: DownloadedFileType) -> Self {
31 match value {
32 DownloadedFileType::Gzip => latest::DownloadedFileType::Gzip,
33 DownloadedFileType::GzipTar => latest::DownloadedFileType::GzipTar,
34 DownloadedFileType::Zip => latest::DownloadedFileType::Zip,
35 DownloadedFileType::Uncompressed => latest::DownloadedFileType::Uncompressed,
36 }
37 }
38}
39
40impl From<LanguageServerInstallationStatus> for latest::LanguageServerInstallationStatus {
41 fn from(value: LanguageServerInstallationStatus) -> Self {
42 match value {
43 LanguageServerInstallationStatus::None => {
44 latest::LanguageServerInstallationStatus::None
45 }
46 LanguageServerInstallationStatus::Downloading => {
47 latest::LanguageServerInstallationStatus::Downloading
48 }
49 LanguageServerInstallationStatus::CheckingForUpdate => {
50 latest::LanguageServerInstallationStatus::CheckingForUpdate
51 }
52 LanguageServerInstallationStatus::Failed(error) => {
53 latest::LanguageServerInstallationStatus::Failed(error)
54 }
55 }
56 }
57}
58
59impl From<Command> for latest::Command {
60 fn from(value: Command) -> Self {
61 Self {
62 command: value.command,
63 args: value.args,
64 env: value.env,
65 }
66 }
67}
68
69#[async_trait]
70impl HostWorktree for WasmState {
71 async fn read_text_file(
72 &mut self,
73 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
74 path: String,
75 ) -> wasmtime::Result<Result<String, String>> {
76 latest::HostWorktree::read_text_file(self, delegate, path).await
77 }
78
79 async fn shell_env(
80 &mut self,
81 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
82 ) -> wasmtime::Result<EnvVars> {
83 latest::HostWorktree::shell_env(self, delegate).await
84 }
85
86 async fn which(
87 &mut self,
88 delegate: Resource<Arc<dyn LspAdapterDelegate>>,
89 binary_name: String,
90 ) -> wasmtime::Result<Option<String>> {
91 latest::HostWorktree::which(self, delegate, binary_name).await
92 }
93
94 fn drop(&mut self, _worktree: Resource<Worktree>) -> Result<()> {
95 // We only ever hand out borrows of worktrees.
96 Ok(())
97 }
98}
99
100#[async_trait]
101impl ExtensionImports for WasmState {
102 async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
103 latest::nodejs::Host::node_binary_path(self).await
104 }
105
106 async fn npm_package_latest_version(
107 &mut self,
108 package_name: String,
109 ) -> wasmtime::Result<Result<String, String>> {
110 latest::nodejs::Host::npm_package_latest_version(self, package_name).await
111 }
112
113 async fn npm_package_installed_version(
114 &mut self,
115 package_name: String,
116 ) -> wasmtime::Result<Result<Option<String>, String>> {
117 latest::nodejs::Host::npm_package_installed_version(self, package_name).await
118 }
119
120 async fn npm_install_package(
121 &mut self,
122 package_name: String,
123 version: String,
124 ) -> wasmtime::Result<Result<(), String>> {
125 latest::nodejs::Host::npm_install_package(self, package_name, version).await
126 }
127
128 async fn latest_github_release(
129 &mut self,
130 repo: String,
131 options: GithubReleaseOptions,
132 ) -> wasmtime::Result<Result<GithubRelease, String>> {
133 latest::zed::extension::github::Host::latest_github_release(self, repo, options).await
134 }
135
136 async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
137 latest::zed::extension::platform::Host::current_platform(self).await
138 }
139
140 async fn set_language_server_installation_status(
141 &mut self,
142 server_name: String,
143 status: LanguageServerInstallationStatus,
144 ) -> wasmtime::Result<()> {
145 latest::ExtensionImports::set_language_server_installation_status(
146 self,
147 server_name,
148 status.into(),
149 )
150 .await
151 }
152
153 async fn download_file(
154 &mut self,
155 url: String,
156 path: String,
157 file_type: DownloadedFileType,
158 ) -> wasmtime::Result<Result<(), String>> {
159 latest::ExtensionImports::download_file(self, url, path, file_type.into()).await
160 }
161
162 async fn make_file_executable(&mut self, path: String) -> wasmtime::Result<Result<(), String>> {
163 latest::ExtensionImports::make_file_executable(self, path).await
164 }
165}