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