Browse Source

wip: move macro lib out of proc macro crate

Jonathan Kelley 3 năm trước cách đây
mục cha
commit
abfac0d59b

+ 4 - 1
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",

+ 27 - 0
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.
+            // }
+        }
+    })
+}

+ 1 - 0
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" }

+ 1 - 6
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::*;

+ 13 - 0
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"] }

+ 0 - 0
packages/core-macro/src/htm.rs → packages/macro-inner/src/htm.rs


+ 0 - 0
packages/core-macro/src/ifmt.rs → packages/macro-inner/src/ifmt.rs


+ 0 - 0
packages/core-macro/src/inlineprops.rs → packages/macro-inner/src/inlineprops.rs


+ 5 - 0
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;

+ 0 - 0
packages/core-macro/src/props/mod.rs → packages/macro-inner/src/props/mod.rs


+ 0 - 0
packages/core-macro/src/router.rs → packages/macro-inner/src/router.rs


+ 4 - 4
packages/core-macro/src/rsx/component.rs → packages/macro-inner/src/rsx/component.rs

@@ -23,10 +23,10 @@ use syn::{
 };
 
 pub struct Component {
-    name: syn::Path,
-    body: Vec<ComponentField>,
-    children: Vec<BodyNode>,
-    manual_props: Option<Expr>,
+    pub name: syn::Path,
+    pub body: Vec<ComponentField>,
+    pub children: Vec<BodyNode>,
+    pub manual_props: Option<Expr>,
 }
 
 impl Parse for Component {

+ 59 - 34
packages/core-macro/src/rsx/element.rs → 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<LitStr>,
-    attributes: Vec<ElementAttrNamed>,
-    listeners: Vec<ElementAttrNamed>,
-    children: Vec<BodyNode>,
-    _is_static: bool,
+    pub name: Ident,
+    pub key: Option<LitStr>,
+    pub attributes: Vec<ElementAttrNamed>,
+    pub children: Vec<BodyNode>,
+    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<ElementAttrNamed> = vec![];
-        let mut listeners: Vec<ElementAttrNamed> = vec![];
         let mut children: Vec<BodyNode> = vec![];
         let mut key = None;
         let mut _el_ref = None;
@@ -54,6 +52,7 @@ impl Parse for Element {
                     });
                 } else {
                     let value = content.parse::<Expr>()?;
+
                     attributes.push(ElementAttrNamed {
                         el_name: el_name.clone(),
                         attr: ElementAttr::CustomAttrExpression { name, value },
@@ -82,13 +81,24 @@ impl Parse for Element {
                 content.parse::<Token![:]>()?;
 
                 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::<ExprClosure>().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)

+ 2 - 2
packages/core-macro/src/rsx/mod.rs → packages/macro-inner/src/rsx/mod.rs

@@ -29,8 +29,8 @@ use syn::{
 };
 
 pub struct CallBody {
-    custom_context: Option<Ident>,
-    roots: Vec<BodyNode>,
+    pub custom_context: Option<Ident>,
+    pub roots: Vec<BodyNode>,
 }
 
 impl Parse for CallBody {

+ 0 - 0
packages/core-macro/src/rsx/node.rs → packages/macro-inner/src/rsx/node.rs


+ 0 - 0
packages/core-macro/src/rsxtemplate.rs → packages/macro-inner/src/rsxtemplate.rs