since_v0_2_0.rs

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