since_v0_2_0.rs

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