since_v0_2_0.rs

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