Add initial element inspector for Zed development (#31315)

Open inspector with `dev: toggle inspector` from command palette or
`cmd-alt-i` on mac or `ctrl-alt-i` on linux.

https://github.com/user-attachments/assets/54c43034-d40b-414e-ba9b-190bed2e6d2f

* Picking of elements via the mouse, with scroll wheel to inspect
occluded elements.

* Temporary manipulation of the selected element.

* Layout info and JSON-based style manipulation for `Div`.

* Navigation to code that constructed the element.

Big thanks to @as-cii and @maxdeviant for sorting out how to implement
the core of an inspector.

Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Marshall Bowers <git@maxdeviant.com>
Co-authored-by: Federico Dionisi <code@fdionisi.me>
This commit is contained in:
Michael Sloan 2025-05-23 17:08:59 -06:00 committed by GitHub
parent 685933b5c8
commit ab59982bf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
74 changed files with 2631 additions and 406 deletions

View file

@ -19,6 +19,7 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
let refineable_attr = attrs.iter().find(|attr| attr.path().is_ident("refineable"));
let mut impl_debug_on_refinement = false;
let mut derives_serialize = false;
let mut refinement_traits_to_derive = vec![];
if let Some(refineable_attr) = refineable_attr {
@ -26,6 +27,9 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
if meta.path.is_ident("Debug") {
impl_debug_on_refinement = true;
} else {
if meta.path.is_ident("Serialize") {
derives_serialize = true;
}
refinement_traits_to_derive.push(meta.path);
}
Ok(())
@ -47,6 +51,21 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
let field_visibilities: Vec<_> = fields.iter().map(|f| &f.vis).collect();
let wrapped_types: Vec<_> = fields.iter().map(|f| get_wrapper_type(f, &f.ty)).collect();
let field_attributes: Vec<TokenStream2> = fields
.iter()
.map(|f| {
if derives_serialize {
if is_refineable_field(f) {
quote! { #[serde(default, skip_serializing_if = "::refineable::IsEmpty::is_empty")] }
} else {
quote! { #[serde(skip_serializing_if = "::std::option::Option::is_none")] }
}
} else {
quote! {}
}
})
.collect();
// Create trait bound that each wrapped type must implement Clone // & Default
let type_param_bounds: Vec<_> = wrapped_types
.iter()
@ -234,6 +253,26 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
quote! {}
};
let refinement_is_empty_conditions: Vec<TokenStream2> = fields
.iter()
.enumerate()
.map(|(i, field)| {
let name = &field.ident;
let condition = if is_refineable_field(field) {
quote! { self.#name.is_empty() }
} else {
quote! { self.#name.is_none() }
};
if i < fields.len() - 1 {
quote! { #condition && }
} else {
condition
}
})
.collect();
let mut derive_stream = quote! {};
for trait_to_derive in refinement_traits_to_derive {
derive_stream.extend(quote! { #[derive(#trait_to_derive)] })
@ -246,6 +285,7 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
pub struct #refinement_ident #impl_generics {
#(
#[allow(missing_docs)]
#field_attributes
#field_visibilities #field_names: #wrapped_types
),*
}
@ -280,6 +320,14 @@ pub fn derive_refineable(input: TokenStream) -> TokenStream {
}
}
impl #impl_generics ::refineable::IsEmpty for #refinement_ident #ty_generics
#where_clause
{
fn is_empty(&self) -> bool {
#( #refinement_is_empty_conditions )*
}
}
impl #impl_generics From<#refinement_ident #ty_generics> for #ident #ty_generics
#where_clause
{

View file

@ -1,7 +1,7 @@
pub use derive_refineable::Refineable;
pub trait Refineable: Clone {
type Refinement: Refineable<Refinement = Self::Refinement> + Default;
type Refinement: Refineable<Refinement = Self::Refinement> + IsEmpty + Default;
fn refine(&mut self, refinement: &Self::Refinement);
fn refined(self, refinement: Self::Refinement) -> Self;
@ -13,6 +13,11 @@ pub trait Refineable: Clone {
}
}
pub trait IsEmpty {
/// When `true`, indicates that use applying this refinement does nothing.
fn is_empty(&self) -> bool;
}
pub struct Cascade<S: Refineable>(Vec<Option<S::Refinement>>);
impl<S: Refineable + Default> Default for Cascade<S> {