Extract ExtensionIndexedDocsProvider
to indexed_docs
crate (#20607)
This PR extracts the `ExtensionIndexedDocsProvider` implementation to the `indexed_docs` crate. To achieve this, we introduce a new `Extension` trait that provides an abstracted interface for calling an extension. This trait resides in the `extension` crate, which has minimal dependencies and can be depended on by other crates, like `indexed_docs`. We're then able to implement the `ExtensionIndexedDocsProvider` without having any knowledge of the Wasm-specific internals of the extension system. Release Notes: - N/A
This commit is contained in:
parent
7832883c74
commit
b084d53f8e
17 changed files with 177 additions and 127 deletions
|
@ -2,6 +2,8 @@ pub mod wit;
|
|||
|
||||
use crate::{ExtensionManifest, ExtensionRegistrationHooks};
|
||||
use anyhow::{anyhow, bail, Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use extension::KeyValueStoreDelegate;
|
||||
use fs::{normalize_path, Fs};
|
||||
use futures::future::LocalBoxFuture;
|
||||
use futures::{
|
||||
|
@ -25,7 +27,7 @@ use wasmtime::{
|
|||
component::{Component, ResourceTable},
|
||||
Engine, Store,
|
||||
};
|
||||
use wasmtime_wasi as wasi;
|
||||
use wasmtime_wasi::{self as wasi, WasiView};
|
||||
use wit::Extension;
|
||||
pub use wit::{ExtensionProject, SlashCommand};
|
||||
|
||||
|
@ -45,10 +47,63 @@ pub struct WasmHost {
|
|||
pub struct WasmExtension {
|
||||
tx: UnboundedSender<ExtensionCall>,
|
||||
pub manifest: Arc<ExtensionManifest>,
|
||||
pub work_dir: Arc<Path>,
|
||||
#[allow(unused)]
|
||||
pub zed_api_version: SemanticVersion,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl extension::Extension for WasmExtension {
|
||||
fn manifest(&self) -> Arc<ExtensionManifest> {
|
||||
self.manifest.clone()
|
||||
}
|
||||
|
||||
fn work_dir(&self) -> Arc<Path> {
|
||||
self.work_dir.clone()
|
||||
}
|
||||
|
||||
async fn suggest_docs_packages(&self, provider: Arc<str>) -> Result<Vec<String>> {
|
||||
self.call(|extension, store| {
|
||||
async move {
|
||||
let packages = extension
|
||||
.call_suggest_docs_packages(store, provider.as_ref())
|
||||
.await?
|
||||
.map_err(|err| anyhow!("{err:?}"))?;
|
||||
|
||||
Ok(packages)
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn index_docs(
|
||||
&self,
|
||||
provider: Arc<str>,
|
||||
package_name: Arc<str>,
|
||||
kv_store: Arc<dyn KeyValueStoreDelegate>,
|
||||
) -> Result<()> {
|
||||
self.call(|extension, store| {
|
||||
async move {
|
||||
let kv_store_resource = store.data_mut().table().push(kv_store)?;
|
||||
extension
|
||||
.call_index_docs(
|
||||
store,
|
||||
provider.as_ref(),
|
||||
package_name.as_ref(),
|
||||
kv_store_resource,
|
||||
)
|
||||
.await?
|
||||
.map_err(|err| anyhow!("{err:?}"))?;
|
||||
|
||||
anyhow::Ok(())
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WasmState {
|
||||
manifest: Arc<ExtensionManifest>,
|
||||
pub table: ResourceTable,
|
||||
|
@ -152,6 +207,7 @@ impl WasmHost {
|
|||
|
||||
Ok(WasmExtension {
|
||||
manifest: manifest.clone(),
|
||||
work_dir: this.work_dir.clone().into(),
|
||||
tx,
|
||||
zed_api_version,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue