since_v0_0_6.rs

  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, 6);
 11
 12wasmtime::component::bindgen!({
 13    async: true,
 14    trappable_imports: true,
 15    path: "../extension_api/wit/since_v0.0.6",
 16    with: {
 17         "worktree": ExtensionWorktree,
 18         "zed:extension/github": latest::zed::extension::github,
 19         "zed:extension/lsp": latest::zed::extension::lsp,
 20         "zed:extension/nodejs": latest::zed::extension::nodejs,
 21         "zed:extension/platform": latest::zed::extension::platform,
 22    },
 23});
 24
 25mod settings {
 26    include!(concat!(env!("OUT_DIR"), "/since_v0.0.6/settings.rs"));
 27}
 28
 29pub type ExtensionWorktree = Arc<dyn LspAdapterDelegate>;
 30
 31pub fn linker() -> &'static Linker<WasmState> {
 32    static LINKER: OnceLock<Linker<WasmState>> = OnceLock::new();
 33    LINKER.get_or_init(|| {
 34        super::new_linker(|linker, f| {
 35            Extension::add_to_linker(linker, f)?;
 36            latest::zed::extension::github::add_to_linker(linker, f)?;
 37            latest::zed::extension::nodejs::add_to_linker(linker, f)?;
 38            latest::zed::extension::platform::add_to_linker(linker, f)?;
 39            Ok(())
 40        })
 41    })
 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
122#[async_trait]
123impl HostWorktree for WasmState {
124    async fn id(
125        &mut self,
126        delegate: Resource<Arc<dyn LspAdapterDelegate>>,
127    ) -> wasmtime::Result<u64> {
128        latest::HostWorktree::id(self, delegate).await
129    }
130
131    async fn root_path(
132        &mut self,
133        delegate: Resource<Arc<dyn LspAdapterDelegate>>,
134    ) -> wasmtime::Result<String> {
135        latest::HostWorktree::root_path(self, delegate).await
136    }
137
138    async fn read_text_file(
139        &mut self,
140        delegate: Resource<Arc<dyn LspAdapterDelegate>>,
141        path: String,
142    ) -> wasmtime::Result<Result<String, String>> {
143        latest::HostWorktree::read_text_file(self, delegate, path).await
144    }
145
146    async fn shell_env(
147        &mut self,
148        delegate: Resource<Arc<dyn LspAdapterDelegate>>,
149    ) -> wasmtime::Result<EnvVars> {
150        latest::HostWorktree::shell_env(self, delegate).await
151    }
152
153    async fn which(
154        &mut self,
155        delegate: Resource<Arc<dyn LspAdapterDelegate>>,
156        binary_name: String,
157    ) -> wasmtime::Result<Option<String>> {
158        latest::HostWorktree::which(self, delegate, binary_name).await
159    }
160
161    fn drop(&mut self, _worktree: Resource<Worktree>) -> Result<()> {
162        // We only ever hand out borrows of worktrees.
163        Ok(())
164    }
165}
166
167#[async_trait]
168impl ExtensionImports for WasmState {
169    async fn get_settings(
170        &mut self,
171        location: Option<self::SettingsLocation>,
172        category: String,
173        key: Option<String>,
174    ) -> wasmtime::Result<Result<String, String>> {
175        latest::ExtensionImports::get_settings(
176            self,
177            location.map(|location| location.into()),
178            category,
179            key,
180        )
181        .await
182    }
183
184    async fn set_language_server_installation_status(
185        &mut self,
186        server_name: String,
187        status: LanguageServerInstallationStatus,
188    ) -> wasmtime::Result<()> {
189        latest::ExtensionImports::set_language_server_installation_status(
190            self,
191            server_name,
192            status.into(),
193        )
194        .await
195    }
196
197    async fn download_file(
198        &mut self,
199        url: String,
200        path: String,
201        file_type: DownloadedFileType,
202    ) -> wasmtime::Result<Result<(), String>> {
203        latest::ExtensionImports::download_file(self, url, path, file_type.into()).await
204    }
205
206    async fn make_file_executable(&mut self, path: String) -> wasmtime::Result<Result<(), String>> {
207        latest::ExtensionImports::make_file_executable(self, path).await
208    }
209}