Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T02:58:50.459Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

derive_app_context.rs

120 lines · 3.8 KB · rust
1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{DeriveInput, parse_macro_input};
4
5use crate::get_simple_attribute_field;
6
7pub fn derive_app_context(input: TokenStream) -> TokenStream {
8    let ast = parse_macro_input!(input as DeriveInput);
9
10    let Some(app_variable) = get_simple_attribute_field(&ast, "app") else {
11        return quote! {
12            compile_error!("Derive must have an #[app] attribute to detect the &mut App field");
13        }
14        .into();
15    };
16
17    let type_name = &ast.ident;
18    let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
19
20    let r#gen = quote! {
21        impl #impl_generics gpui::AppContext for #type_name #type_generics
22        #where_clause
23        {
24            fn new<T: 'static>(
25                &mut self,
26                build_entity: impl FnOnce(&mut gpui::Context<'_, T>) -> T,
27            ) -> gpui::Entity<T> {
28                self.#app_variable.new(build_entity)
29            }
30
31            fn reserve_entity<T: 'static>(&mut self) -> gpui::Reservation<T> {
32                self.#app_variable.reserve_entity()
33            }
34
35            fn insert_entity<T: 'static>(
36                &mut self,
37                reservation: gpui::Reservation<T>,
38                build_entity: impl FnOnce(&mut gpui::Context<'_, T>) -> T,
39            ) -> gpui::Entity<T> {
40                self.#app_variable.insert_entity(reservation, build_entity)
41            }
42
43            fn update_entity<T, R>(
44                &mut self,
45                handle: &gpui::Entity<T>,
46                update: impl FnOnce(&mut T, &mut gpui::Context<'_, T>) -> R,
47            ) -> R
48            where
49                T: 'static,
50            {
51                self.#app_variable.update_entity(handle, update)
52            }
53
54            fn as_mut<'y, 'z, T>(
55                &'y mut self,
56                handle: &'z gpui::Entity<T>,
57            ) -> gpui::GpuiBorrow<'y, T>
58            where
59                T: 'static,
60            {
61                self.#app_variable.as_mut(handle)
62            }
63
64            fn read_entity<T, R>(
65                &self,
66                handle: &gpui::Entity<T>,
67                read: impl FnOnce(&T, &gpui::App) -> R,
68            ) -> R
69            where
70                T: 'static,
71            {
72                self.#app_variable.read_entity(handle, read)
73            }
74
75            fn update_window<T, F>(&mut self, window: gpui::AnyWindowHandle, f: F) -> gpui::Result<T>
76            where
77                F: FnOnce(gpui::AnyView, &mut gpui::Window, &mut gpui::App) -> T,
78            {
79                self.#app_variable.update_window(window, f)
80            }
81
82            fn with_window<R>(
83                &mut self,
84                entity_id: gpui::EntityId,
85                f: impl FnOnce(&mut gpui::Window, &mut gpui::App) -> R,
86            ) -> Option<R>
87            {
88                self.#app_variable.with_window(entity_id, f)
89            }
90
91            fn read_window<T, R>(
92                &self,
93                window: &gpui::WindowHandle<T>,
94                read: impl FnOnce(gpui::Entity<T>, &gpui::App) -> R,
95            ) -> gpui::Result<R>
96            where
97                T: 'static,
98            {
99                self.#app_variable.read_window(window, read)
100            }
101
102            fn background_spawn<R>(&self, future: impl std::future::Future<Output = R> + Send + 'static) -> gpui::Task<R>
103            where
104                R: Send + 'static,
105            {
106                self.#app_variable.background_spawn(future)
107            }
108
109            fn read_global<G, R>(&self, callback: impl FnOnce(&G, &gpui::App) -> R) -> R
110            where
111                G: gpui::Global,
112            {
113                self.#app_variable.read_global(callback)
114            }
115        }
116    };
117
118    r#gen.into()
119}
120
Served at tenant.openagents/omega Member data and write actions are omitted.