From abfac0d59b620cdf3af77c80aadb07266e0d651b Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 17 Jan 2022 16:37:44 -0500 Subject: [PATCH] wip: move macro lib out of proc macro crate --- Cargo.toml | 5 +- examples/rsx_autocomplete.rs | 27 ++++++ packages/core-macro/Cargo.toml | 1 + packages/core-macro/src/lib.rs | 7 +- packages/macro-inner/Cargo.toml | 13 +++ .../{core-macro => macro-inner}/src/htm.rs | 0 .../{core-macro => macro-inner}/src/ifmt.rs | 0 .../src/inlineprops.rs | 0 packages/macro-inner/src/lib.rs | 5 + .../src/props/mod.rs | 0 .../{core-macro => macro-inner}/src/router.rs | 0 .../src/rsx/component.rs | 8 +- .../src/rsx/element.rs | 93 ++++++++++++------- .../src/rsx/mod.rs | 4 +- .../src/rsx/node.rs | 0 .../src/rsxtemplate.rs | 0 16 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 examples/rsx_autocomplete.rs create mode 100644 packages/macro-inner/Cargo.toml rename packages/{core-macro => macro-inner}/src/htm.rs (100%) rename packages/{core-macro => macro-inner}/src/ifmt.rs (100%) rename packages/{core-macro => macro-inner}/src/inlineprops.rs (100%) create mode 100644 packages/macro-inner/src/lib.rs rename packages/{core-macro => macro-inner}/src/props/mod.rs (100%) rename packages/{core-macro => macro-inner}/src/router.rs (100%) rename packages/{core-macro => macro-inner}/src/rsx/component.rs (98%) rename packages/{core-macro => macro-inner}/src/rsx/element.rs (80%) rename packages/{core-macro => macro-inner}/src/rsx/mod.rs (97%) rename packages/{core-macro => macro-inner}/src/rsx/node.rs (100%) rename packages/{core-macro => macro-inner}/src/rsxtemplate.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 884513a5b..98da05bcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ keywords = ["dom", "ui", "gui", "react", "wasm"] dioxus-core = { path = "./packages/core", version = "^0.1.7" } dioxus-html = { path = "./packages/html", version = "^0.1.4", optional = true } dioxus-core-macro = { path = "./packages/core-macro", version = "^0.1.6", optional = true } +dioxus-macro-inner = { path = "./packages/macro-inner", optional = true } dioxus-hooks = { path = "./packages/hooks", version = "^0.1.6", optional = true } dioxus-web = { path = "./packages/web", version = "^0.0.4", optional = true } @@ -27,7 +28,7 @@ dioxus-mobile = { path = "./packages/mobile", version = "^0.0.3", optional = tru [features] default = ["macro", "hooks", "html"] -macro = ["dioxus-core-macro"] +macro = ["dioxus-core-macro", "dioxus-macro-inner"] hooks = ["dioxus-hooks"] html = ["dioxus-html"] ssr = ["dioxus-ssr"] @@ -35,6 +36,7 @@ web = ["dioxus-web"] desktop = ["dioxus-desktop"] router = ["dioxus-router"] + # "dioxus-router/web" # "dioxus-router/desktop" # desktop = ["dioxus-desktop", "dioxus-router/desktop"] @@ -46,6 +48,7 @@ router = ["dioxus-router"] members = [ "packages/core", "packages/core-macro", + "packages/macro-inner", "packages/html", "packages/hooks", "packages/web", diff --git a/examples/rsx_autocomplete.rs b/examples/rsx_autocomplete.rs new file mode 100644 index 000000000..99afe7521 --- /dev/null +++ b/examples/rsx_autocomplete.rs @@ -0,0 +1,27 @@ +//! This example shows that autocomplete works in RSX + +use dioxus::prelude::*; + +fn main() { + dioxus::desktop::launch(app); +} + +fn app(cx: Scope) -> Element { + cx.render(rsx! { + div { + onclick: move |_| { + } + // class: "asd", + // style { + // media: "Ad", + // } + // div { + + // } + // { + // let t = String::new(); + // t. + // } + } + }) +} diff --git a/packages/core-macro/Cargo.toml b/packages/core-macro/Cargo.toml index ee3dc2883..8f2b81cba 100644 --- a/packages/core-macro/Cargo.toml +++ b/packages/core-macro/Cargo.toml @@ -15,6 +15,7 @@ keywords = ["dom", "ui", "gui", "react", "wasm"] proc-macro = true [dependencies] +dioxus-macro-inner = { path = "../macro-inner" } once_cell = "1.8" proc-macro-error = "1.0.4" proc-macro2 = { version = "1.0.6" } diff --git a/packages/core-macro/src/lib.rs b/packages/core-macro/src/lib.rs index 4ad7543a2..3579039b5 100644 --- a/packages/core-macro/src/lib.rs +++ b/packages/core-macro/src/lib.rs @@ -1,13 +1,8 @@ +use dioxus_macro_inner::*; use proc_macro::TokenStream; use quote::ToTokens; use syn::parse_macro_input; -pub(crate) mod ifmt; -pub(crate) mod inlineprops; -pub(crate) mod props; -pub(crate) mod router; -pub(crate) mod rsx; - #[proc_macro] pub fn format_args_f(input: TokenStream) -> TokenStream { use ifmt::*; diff --git a/packages/macro-inner/Cargo.toml b/packages/macro-inner/Cargo.toml new file mode 100644 index 000000000..63fcbbd9b --- /dev/null +++ b/packages/macro-inner/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "dioxus-macro-inner" +version = "0.0.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +once_cell = "1.8" +proc-macro-error = "1.0.4" +proc-macro2 = { version = "1.0.6" } +quote = "1.0" +syn = { version = "1.0.11", features = ["full", "extra-traits"] } diff --git a/packages/core-macro/src/htm.rs b/packages/macro-inner/src/htm.rs similarity index 100% rename from packages/core-macro/src/htm.rs rename to packages/macro-inner/src/htm.rs diff --git a/packages/core-macro/src/ifmt.rs b/packages/macro-inner/src/ifmt.rs similarity index 100% rename from packages/core-macro/src/ifmt.rs rename to packages/macro-inner/src/ifmt.rs diff --git a/packages/core-macro/src/inlineprops.rs b/packages/macro-inner/src/inlineprops.rs similarity index 100% rename from packages/core-macro/src/inlineprops.rs rename to packages/macro-inner/src/inlineprops.rs diff --git a/packages/macro-inner/src/lib.rs b/packages/macro-inner/src/lib.rs new file mode 100644 index 000000000..69e4b743f --- /dev/null +++ b/packages/macro-inner/src/lib.rs @@ -0,0 +1,5 @@ +pub mod ifmt; +pub mod inlineprops; +pub mod props; +pub mod router; +pub mod rsx; diff --git a/packages/core-macro/src/props/mod.rs b/packages/macro-inner/src/props/mod.rs similarity index 100% rename from packages/core-macro/src/props/mod.rs rename to packages/macro-inner/src/props/mod.rs diff --git a/packages/core-macro/src/router.rs b/packages/macro-inner/src/router.rs similarity index 100% rename from packages/core-macro/src/router.rs rename to packages/macro-inner/src/router.rs diff --git a/packages/core-macro/src/rsx/component.rs b/packages/macro-inner/src/rsx/component.rs similarity index 98% rename from packages/core-macro/src/rsx/component.rs rename to packages/macro-inner/src/rsx/component.rs index 5257022a4..26305359d 100644 --- a/packages/core-macro/src/rsx/component.rs +++ b/packages/macro-inner/src/rsx/component.rs @@ -23,10 +23,10 @@ use syn::{ }; pub struct Component { - name: syn::Path, - body: Vec, - children: Vec, - manual_props: Option, + pub name: syn::Path, + pub body: Vec, + pub children: Vec, + pub manual_props: Option, } impl Parse for Component { diff --git a/packages/core-macro/src/rsx/element.rs b/packages/macro-inner/src/rsx/element.rs similarity index 80% rename from packages/core-macro/src/rsx/element.rs rename to packages/macro-inner/src/rsx/element.rs index ebc401b6e..c612e9f1b 100644 --- a/packages/core-macro/src/rsx/element.rs +++ b/packages/macro-inner/src/rsx/element.rs @@ -4,19 +4,18 @@ use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ parse::{Parse, ParseBuffer, ParseStream}, - Expr, Ident, LitStr, Result, Token, + Expr, ExprClosure, Ident, LitStr, Result, Token, }; // ======================================= // Parse the VNode::Element type // ======================================= pub struct Element { - name: Ident, - key: Option, - attributes: Vec, - listeners: Vec, - children: Vec, - _is_static: bool, + pub name: Ident, + pub key: Option, + pub attributes: Vec, + pub children: Vec, + pub _is_static: bool, } impl Parse for Element { @@ -28,7 +27,6 @@ impl Parse for Element { syn::braced!(content in stream); let mut attributes: Vec = vec![]; - let mut listeners: Vec = vec![]; let mut children: Vec = vec![]; let mut key = None; let mut _el_ref = None; @@ -54,6 +52,7 @@ impl Parse for Element { }); } else { let value = content.parse::()?; + attributes.push(ElementAttrNamed { el_name: el_name.clone(), attr: ElementAttr::CustomAttrExpression { name, value }, @@ -82,13 +81,24 @@ impl Parse for Element { content.parse::()?; if name_str.starts_with("on") { - listeners.push(ElementAttrNamed { - el_name: el_name.clone(), - attr: ElementAttr::EventTokens { - name, - tokens: content.parse()?, - }, - }); + if content.fork().parse::().is_ok() { + // + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::EventClosure { + name, + closure: content.parse()?, + }, + }); + } else { + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::EventTokens { + name, + tokens: content.parse()?, + }, + }); + } } else { match name_str.as_str() { "key" => { @@ -182,7 +192,7 @@ impl Parse for Element { name: el_name, attributes, children, - listeners, + // listeners, _is_static: false, }) } @@ -193,14 +203,29 @@ impl ToTokens for Element { let name = &self.name; let children = &self.children; - let listeners = &self.listeners; - let attr = &self.attributes; + // let listeners = &self.listeners; let key = match &self.key { Some(ty) => quote! { Some(format_args_f!(#ty)) }, None => quote! { None }, }; + let listeners = self.attributes.iter().filter(|f| { + if let ElementAttr::EventTokens { .. } = f.attr { + true + } else { + false + } + }); + + let attr = self.attributes.iter().filter(|f| { + if let ElementAttr::EventTokens { .. } = f.attr { + false + } else { + true + } + }); + tokens.append_all(quote! { __cx.element( dioxus_elements::#name, @@ -213,29 +238,29 @@ impl ToTokens for Element { } } -enum ElementAttr { - // attribute: "valuee {}" +pub enum ElementAttr { + /// attribute: "valuee {}" AttrText { name: Ident, value: LitStr }, - // attribute: true, + /// attribute: true, AttrExpression { name: Ident, value: Expr }, - // "attribute": "value {}" + /// "attribute": "value {}" CustomAttrText { name: LitStr, value: LitStr }, - // "attribute": true, + /// "attribute": true, CustomAttrExpression { name: LitStr, value: Expr }, - // // onclick: move |_| {} - // EventClosure { name: Ident, closure: ExprClosure }, + /// onclick: move |_| {} + EventClosure { name: Ident, closure: ExprClosure }, - // onclick: {} + /// onclick: {} EventTokens { name: Ident, tokens: Expr }, } -struct ElementAttrNamed { - el_name: Ident, - attr: ElementAttr, +pub struct ElementAttrNamed { + pub el_name: Ident, + pub attr: ElementAttr, } impl ToTokens for ElementAttrNamed { @@ -263,11 +288,11 @@ impl ToTokens for ElementAttrNamed { __cx.attr( #name, format_args_f!(#value), None, false ) } } - // ElementAttr::EventClosure { name, closure } => { - // quote! { - // dioxus_elements::on::#name(__cx, #closure) - // } - // } + ElementAttr::EventClosure { name, closure } => { + quote! { + dioxus_elements::on::#name(__cx, #closure) + } + } ElementAttr::EventTokens { name, tokens } => { quote! { dioxus_elements::on::#name(__cx, #tokens) diff --git a/packages/core-macro/src/rsx/mod.rs b/packages/macro-inner/src/rsx/mod.rs similarity index 97% rename from packages/core-macro/src/rsx/mod.rs rename to packages/macro-inner/src/rsx/mod.rs index 5ff5d85f7..87b324dfc 100644 --- a/packages/core-macro/src/rsx/mod.rs +++ b/packages/macro-inner/src/rsx/mod.rs @@ -29,8 +29,8 @@ use syn::{ }; pub struct CallBody { - custom_context: Option, - roots: Vec, + pub custom_context: Option, + pub roots: Vec, } impl Parse for CallBody { diff --git a/packages/core-macro/src/rsx/node.rs b/packages/macro-inner/src/rsx/node.rs similarity index 100% rename from packages/core-macro/src/rsx/node.rs rename to packages/macro-inner/src/rsx/node.rs diff --git a/packages/core-macro/src/rsxtemplate.rs b/packages/macro-inner/src/rsxtemplate.rs similarity index 100% rename from packages/core-macro/src/rsxtemplate.rs rename to packages/macro-inner/src/rsxtemplate.rs