Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into taffy

Nathan Sobo740b1053307c
diff --cc Cargo.lock
index 37af2d52f32,1ff9981a6a0..8cabfe11a89
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -3173,7 -3172,7 +3173,8 @@@ dependencies =
name = "gpui_macros"
version = "0.1.0"
dependencies = [
+ "gpui",
+ "lazy_static",
"proc-macro2",
"quote",
"syn 1.0.109",
diff --cc crates/gpui/playground/src/element.rs
index 7198404919d,00000000000..fb5592d7a01
mode 100644,000000..100644
--- a/crates/gpui/playground/src/element.rs
+++ b/crates/gpui/playground/src/element.rs
@@@ -1,279 -1,0 +1,296 @@@
+use crate::{
+ adapter::Adapter,
- style::{DefinedLength, Display, Overflow, Position, Style},
++ style::{DefinedLength, Display, Fill, Overflow, Position, Style},
+};
+use anyhow::Result;
+use derive_more::{Deref, DerefMut};
+use gpui::{Layout, LayoutContext as LegacyLayoutContext, PaintContext as LegacyPaintContext};
+use playground_macros::tailwind_lengths;
+pub use taffy::tree::NodeId;
+
+#[derive(Deref, DerefMut)]
+pub struct LayoutContext<'a, 'b, 'c, 'd, V> {
+ pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>,
+}
+
+#[derive(Deref, DerefMut)]
+pub struct PaintContext<'a, 'b, 'c, 'd, V> {
+ #[deref]
+ #[deref_mut]
+ pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>,
+ pub(crate) scene: &'d mut gpui::SceneBuilder,
+}
+
+pub trait Element<V: 'static>: 'static + Clone {
+ fn style_mut(&mut self) -> &mut Style;
+ fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId>;
+ fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()>;
+
+ /// Convert to a dynamically-typed element suitable for layout and paint.
+ fn into_any(self) -> AnyElement<V>
+ where
+ Self: 'static + Sized,
+ {
+ AnyElement {
+ element: Box::new(self) as Box<dyn ElementObject<V>>,
+ layout_node_id: None,
+ }
+ }
+
+ fn adapt(self) -> Adapter<V>
+ where
+ Self: Sized,
+ Self: Element<V>,
+ {
+ Adapter(self.into_any())
+ }
+
+ // Display ////////////////////
+
+ fn block(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().display = Display::Block;
+ self
+ }
+
+ fn flex(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().display = Display::Flex;
+ self
+ }
+
+ fn grid(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().display = Display::Grid;
+ self
+ }
+
+ // style::Overflow ///////////////////
+
+ fn overflow_visible(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Visible;
+ self.style_mut().overflow.y = Overflow::Visible;
+ self
+ }
+
+ fn overflow_hidden(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Hidden;
+ self.style_mut().overflow.y = Overflow::Hidden;
+ self
+ }
+
+ fn overflow_scroll(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Scroll;
+ self.style_mut().overflow.y = Overflow::Scroll;
+ self
+ }
+
+ fn overflow_x_visible(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Visible;
+ self
+ }
+
+ fn overflow_x_hidden(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Hidden;
+ self
+ }
+
+ fn overflow_x_scroll(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.x = Overflow::Scroll;
+ self
+ }
+
+ fn overflow_y_visible(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.y = Overflow::Visible;
+ self
+ }
+
+ fn overflow_y_hidden(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.y = Overflow::Hidden;
+ self
+ }
+
+ fn overflow_y_scroll(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().overflow.y = Overflow::Scroll;
+ self
+ }
+
+ // Position ///////////////////
+
+ fn relative(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().position = Position::Relative;
+ self
+ }
+
+ fn absolute(mut self) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().position = Position::Absolute;
+
+ self
+ }
+
+ #[tailwind_lengths]
+ fn inset(mut self, length: DefinedLength) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().inset.top = length;
+ self.style_mut().inset.right = length;
+ self.style_mut().inset.bottom = length;
+ self.style_mut().inset.left = length;
+ self
+ }
+
+ #[tailwind_lengths]
+ fn w(mut self, length: DefinedLength) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().size.width = length;
+ self
+ }
+
+ #[tailwind_lengths]
+ fn min_w(mut self, length: DefinedLength) -> Self
+ where
+ Self: Sized,
+ {
- self.style_mut().size.width = length;
++ self.style_mut().min_size.width = length;
+ self
+ }
+
+ #[tailwind_lengths]
+ fn h(mut self, length: DefinedLength) -> Self
+ where
+ Self: Sized,
+ {
+ self.style_mut().size.height = length;
+ self
+ }
++
++ #[tailwind_lengths]
++ fn min_h(mut self, length: DefinedLength) -> Self
++ where
++ Self: Sized,
++ {
++ self.style_mut().min_size.height = length;
++ self
++ }
++
++ fn fill(mut self, fill: impl Into<Fill>) -> Self
++ where
++ Self: Sized,
++ {
++ self.style_mut().fill = fill.into();
++ self
++ }
+}
+
+pub trait ElementObject<V> {
+ fn style_mut(&mut self) -> &mut Style;
+ fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId>;
+ fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()>;
+ fn clone_object(&self) -> Box<dyn ElementObject<V>>;
+}
+
+impl<V: 'static, E: Element<V>> ElementObject<V> for E {
+ fn style_mut(&mut self) -> &mut Style {
+ self.style_mut()
+ }
+
+ fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> {
+ self.layout(view, cx)
+ }
+
+ fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
+ self.paint(layout, view, cx)
+ }
+
+ fn clone_object(&self) -> Box<dyn ElementObject<V>> {
+ Box::new(Clone::clone(self))
+ }
+}
+
+pub struct AnyElement<V> {
+ element: Box<dyn ElementObject<V>>,
+ layout_node_id: Option<NodeId>,
+}
+
+impl<V> AnyElement<V> {
+ pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> {
+ let layout_node_id = self.element.layout(view, cx)?;
+ self.layout_node_id = Some(layout_node_id);
+ Ok(layout_node_id)
+ }
+
+ pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
+ let layout_node_id = self.layout_node_id.expect("paint called before layout");
+ let layout = cx
+ .layout_engine()
+ .unwrap()
+ .computed_layout(layout_node_id)
+ .expect("you can currently only use playground elements within an adapter");
+ self.element.paint(layout, view, cx)
+ }
+}
+
+impl<V> Clone for AnyElement<V> {
+ fn clone(&self) -> Self {
+ Self {
+ element: self.element.clone_object(),
+ layout_node_id: self.layout_node_id,
+ }
+ }
+}
+
+impl<V: 'static> Element<V> for AnyElement<V> {
+ fn style_mut(&mut self) -> &mut Style {
+ self.element.style_mut()
+ }
+
+ fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> {
+ self.layout(view, cx)
+ }
+
+ fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
+ self.paint(view, cx)
+ }
+}
diff --cc crates/gpui/src/elements/container.rs
index f07e425d4b3,698100ab296..52954db78ee
--- a/crates/gpui/src/elements/container.rs
+++ b/crates/gpui/src/elements/container.rs
@@@ -9,8 -9,9 +9,8 @@@ use crate::
},
json::ToJson,
platform::CursorStyle,
- scene::{self, Border, CursorRegion, Quad},
+ scene::{self, Border, CornerRadii, CursorRegion, Quad},
- AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View,
- ViewContext,
+ AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, ViewContext,
};
use schemars::JsonSchema;
use serde::Deserialize;
diff --cc crates/gpui/src/elements/tooltip.rs
index 9189d6da0ea,14f3809e67b..58efb9b5e37
--- a/crates/gpui/src/elements/tooltip.rs
+++ b/crates/gpui/src/elements/tooltip.rs
@@@ -51,10 -52,10 +52,10 @@@ pub struct KeystrokeStyle
text: TextStyle,
}
-impl<V: View> Tooltip<V> {
+impl<V: 'static> Tooltip<V> {
- pub fn new<Tag: 'static, T: 'static>(
+ pub fn new<Tag: 'static>(
id: usize,
- text: String,
+ text: impl Into<Cow<'static, str>>,
action: Option<Box<dyn Action>>,
style: TooltipStyle,
child: AnyElement<V>,
diff --cc crates/gpui_macros/Cargo.toml
index c11a4af33df,9ff340299b6..84b0ac55c5e
--- a/crates/gpui_macros/Cargo.toml
+++ b/crates/gpui_macros/Cargo.toml
@@@ -10,7 -10,9 +10,10 @@@ proc-macro = tru
doctest = false
[dependencies]
+lazy_static.workspace = true
+proc-macro2 = "1.0"
syn = "1.0"
quote = "1.0"
-proc-macro2 = "1.0"
+
+ [dev-dependencies]
+ gpui = { path = "../gpui" }
diff --cc crates/gpui_macros/src/gpui_macros.rs
index a225759cd16,2e6c7258727..7f70bc6a911
--- a/crates/gpui_macros/src/gpui_macros.rs
+++ b/crates/gpui_macros/src/gpui_macros.rs
@@@ -278,44 -278,18 +278,44 @@@ fn parse_bool(literal: &Lit) -> Result<
#[proc_macro_derive(Element)]
pub fn element_derive(input: TokenStream) -> TokenStream {
- // Parse the input tokens into a syntax tree
- let input = parse_macro_input!(input as DeriveInput);
+ let ast = parse_macro_input!(input as DeriveInput);
+ let type_name = ast.ident;
- let placeholder_view_generics: Generics = parse_quote! { <V> };
- // The name of the struct/enum
- let name = input.ident;
- let must_implement = format_ident!("{}MustImplementRenderElement", name);
++ let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
+ let placeholder_view_type_name: Ident = parse_quote! { V };
+ let view_type_name: Ident;
+ let impl_generics: syn::ImplGenerics<'_>;
+ let type_generics: Option<syn::TypeGenerics<'_>>;
+ let where_clause: Option<&'_ WhereClause>;
- let expanded = quote! {
- trait #must_implement : gpui::elements::RenderElement {}
- impl #must_implement for #name {}
+ match ast.generics.params.iter().find_map(|param| {
+ if let GenericParam::Type(type_param) = param {
+ Some(type_param.ident.clone())
+ } else {
+ None
+ }
+ }) {
+ Some(type_name) => {
+ view_type_name = type_name;
+ let generics = ast.generics.split_for_impl();
+ impl_generics = generics.0;
+ type_generics = Some(generics.1);
+ where_clause = generics.2;
+ }
+ _ => {
+ view_type_name = placeholder_view_type_name;
+ let generics = placeholder_view_generics.split_for_impl();
+ impl_generics = generics.0;
+ type_generics = None;
+ where_clause = generics.2;
+ }
+ }
+
+ let gen = quote! {
+ impl #impl_generics Element<#view_type_name> for #type_name #type_generics
+ #where_clause
+ {
- impl<V: gpui::View> gpui::elements::Element<V> for #name {
type LayoutState = gpui::elements::AnyElement<V>;
type PaintState = ();
diff --cc crates/gpui_macros/tests/test.rs
index 00000000000,b5a91a7e980..1c47e623ea8
mode 000000,100644..100644
--- a/crates/gpui_macros/tests/test.rs
+++ b/crates/gpui_macros/tests/test.rs
@@@ -1,0 -1,14 +1,14 @@@
-use gpui::{elements::RenderElement, View, ViewContext};
-use gpui_macros::Element;
++use gpui::{elements::Empty, Element, ViewContext};
++// use gpui_macros::Element;
+
+ #[test]
+ fn test_derive_render_element() {
+ #[derive(Element)]
+ struct TestElement {}
+
- impl RenderElement for TestElement {
- fn render<V: View>(&mut self, _: &mut V, _: &mut ViewContext<V>) -> gpui::AnyElement<V> {
- unimplemented!()
++ impl TestElement {
++ fn render<V: 'static>(&mut self, _: &mut V, _: &mut ViewContext<V>) -> impl Element<V> {
++ Empty::new()
+ }
+ }
+ }