View plugins for literals

This commit is contained in:
2026-02-01 17:46:40 +01:00
parent 44a4837647
commit 76984187a0
3 changed files with 50 additions and 13 deletions
+25 -6
View File
@@ -146,14 +146,33 @@ fn emit_node_scope(node: &RsxNode) -> TokenStream {
fn emit_node(node: &RsxNode) -> TokenStream {
match node {
RsxNode::Text { text, deps } => {
RsxNode::Text {
text,
deps,
plugins,
} => {
let deps_emit = emit_deps(deps);
quote! {
scope.view(Arc::new({
#deps_emit
move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
}));
if plugins.len() == 0 {
quote! {
scope.view(Arc::new({
#deps_emit
move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
}));
}
} else {
let plugins_emit = emit_plugins(plugins);
quote! {
scope.view_wrapped(Arc::new({
#deps_emit
move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
}), std::sync::Arc::new(|ctx, view| {
let area = ctx.allocate(ctx.area.x, ctx.area.y, ctx.area.width, ctx.area.height);
ctx.draw_view(area, view.clone());
#plugins_emit
}));
}
}
}
+14 -5
View File
@@ -41,7 +41,11 @@ pub struct RsxProp {
/// AST node representing different RSX constructs
pub enum RsxNode {
/// String literal: `"text"`
Text { text: LitStr, deps: Vec<Dep> },
Text {
plugins: Vec<ViewPlugin>,
text: LitStr,
deps: Vec<Dep>,
},
/// Expression node: `{expr}`
Expr(Expr),
/// Component instantiation: `Component { prop: value, ... }`
@@ -212,6 +216,8 @@ impl Parse for RsxNode {
});
}
let plugins = parse_plugins(input)?;
// "%$dep for $pat in $expr { $rsx }"
if input.peek(LitStr) {
let text = input.parse()?;
@@ -229,16 +235,19 @@ impl Parse for RsxNode {
.collect(),
);
return Ok(RsxNode::Text { text, deps });
return Ok(RsxNode::Text {
text,
deps,
plugins,
});
}
parse_component_invocation(input)
parse_component_invocation(input, plugins)
}
}
/// Parses Path or Path { ... } into RsxNode::Component.
fn parse_component_invocation(input: ParseStream) -> Result<RsxNode> {
let plugins = parse_plugins(input)?;
fn parse_component_invocation(input: ParseStream, plugins: Vec<ViewPlugin>) -> Result<RsxNode> {
let path: Path = input.parse()?;
if !input.peek(Brace) {
+9
View File
@@ -53,4 +53,13 @@ impl Scope {
self.children.lock().unwrap().push((ctx, None));
}
/// Adds a view directly to this scope
pub fn view_wrapped(self: &Arc<Self>, view: View, view_wrapper: ViewWrapper) {
let ctx = Context::new(view, self.executor.clone());
ctx.refresh();
self.children.lock().unwrap().push((ctx, Some(view_wrapper)));
}
}