There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
490 lines
17 KiB
Rust
490 lines
17 KiB
Rust
mod since_v0_0_1;
|
|
mod since_v0_0_4;
|
|
mod since_v0_0_6;
|
|
mod since_v0_1_0;
|
|
mod since_v0_2_0;
|
|
use extension::{KeyValueStoreDelegate, WorktreeDelegate};
|
|
use language::LanguageName;
|
|
use lsp::LanguageServerName;
|
|
use release_channel::ReleaseChannel;
|
|
use since_v0_2_0 as latest;
|
|
|
|
use super::{wasm_engine, WasmState};
|
|
use anyhow::{anyhow, Context as _, Result};
|
|
use semantic_version::SemanticVersion;
|
|
use std::{ops::RangeInclusive, sync::Arc};
|
|
use wasmtime::{
|
|
component::{Component, Linker, Resource},
|
|
Store,
|
|
};
|
|
|
|
#[cfg(test)]
|
|
pub use latest::CodeLabelSpanLiteral;
|
|
pub use latest::{
|
|
zed::extension::lsp::{
|
|
Completion, CompletionKind, CompletionLabelDetails, InsertTextFormat, Symbol, SymbolKind,
|
|
},
|
|
zed::extension::slash_command::{SlashCommandArgumentCompletion, SlashCommandOutput},
|
|
CodeLabel, CodeLabelSpan, Command, ExtensionProject, Range, SlashCommand,
|
|
};
|
|
pub use since_v0_0_4::LanguageServerConfig;
|
|
|
|
pub fn new_linker(
|
|
f: impl Fn(&mut Linker<WasmState>, fn(&mut WasmState) -> &mut WasmState) -> Result<()>,
|
|
) -> Linker<WasmState> {
|
|
let mut linker = Linker::new(&wasm_engine());
|
|
wasmtime_wasi::add_to_linker_async(&mut linker).unwrap();
|
|
f(&mut linker, wasi_view).unwrap();
|
|
linker
|
|
}
|
|
|
|
fn wasi_view(state: &mut WasmState) -> &mut WasmState {
|
|
state
|
|
}
|
|
|
|
/// Returns whether the given Wasm API version is supported by the Wasm host.
|
|
pub fn is_supported_wasm_api_version(
|
|
release_channel: ReleaseChannel,
|
|
version: SemanticVersion,
|
|
) -> bool {
|
|
wasm_api_version_range(release_channel).contains(&version)
|
|
}
|
|
|
|
/// Returns the Wasm API version range that is supported by the Wasm host.
|
|
#[inline(always)]
|
|
pub fn wasm_api_version_range(release_channel: ReleaseChannel) -> RangeInclusive<SemanticVersion> {
|
|
// Note: The release channel can be used to stage a new version of the extension API.
|
|
let _ = release_channel;
|
|
|
|
let max_version = match release_channel {
|
|
ReleaseChannel::Dev | ReleaseChannel::Nightly => latest::MAX_VERSION,
|
|
ReleaseChannel::Stable | ReleaseChannel::Preview => latest::MAX_VERSION,
|
|
};
|
|
|
|
since_v0_0_1::MIN_VERSION..=max_version
|
|
}
|
|
|
|
/// Authorizes access to use unreleased versions of the Wasm API, based on the provided [`ReleaseChannel`].
|
|
///
|
|
/// Note: If there isn't currently an unreleased Wasm API version this function may be unused. Don't delete it!
|
|
pub fn authorize_access_to_unreleased_wasm_api_version(
|
|
release_channel: ReleaseChannel,
|
|
) -> Result<()> {
|
|
let allow_unreleased_version = match release_channel {
|
|
ReleaseChannel::Dev | ReleaseChannel::Nightly => true,
|
|
ReleaseChannel::Stable | ReleaseChannel::Preview => {
|
|
// We always allow the latest in tests so that the extension tests pass on release branches.
|
|
cfg!(any(test, feature = "test-support"))
|
|
}
|
|
};
|
|
|
|
if !allow_unreleased_version {
|
|
Err(anyhow!(
|
|
"unreleased versions of the extension API can only be used on development builds of Zed"
|
|
))?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub enum Extension {
|
|
V020(since_v0_2_0::Extension),
|
|
V010(since_v0_1_0::Extension),
|
|
V006(since_v0_0_6::Extension),
|
|
V004(since_v0_0_4::Extension),
|
|
V001(since_v0_0_1::Extension),
|
|
}
|
|
|
|
impl Extension {
|
|
pub async fn instantiate_async(
|
|
store: &mut Store<WasmState>,
|
|
release_channel: ReleaseChannel,
|
|
version: SemanticVersion,
|
|
component: &Component,
|
|
) -> Result<Self> {
|
|
// Note: The release channel can be used to stage a new version of the extension API.
|
|
let _ = release_channel;
|
|
|
|
if version >= latest::MIN_VERSION {
|
|
let extension =
|
|
latest::Extension::instantiate_async(store, component, latest::linker())
|
|
.await
|
|
.context("failed to instantiate wasm extension")?;
|
|
Ok(Self::V020(extension))
|
|
} else if version >= since_v0_1_0::MIN_VERSION {
|
|
let extension = since_v0_1_0::Extension::instantiate_async(
|
|
store,
|
|
component,
|
|
since_v0_1_0::linker(),
|
|
)
|
|
.await
|
|
.context("failed to instantiate wasm extension")?;
|
|
Ok(Self::V010(extension))
|
|
} else if version >= since_v0_0_6::MIN_VERSION {
|
|
let extension = since_v0_0_6::Extension::instantiate_async(
|
|
store,
|
|
component,
|
|
since_v0_0_6::linker(),
|
|
)
|
|
.await
|
|
.context("failed to instantiate wasm extension")?;
|
|
Ok(Self::V006(extension))
|
|
} else if version >= since_v0_0_4::MIN_VERSION {
|
|
let extension = since_v0_0_4::Extension::instantiate_async(
|
|
store,
|
|
component,
|
|
since_v0_0_4::linker(),
|
|
)
|
|
.await
|
|
.context("failed to instantiate wasm extension")?;
|
|
Ok(Self::V004(extension))
|
|
} else {
|
|
let extension = since_v0_0_1::Extension::instantiate_async(
|
|
store,
|
|
component,
|
|
since_v0_0_1::linker(),
|
|
)
|
|
.await
|
|
.context("failed to instantiate wasm extension")?;
|
|
Ok(Self::V001(extension))
|
|
}
|
|
}
|
|
|
|
pub async fn call_init_extension(&self, store: &mut Store<WasmState>) -> Result<()> {
|
|
match self {
|
|
Extension::V020(ext) => ext.call_init_extension(store).await,
|
|
Extension::V010(ext) => ext.call_init_extension(store).await,
|
|
Extension::V006(ext) => ext.call_init_extension(store).await,
|
|
Extension::V004(ext) => ext.call_init_extension(store).await,
|
|
Extension::V001(ext) => ext.call_init_extension(store).await,
|
|
}
|
|
}
|
|
|
|
pub async fn call_language_server_command(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
language_server_id: &LanguageServerName,
|
|
language_name: &LanguageName,
|
|
resource: Resource<Arc<dyn WorktreeDelegate>>,
|
|
) -> Result<Result<Command, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_language_server_command(store, &language_server_id.0, resource)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => Ok(ext
|
|
.call_language_server_command(store, &language_server_id.0, resource)
|
|
.await?
|
|
.map(|command| command.into())),
|
|
Extension::V006(ext) => Ok(ext
|
|
.call_language_server_command(store, &language_server_id.0, resource)
|
|
.await?
|
|
.map(|command| command.into())),
|
|
Extension::V004(ext) => Ok(ext
|
|
.call_language_server_command(
|
|
store,
|
|
&LanguageServerConfig {
|
|
name: language_server_id.0.to_string(),
|
|
language_name: language_name.to_string(),
|
|
},
|
|
resource,
|
|
)
|
|
.await?
|
|
.map(|command| command.into())),
|
|
Extension::V001(ext) => Ok(ext
|
|
.call_language_server_command(
|
|
store,
|
|
&LanguageServerConfig {
|
|
name: language_server_id.0.to_string(),
|
|
language_name: language_name.to_string(),
|
|
}
|
|
.into(),
|
|
resource,
|
|
)
|
|
.await?
|
|
.map(|command| command.into())),
|
|
}
|
|
}
|
|
|
|
pub async fn call_language_server_initialization_options(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
language_server_id: &LanguageServerName,
|
|
language_name: &LanguageName,
|
|
resource: Resource<Arc<dyn WorktreeDelegate>>,
|
|
) -> Result<Result<Option<String>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_language_server_initialization_options(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => {
|
|
ext.call_language_server_initialization_options(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V006(ext) => {
|
|
ext.call_language_server_initialization_options(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V004(ext) => {
|
|
ext.call_language_server_initialization_options(
|
|
store,
|
|
&LanguageServerConfig {
|
|
name: language_server_id.0.to_string(),
|
|
language_name: language_name.to_string(),
|
|
},
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V001(ext) => {
|
|
ext.call_language_server_initialization_options(
|
|
store,
|
|
&LanguageServerConfig {
|
|
name: language_server_id.0.to_string(),
|
|
language_name: language_name.to_string(),
|
|
}
|
|
.into(),
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn call_language_server_workspace_configuration(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
language_server_id: &LanguageServerName,
|
|
resource: Resource<Arc<dyn WorktreeDelegate>>,
|
|
) -> Result<Result<Option<String>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_language_server_workspace_configuration(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => {
|
|
ext.call_language_server_workspace_configuration(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V006(ext) => {
|
|
ext.call_language_server_workspace_configuration(
|
|
store,
|
|
&language_server_id.0,
|
|
resource,
|
|
)
|
|
.await
|
|
}
|
|
Extension::V004(_) | Extension::V001(_) => Ok(Ok(None)),
|
|
}
|
|
}
|
|
|
|
pub async fn call_labels_for_completions(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
language_server_id: &LanguageServerName,
|
|
completions: Vec<latest::Completion>,
|
|
) -> Result<Result<Vec<Option<CodeLabel>>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_labels_for_completions(store, &language_server_id.0, &completions)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => Ok(ext
|
|
.call_labels_for_completions(
|
|
store,
|
|
&language_server_id.0,
|
|
&completions.into_iter().map(Into::into).collect::<Vec<_>>(),
|
|
)
|
|
.await?
|
|
.map(|labels| {
|
|
labels
|
|
.into_iter()
|
|
.map(|label| label.map(Into::into))
|
|
.collect()
|
|
})),
|
|
Extension::V006(ext) => Ok(ext
|
|
.call_labels_for_completions(
|
|
store,
|
|
&language_server_id.0,
|
|
&completions.into_iter().map(Into::into).collect::<Vec<_>>(),
|
|
)
|
|
.await?
|
|
.map(|labels| {
|
|
labels
|
|
.into_iter()
|
|
.map(|label| label.map(Into::into))
|
|
.collect()
|
|
})),
|
|
Extension::V001(_) | Extension::V004(_) => Ok(Ok(Vec::new())),
|
|
}
|
|
}
|
|
|
|
pub async fn call_labels_for_symbols(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
language_server_id: &LanguageServerName,
|
|
symbols: Vec<latest::Symbol>,
|
|
) -> Result<Result<Vec<Option<CodeLabel>>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_labels_for_symbols(store, &language_server_id.0, &symbols)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => Ok(ext
|
|
.call_labels_for_symbols(
|
|
store,
|
|
&language_server_id.0,
|
|
&symbols.into_iter().map(Into::into).collect::<Vec<_>>(),
|
|
)
|
|
.await?
|
|
.map(|labels| {
|
|
labels
|
|
.into_iter()
|
|
.map(|label| label.map(Into::into))
|
|
.collect()
|
|
})),
|
|
Extension::V006(ext) => Ok(ext
|
|
.call_labels_for_symbols(
|
|
store,
|
|
&language_server_id.0,
|
|
&symbols.into_iter().map(Into::into).collect::<Vec<_>>(),
|
|
)
|
|
.await?
|
|
.map(|labels| {
|
|
labels
|
|
.into_iter()
|
|
.map(|label| label.map(Into::into))
|
|
.collect()
|
|
})),
|
|
Extension::V001(_) | Extension::V004(_) => Ok(Ok(Vec::new())),
|
|
}
|
|
}
|
|
|
|
pub async fn call_complete_slash_command_argument(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
command: &SlashCommand,
|
|
arguments: &[String],
|
|
) -> Result<Result<Vec<SlashCommandArgumentCompletion>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_complete_slash_command_argument(store, command, arguments)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => {
|
|
ext.call_complete_slash_command_argument(store, command, arguments)
|
|
.await
|
|
}
|
|
Extension::V001(_) | Extension::V004(_) | Extension::V006(_) => Ok(Ok(Vec::new())),
|
|
}
|
|
}
|
|
|
|
pub async fn call_run_slash_command(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
command: &SlashCommand,
|
|
arguments: &[String],
|
|
resource: Option<Resource<Arc<dyn WorktreeDelegate>>>,
|
|
) -> Result<Result<SlashCommandOutput, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_run_slash_command(store, command, arguments, resource)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => {
|
|
ext.call_run_slash_command(store, command, arguments, resource)
|
|
.await
|
|
}
|
|
Extension::V001(_) | Extension::V004(_) | Extension::V006(_) => {
|
|
Err(anyhow!("`run_slash_command` not available prior to v0.1.0"))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn call_context_server_command(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
context_server_id: Arc<str>,
|
|
project: Resource<ExtensionProject>,
|
|
) -> Result<Result<Command, String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_context_server_command(store, &context_server_id, project)
|
|
.await
|
|
}
|
|
Extension::V001(_) | Extension::V004(_) | Extension::V006(_) | Extension::V010(_) => {
|
|
Err(anyhow!(
|
|
"`context_server_command` not available prior to v0.2.0"
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn call_suggest_docs_packages(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
provider: &str,
|
|
) -> Result<Result<Vec<String>, String>> {
|
|
match self {
|
|
Extension::V020(ext) => ext.call_suggest_docs_packages(store, provider).await,
|
|
Extension::V010(ext) => ext.call_suggest_docs_packages(store, provider).await,
|
|
Extension::V001(_) | Extension::V004(_) | Extension::V006(_) => Err(anyhow!(
|
|
"`suggest_docs_packages` not available prior to v0.1.0"
|
|
)),
|
|
}
|
|
}
|
|
|
|
pub async fn call_index_docs(
|
|
&self,
|
|
store: &mut Store<WasmState>,
|
|
provider: &str,
|
|
package_name: &str,
|
|
kv_store: Resource<Arc<dyn KeyValueStoreDelegate>>,
|
|
) -> Result<Result<(), String>> {
|
|
match self {
|
|
Extension::V020(ext) => {
|
|
ext.call_index_docs(store, provider, package_name, kv_store)
|
|
.await
|
|
}
|
|
Extension::V010(ext) => {
|
|
ext.call_index_docs(store, provider, package_name, kv_store)
|
|
.await
|
|
}
|
|
Extension::V001(_) | Extension::V004(_) | Extension::V006(_) => {
|
|
Err(anyhow!("`index_docs` not available prior to v0.1.0"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
trait ToWasmtimeResult<T> {
|
|
fn to_wasmtime_result(self) -> wasmtime::Result<Result<T, String>>;
|
|
}
|
|
|
|
impl<T> ToWasmtimeResult<T> for Result<T> {
|
|
fn to_wasmtime_result(self) -> wasmtime::Result<Result<T, String>> {
|
|
Ok(self.map_err(|error| error.to_string()))
|
|
}
|
|
}
|