Hotreload the contents of for loops, if chains, component bodies, props, attributes, and any literals discovered in rsx! Add a TUI renderer to the CLI. Improve the CLI build system to be async and parallel. Refactor RSX to allow partial expansion of expressions. Merge autofmt implementations for consistency. Merge the representation of elements and components under the hood. Add a diagnostics system for rsx for improved error messages. Drop interprocess and move to websockets for communication between the CLI and the server. Assign IDs to nodes and attributes in a stable way to be used in non compiler contexts. Add hotreloading to any body of component/for loop/if chain/etc. --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com> Co-authored-by: Liam Mitchell <liamkarlmitchell@gmail.com>
116 lines
3.0 KiB
Rust
116 lines
3.0 KiB
Rust
#[cfg(feature = "hot_reload")]
|
|
use dioxus_core::TemplateNode;
|
|
|
|
use crate::{
|
|
literal::{HotLiteral, HotLiteralType},
|
|
location::DynIdx,
|
|
IfmtInput,
|
|
};
|
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
|
use quote::ToTokens;
|
|
use quote::{quote, TokenStreamExt};
|
|
use syn::Result;
|
|
use syn::{
|
|
parse::{Parse, ParseStream},
|
|
LitStr,
|
|
};
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
|
|
pub struct TextNode {
|
|
pub input: IfmtInput,
|
|
pub hr_idx: DynIdx,
|
|
pub dyn_idx: DynIdx,
|
|
}
|
|
|
|
impl Parse for TextNode {
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
|
Ok(Self {
|
|
input: input.parse()?,
|
|
hr_idx: DynIdx::default(),
|
|
dyn_idx: DynIdx::default(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl ToTokens for TextNode {
|
|
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
|
let txt = &self.input;
|
|
|
|
if txt.is_static() {
|
|
tokens.append_all(quote! {
|
|
dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt.to_string()))
|
|
})
|
|
} else {
|
|
// todo:
|
|
// Use the RsxLiteral implementation to spit out a hotreloadable variant of this string
|
|
// This is not super efficient since we're doing a bit of cloning
|
|
let as_lit = HotLiteral {
|
|
hr_idx: self.hr_idx.clone(),
|
|
value: HotLiteralType::Fmted(txt.clone()),
|
|
};
|
|
|
|
tokens.append_all(quote! {
|
|
dioxus_core::DynamicNode::Text(dioxus_core::VText::new( #as_lit ))
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
impl TextNode {
|
|
pub fn from_text(text: &str) -> Self {
|
|
let ifmt = IfmtInput {
|
|
source: LitStr::new(text, Span::call_site()),
|
|
segments: vec![],
|
|
};
|
|
Self {
|
|
input: ifmt,
|
|
dyn_idx: Default::default(),
|
|
hr_idx: Default::default(),
|
|
}
|
|
}
|
|
|
|
pub fn is_static(&self) -> bool {
|
|
self.input.is_static()
|
|
}
|
|
|
|
#[cfg(feature = "hot_reload")]
|
|
pub fn to_template_node(&self) -> TemplateNode {
|
|
use crate::intern;
|
|
match self.is_static() {
|
|
true => {
|
|
let text = self.input.source.clone();
|
|
let text = intern(text.value().as_str());
|
|
TemplateNode::Text { text }
|
|
}
|
|
false => TemplateNode::Dynamic {
|
|
id: self.dyn_idx.get(),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::PrettyUnparse;
|
|
|
|
#[test]
|
|
fn parses() {
|
|
let input = syn::parse2::<TextNode>(quote! { "hello world" }).unwrap();
|
|
assert_eq!(input.input.source.value(), "hello world");
|
|
}
|
|
|
|
#[test]
|
|
fn to_tokens_with_hr() {
|
|
let lit = syn::parse2::<TextNode>(quote! { "hi {world1} {world2} {world3}" }).unwrap();
|
|
println!("{}", lit.to_token_stream().pretty_unparse());
|
|
}
|
|
|
|
#[test]
|
|
fn raw_str() {
|
|
let input = syn::parse2::<TextNode>(quote! { r#"hello world"# }).unwrap();
|
|
println!("{}", input.input.source.to_token_stream());
|
|
assert_eq!(input.input.source.value(), "hello world");
|
|
}
|
|
}
|