Remove duplicated code for unchanged parts of different extension API versions (#10218)

Release Notes:

- N/A

---------

Co-authored-by: Marshall <marshall@zed.dev>
Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Max Brunsfeld 2024-04-08 07:16:12 -07:00 committed by GitHub
parent 4ce5b22989
commit 4bdfc12b79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 311 additions and 334 deletions

View file

@ -15,6 +15,8 @@ wasmtime::component::bindgen!({
path: "../extension_api/wit/since_v0.0.1",
with: {
"worktree": ExtensionWorktree,
"zed:extension/github": latest::zed::extension::github,
"zed:extension/platform": latest::zed::extension::platform,
},
});
@ -25,53 +27,6 @@ pub fn linker() -> &'static Linker<WasmState> {
LINKER.get_or_init(|| super::new_linker(Extension::add_to_linker))
}
impl From<latest::Os> for Os {
fn from(value: latest::Os) -> Self {
match value {
latest::Os::Mac => Os::Mac,
latest::Os::Linux => Os::Linux,
latest::Os::Windows => Os::Windows,
}
}
}
impl From<latest::Architecture> for Architecture {
fn from(value: latest::Architecture) -> Self {
match value {
latest::Architecture::Aarch64 => Self::Aarch64,
latest::Architecture::X86 => Self::X86,
latest::Architecture::X8664 => Self::X8664,
}
}
}
impl From<latest::GithubRelease> for GithubRelease {
fn from(value: latest::GithubRelease) -> Self {
Self {
version: value.version,
assets: value.assets.into_iter().map(|asset| asset.into()).collect(),
}
}
}
impl From<latest::GithubReleaseAsset> for GithubReleaseAsset {
fn from(value: latest::GithubReleaseAsset) -> Self {
Self {
name: value.name,
download_url: value.download_url,
}
}
}
impl From<GithubReleaseOptions> for latest::GithubReleaseOptions {
fn from(value: GithubReleaseOptions) -> Self {
Self {
require_assets: value.require_assets,
pre_release: value.pre_release,
}
}
}
impl From<DownloadedFileType> for latest::DownloadedFileType {
fn from(value: DownloadedFileType) -> Self {
match value {
@ -135,21 +90,21 @@ impl HostWorktree for WasmState {
#[async_trait]
impl ExtensionImports for WasmState {
async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
latest::ExtensionImports::node_binary_path(self).await
latest::nodejs::Host::node_binary_path(self).await
}
async fn npm_package_latest_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<String, String>> {
latest::ExtensionImports::npm_package_latest_version(self, package_name).await
latest::nodejs::Host::npm_package_latest_version(self, package_name).await
}
async fn npm_package_installed_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<Option<String>, String>> {
latest::ExtensionImports::npm_package_installed_version(self, package_name).await
latest::nodejs::Host::npm_package_installed_version(self, package_name).await
}
async fn npm_install_package(
@ -157,7 +112,7 @@ impl ExtensionImports for WasmState {
package_name: String,
version: String,
) -> wasmtime::Result<Result<(), String>> {
latest::ExtensionImports::npm_install_package(self, package_name, version).await
latest::nodejs::Host::npm_install_package(self, package_name, version).await
}
async fn latest_github_release(
@ -165,17 +120,11 @@ impl ExtensionImports for WasmState {
repo: String,
options: GithubReleaseOptions,
) -> wasmtime::Result<Result<GithubRelease, String>> {
Ok(
latest::ExtensionImports::latest_github_release(self, repo, options.into())
.await?
.map(|github| github.into()),
)
latest::zed::extension::github::Host::latest_github_release(self, repo, options).await
}
async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
latest::ExtensionImports::current_platform(self)
.await
.map(|(os, arch)| (os.into(), arch.into()))
latest::zed::extension::platform::Host::current_platform(self).await
}
async fn set_language_server_installation_status(

View file

@ -14,6 +14,8 @@ wasmtime::component::bindgen!({
path: "../extension_api/wit/since_v0.0.4",
with: {
"worktree": ExtensionWorktree,
"zed:extension/github": latest::zed::extension::github,
"zed:extension/platform": latest::zed::extension::platform,
},
});
@ -24,53 +26,6 @@ pub fn linker() -> &'static Linker<WasmState> {
LINKER.get_or_init(|| super::new_linker(Extension::add_to_linker))
}
impl From<latest::Os> for Os {
fn from(value: latest::Os) -> Self {
match value {
latest::Os::Mac => Os::Mac,
latest::Os::Linux => Os::Linux,
latest::Os::Windows => Os::Windows,
}
}
}
impl From<latest::Architecture> for Architecture {
fn from(value: latest::Architecture) -> Self {
match value {
latest::Architecture::Aarch64 => Self::Aarch64,
latest::Architecture::X86 => Self::X86,
latest::Architecture::X8664 => Self::X8664,
}
}
}
impl From<latest::GithubRelease> for GithubRelease {
fn from(value: latest::GithubRelease) -> Self {
Self {
version: value.version,
assets: value.assets.into_iter().map(|asset| asset.into()).collect(),
}
}
}
impl From<latest::GithubReleaseAsset> for GithubReleaseAsset {
fn from(value: latest::GithubReleaseAsset) -> Self {
Self {
name: value.name,
download_url: value.download_url,
}
}
}
impl From<GithubReleaseOptions> for latest::GithubReleaseOptions {
fn from(value: GithubReleaseOptions) -> Self {
Self {
require_assets: value.require_assets,
pre_release: value.pre_release,
}
}
}
impl From<DownloadedFileType> for latest::DownloadedFileType {
fn from(value: DownloadedFileType) -> Self {
match value {
@ -145,21 +100,21 @@ impl HostWorktree for WasmState {
#[async_trait]
impl ExtensionImports for WasmState {
async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
latest::ExtensionImports::node_binary_path(self).await
latest::nodejs::Host::node_binary_path(self).await
}
async fn npm_package_latest_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<String, String>> {
latest::ExtensionImports::npm_package_latest_version(self, package_name).await
latest::nodejs::Host::npm_package_latest_version(self, package_name).await
}
async fn npm_package_installed_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<Option<String>, String>> {
latest::ExtensionImports::npm_package_installed_version(self, package_name).await
latest::nodejs::Host::npm_package_installed_version(self, package_name).await
}
async fn npm_install_package(
@ -167,7 +122,7 @@ impl ExtensionImports for WasmState {
package_name: String,
version: String,
) -> wasmtime::Result<Result<(), String>> {
latest::ExtensionImports::npm_install_package(self, package_name, version).await
latest::nodejs::Host::npm_install_package(self, package_name, version).await
}
async fn latest_github_release(
@ -175,17 +130,11 @@ impl ExtensionImports for WasmState {
repo: String,
options: GithubReleaseOptions,
) -> wasmtime::Result<Result<GithubRelease, String>> {
Ok(
latest::ExtensionImports::latest_github_release(self, repo, options.into())
.await?
.map(|github| github.into()),
)
latest::zed::extension::github::Host::latest_github_release(self, repo, options).await
}
async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
latest::ExtensionImports::current_platform(self)
.await
.map(|(os, arch)| (os.into(), arch.into()))
latest::zed::extension::platform::Host::current_platform(self).await
}
async fn set_language_server_installation_status(

View file

@ -1,13 +1,13 @@
use crate::wasm_host::wit::ToWasmtimeResult;
use crate::wasm_host::WasmState;
use crate::wasm_host::{wit::ToWasmtimeResult, WasmState};
use ::settings::Settings;
use anyhow::{anyhow, bail, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use async_trait::async_trait;
use futures::{io::BufReader, FutureExt as _};
use language::language_settings::AllLanguageSettings;
use language::{LanguageServerBinaryStatus, LspAdapterDelegate};
use language::{
language_settings::AllLanguageSettings, LanguageServerBinaryStatus, LspAdapterDelegate,
};
use project::project_settings::ProjectSettings;
use semantic_version::SemanticVersion;
use std::{
@ -29,6 +29,8 @@ wasmtime::component::bindgen!({
},
});
pub use self::zed::extension::*;
mod settings {
include!("../../../../extension_api/wit/since_v0.0.6/settings.rs");
}
@ -96,7 +98,106 @@ impl HostWorktree for WasmState {
}
}
impl self::zed::extension::lsp::Host for WasmState {}
#[async_trait]
impl nodejs::Host for WasmState {
async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
self.host
.node_runtime
.binary_path()
.await
.map(|path| path.to_string_lossy().to_string())
.to_wasmtime_result()
}
async fn npm_package_latest_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<String, String>> {
self.host
.node_runtime
.npm_package_latest_version(&package_name)
.await
.to_wasmtime_result()
}
async fn npm_package_installed_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<Option<String>, String>> {
self.host
.node_runtime
.npm_package_installed_version(&self.work_dir(), &package_name)
.await
.to_wasmtime_result()
}
async fn npm_install_package(
&mut self,
package_name: String,
version: String,
) -> wasmtime::Result<Result<(), String>> {
self.host
.node_runtime
.npm_install_packages(&self.work_dir(), &[(&package_name, &version)])
.await
.to_wasmtime_result()
}
}
#[async_trait]
impl lsp::Host for WasmState {}
#[async_trait]
impl github::Host for WasmState {
async fn latest_github_release(
&mut self,
repo: String,
options: github::GithubReleaseOptions,
) -> wasmtime::Result<Result<github::GithubRelease, String>> {
maybe!(async {
let release = util::github::latest_github_release(
&repo,
options.require_assets,
options.pre_release,
self.host.http_client.clone(),
)
.await?;
Ok(github::GithubRelease {
version: release.tag_name,
assets: release
.assets
.into_iter()
.map(|asset| github::GithubReleaseAsset {
name: asset.name,
download_url: asset.browser_download_url,
})
.collect(),
})
})
.await
.to_wasmtime_result()
}
}
#[async_trait]
impl platform::Host for WasmState {
async fn current_platform(&mut self) -> Result<(platform::Os, platform::Architecture)> {
Ok((
match env::consts::OS {
"macos" => platform::Os::Mac,
"linux" => platform::Os::Linux,
"windows" => platform::Os::Windows,
_ => panic!("unsupported os"),
},
match env::consts::ARCH {
"aarch64" => platform::Architecture::Aarch64,
"x86" => platform::Architecture::X86,
"x86_64" => platform::Architecture::X8664,
_ => panic!("unsupported architecture"),
},
))
}
}
#[async_trait]
impl ExtensionImports for WasmState {
@ -152,95 +253,6 @@ impl ExtensionImports for WasmState {
.to_wasmtime_result()
}
async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
self.host
.node_runtime
.binary_path()
.await
.map(|path| path.to_string_lossy().to_string())
.to_wasmtime_result()
}
async fn npm_package_latest_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<String, String>> {
self.host
.node_runtime
.npm_package_latest_version(&package_name)
.await
.to_wasmtime_result()
}
async fn npm_package_installed_version(
&mut self,
package_name: String,
) -> wasmtime::Result<Result<Option<String>, String>> {
self.host
.node_runtime
.npm_package_installed_version(&self.work_dir(), &package_name)
.await
.to_wasmtime_result()
}
async fn npm_install_package(
&mut self,
package_name: String,
version: String,
) -> wasmtime::Result<Result<(), String>> {
self.host
.node_runtime
.npm_install_packages(&self.work_dir(), &[(&package_name, &version)])
.await
.to_wasmtime_result()
}
async fn latest_github_release(
&mut self,
repo: String,
options: GithubReleaseOptions,
) -> wasmtime::Result<Result<GithubRelease, String>> {
maybe!(async {
let release = util::github::latest_github_release(
&repo,
options.require_assets,
options.pre_release,
self.host.http_client.clone(),
)
.await?;
Ok(GithubRelease {
version: release.tag_name,
assets: release
.assets
.into_iter()
.map(|asset| GithubReleaseAsset {
name: asset.name,
download_url: asset.browser_download_url,
})
.collect(),
})
})
.await
.to_wasmtime_result()
}
async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
Ok((
match env::consts::OS {
"macos" => Os::Mac,
"linux" => Os::Linux,
"windows" => Os::Windows,
_ => panic!("unsupported os"),
},
match env::consts::ARCH {
"aarch64" => Architecture::Aarch64,
"x86" => Architecture::X86,
"x86_64" => Architecture::X8664,
_ => panic!("unsupported architecture"),
},
))
}
async fn set_language_server_installation_status(
&mut self,
server_name: String,