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,

View file

@ -13,11 +13,17 @@ pub use serde_json;
// We explicitly enumerate the symbols we want to re-export, as there are some
// that we may want to shadow to provide a cleaner Rust API.
pub use wit::{
current_platform, download_file, latest_github_release, make_file_executable, node_binary_path,
npm_install_package, npm_package_installed_version, npm_package_latest_version, Architecture,
download_file, make_file_executable,
zed::extension::github::{
latest_github_release, GithubRelease, GithubReleaseAsset, GithubReleaseOptions,
},
zed::extension::nodejs::{
node_binary_path, npm_install_package, npm_package_installed_version,
npm_package_latest_version,
},
zed::extension::platform::{current_platform, Architecture, Os},
CodeLabel, CodeLabelSpan, CodeLabelSpanLiteral, Command, DownloadedFileType, EnvVars,
GithubRelease, GithubReleaseAsset, GithubReleaseOptions, LanguageServerInstallationStatus, Os,
Range, Worktree,
LanguageServerInstallationStatus, Range, Worktree,
};
// Undocumented WIT re-exports.

View file

@ -1,5 +1,5 @@
#[path = "../wit/since_v0.0.6/settings.rs"]
pub mod types;
mod types;
use crate::{wit, Result, SettingsLocation, Worktree};
use serde_json;

View file

@ -1,35 +1,11 @@
package zed:extension;
world extension {
use github.{github-release, github-release-options};
use platform.{os, architecture};
export init-extension: func();
record github-release {
version: string,
assets: list<github-release-asset>,
}
record github-release-asset {
name: string,
download-url: string,
}
record github-release-options {
require-assets: bool,
pre-release: bool,
}
enum os {
mac,
linux,
windows,
}
enum architecture {
aarch64,
x86,
x8664,
}
enum downloaded-file-type {
gzip,
gzip-tar,

View file

@ -0,0 +1,28 @@
interface github {
/// A GitHub release.
record github-release {
/// The version of the release.
version: string,
/// The list of assets attached to the release.
assets: list<github-release-asset>,
}
/// An asset from a GitHub release.
record github-release-asset {
/// The name of the asset.
name: string,
/// The download URL for the asset.
download-url: string,
}
/// The options used to filter down GitHub releases.
record github-release-options {
/// Whether releases without assets should be included.
require-assets: bool,
/// Whether pre-releases should be included.
pre-release: bool,
}
/// Returns the latest release for the given GitHub repository.
latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
}

View file

@ -0,0 +1,24 @@
interface platform {
/// An operating system.
enum os {
/// macOS.
mac,
/// Linux.
linux,
/// Windows.
windows,
}
/// A platform architecture.
enum architecture {
/// AArch64 (e.g., Apple Silicon).
aarch64,
/// x86.
x86,
/// x86-64.
x8664,
}
/// Gets the current operating system and architecture.
current-platform: func() -> tuple<os, architecture>;
}

View file

@ -1,35 +1,11 @@
package zed:extension;
world extension {
use github.{github-release, github-release-options};
use platform.{os, architecture};
export init-extension: func();
record github-release {
version: string,
assets: list<github-release-asset>,
}
record github-release-asset {
name: string,
download-url: string,
}
record github-release-options {
require-assets: bool,
pre-release: bool,
}
enum os {
mac,
linux,
windows,
}
enum architecture {
aarch64,
x86,
x8664,
}
enum downloaded-file-type {
gzip,
gzip-tar,

View file

@ -0,0 +1,28 @@
interface github {
/// A GitHub release.
record github-release {
/// The version of the release.
version: string,
/// The list of assets attached to the release.
assets: list<github-release-asset>,
}
/// An asset from a GitHub release.
record github-release-asset {
/// The name of the asset.
name: string,
/// The download URL for the asset.
download-url: string,
}
/// The options used to filter down GitHub releases.
record github-release-options {
/// Whether releases without assets should be included.
require-assets: bool,
/// Whether pre-releases should be included.
pre-release: bool,
}
/// Returns the latest release for the given GitHub repository.
latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
}

View file

@ -0,0 +1,24 @@
interface platform {
/// An operating system.
enum os {
/// macOS.
mac,
/// Linux.
linux,
/// Windows.
windows,
}
/// A platform architecture.
enum architecture {
/// AArch64 (e.g., Apple Silicon).
aarch64,
/// x86.
x86,
/// x86-64.
x8664,
}
/// Gets the current operating system and architecture.
current-platform: func() -> tuple<os, architecture>;
}

View file

@ -1,57 +1,15 @@
package zed:extension;
world extension {
import lsp;
import github;
import platform;
import nodejs;
use lsp.{completion, symbol};
/// Initializes the extension.
export init-extension: func();
/// A GitHub release.
record github-release {
/// The version of the release.
version: string,
/// The list of assets attached to the release.
assets: list<github-release-asset>,
}
/// An asset from a GitHub release.
record github-release-asset {
/// The name of the asset.
name: string,
/// The download URL for the asset.
download-url: string,
}
/// The options used to filter down GitHub releases.
record github-release-options {
/// Whether releases without assets should be included.
require-assets: bool,
/// Whether pre-releases should be included.
pre-release: bool,
}
/// An operating system.
enum os {
/// macOS.
mac,
/// Linux.
linux,
/// Windows.
windows,
}
/// A platform architecture.
enum architecture {
/// AArch64 (e.g., Apple Silicon).
aarch64,
/// x86.
x86,
/// x86-64.
x8664,
}
/// The type of a downloaded file.
enum downloaded-file-type {
/// A gzipped file (`.gz`).
@ -76,9 +34,6 @@ world extension {
failed(string),
}
/// Returns operating system and architecture for the current platform.
import current-platform: func() -> tuple<os, architecture>;
record settings-location {
worktree-id: u64,
path: string,
@ -86,21 +41,6 @@ world extension {
import get-settings: func(path: option<settings-location>, category: string, key: option<string>) -> result<string, string>;
/// Returns the path to the Node binary used by Zed.
import node-binary-path: func() -> result<string, string>;
/// Returns the latest version of the given NPM package.
import npm-package-latest-version: func(package-name: string) -> result<string, string>;
/// Returns the installed version of the given NPM package, if it exists.
import npm-package-installed-version: func(package-name: string) -> result<option<string>, string>;
/// Installs the specified NPM package.
import npm-install-package: func(package-name: string, version: string) -> result<_, string>;
/// Returns the latest release for the given GitHub repository.
import latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
/// Downloads a file from the given URL and saves it to the given path within the extension's
/// working directory.
///

View file

@ -0,0 +1,28 @@
interface github {
/// A GitHub release.
record github-release {
/// The version of the release.
version: string,
/// The list of assets attached to the release.
assets: list<github-release-asset>,
}
/// An asset from a GitHub release.
record github-release-asset {
/// The name of the asset.
name: string,
/// The download URL for the asset.
download-url: string,
}
/// The options used to filter down GitHub releases.
record github-release-options {
/// Whether releases without assets should be included.
require-assets: bool,
/// Whether pre-releases should be included.
pre-release: bool,
}
/// Returns the latest release for the given GitHub repository.
latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
}

View file

@ -0,0 +1,13 @@
interface nodejs {
/// Returns the path to the Node binary used by Zed.
node-binary-path: func() -> result<string, string>;
/// Returns the latest version of the given NPM package.
npm-package-latest-version: func(package-name: string) -> result<string, string>;
/// Returns the installed version of the given NPM package, if it exists.
npm-package-installed-version: func(package-name: string) -> result<option<string>, string>;
/// Installs the specified NPM package.
npm-install-package: func(package-name: string, version: string) -> result<_, string>;
}

View file

@ -0,0 +1,24 @@
interface platform {
/// An operating system.
enum os {
/// macOS.
mac,
/// Linux.
linux,
/// Windows.
windows,
}
/// A platform architecture.
enum architecture {
/// AArch64 (e.g., Apple Silicon).
aarch64,
/// x86.
x86,
/// x86-64.
x8664,
}
/// Gets the current operating system and architecture.
current-platform: func() -> tuple<os, architecture>;
}