Fix window double borrows (#23739)
Fix bugs caused by the window context PR, where the window could be on the stack and is then requested from the App. This PR also adds derive macros for `AppContext` and `VisualContext` so that it's easy to define further contexts in API code, such as `editor::BlockContext`. Release Notes: - N/A
This commit is contained in:
parent
29bfb56739
commit
a7c549b85b
24 changed files with 465 additions and 297 deletions
|
@ -1,11 +1,14 @@
|
|||
mod derive_app_context;
|
||||
mod derive_into_element;
|
||||
mod derive_path_static_str;
|
||||
mod derive_render;
|
||||
mod derive_visual_context;
|
||||
mod register_action;
|
||||
mod styles;
|
||||
mod test;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{DeriveInput, Ident};
|
||||
|
||||
/// register_action! can be used to register an action with the GPUI runtime.
|
||||
/// You should typically use `gpui::actions!` or `gpui::impl_actions!` instead,
|
||||
|
@ -34,6 +37,57 @@ pub fn derive_path_static_str(input: TokenStream) -> TokenStream {
|
|||
derive_path_static_str::derive_path_static_str(input)
|
||||
}
|
||||
|
||||
/// #[derive(AppContext)] is used to create a context out of anything that holds a `&mut App`
|
||||
/// Note that a `#[app]` attribute is required to identify the variable holding the &mut App.
|
||||
///
|
||||
/// Failure to add the attribute causes a compile error:
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # #[macro_use] extern crate gpui_macros;
|
||||
/// # #[macro_use] extern crate gpui;
|
||||
/// #[derive(AppContext)]
|
||||
/// struct MyContext<'a> {
|
||||
/// app: &'a mut gpui::App
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(AppContext, attributes(app))]
|
||||
pub fn derive_app_context(input: TokenStream) -> TokenStream {
|
||||
derive_app_context::derive_app_context(input)
|
||||
}
|
||||
|
||||
/// #[derive(VisualContext)] is used to create a visual context out of anything that holds a `&mut Window` and
|
||||
/// implements `AppContext`
|
||||
/// Note that a `#[app]` and a `#[window]` attribute are required to identify the variables holding the &mut App,
|
||||
/// and &mut Window respectively.
|
||||
///
|
||||
/// Failure to add both attributes causes a compile error:
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # #[macro_use] extern crate gpui_macros;
|
||||
/// # #[macro_use] extern crate gpui;
|
||||
/// #[derive(VisualContext)]
|
||||
/// struct MyContext<'a, 'b> {
|
||||
/// #[app]
|
||||
/// app: &'a mut gpui::App,
|
||||
/// window: &'b mut gpui::Window
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # #[macro_use] extern crate gpui_macros;
|
||||
/// # #[macro_use] extern crate gpui;
|
||||
/// #[derive(VisualContext)]
|
||||
/// struct MyContext<'a, 'b> {
|
||||
/// app: &'a mut gpui::App,
|
||||
/// #[window]
|
||||
/// window: &'b mut gpui::Window
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(VisualContext, attributes(window, app))]
|
||||
pub fn derive_visual_context(input: TokenStream) -> TokenStream {
|
||||
derive_visual_context::derive_visual_context(input)
|
||||
}
|
||||
|
||||
/// Used by GPUI to generate the style helpers.
|
||||
#[proc_macro]
|
||||
#[doc(hidden)]
|
||||
|
@ -115,3 +169,15 @@ pub fn box_shadow_style_methods(input: TokenStream) -> TokenStream {
|
|||
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
test::test(args, function)
|
||||
}
|
||||
|
||||
pub(crate) fn get_simple_attribute_field(ast: &DeriveInput, name: &'static str) -> Option<Ident> {
|
||||
match &ast.data {
|
||||
syn::Data::Struct(data_struct) => data_struct
|
||||
.fields
|
||||
.iter()
|
||||
.find(|field| field.attrs.iter().any(|attr| attr.path.is_ident(name)))
|
||||
.map(|field| field.ident.clone().unwrap()),
|
||||
syn::Data::Enum(_) => None,
|
||||
syn::Data::Union(_) => None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue