fix: dont choke on emojji n exprs

This commit is contained in:
Jonathan Kelley 2023-01-17 16:24:29 -08:00
parent ae1a618aa8
commit 4e4e101eee
5 changed files with 32 additions and 21 deletions

View file

@ -3,7 +3,7 @@ use std::fmt::{Result, Write};
use proc_macro2::Span;
use crate::Writer;
use crate::{collect_macros::byte_offset, Writer};
impl Writer<'_> {
pub fn write_raw_expr(&mut self, placement: Span) -> Result {
@ -16,7 +16,10 @@ impl Writer<'_> {
// if the expr is on one line, just write it directly
if start.line == end.line {
let row = &self.src[start.line - 1][start.column..end.column].trim();
// split counting utf8 chars
let start = byte_offset(self.raw_src, start);
let end = byte_offset(self.raw_src, end);
let row = self.raw_src[start..end].trim();
write!(self.out, "{}", row)?;
return Ok(());
}

View file

@ -53,10 +53,7 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
return formatted_blocks;
}
let mut writer = Writer {
src: contents.lines().collect::<Vec<_>>(),
..Writer::default()
};
let mut writer = Writer::new(contents);
// Dont parse nested macros
let mut end_span = LineColumn { column: 0, line: 0 };
@ -125,10 +122,7 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
}
pub fn write_block_out(body: CallBody) -> Option<String> {
let mut buf = Writer {
src: vec![""],
..Writer::default()
};
let mut buf = Writer::new("");
write_body(&mut buf, &body);
@ -157,10 +151,7 @@ fn write_body(buf: &mut Writer, body: &CallBody) {
pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option<String> {
let body = syn::parse2::<CallBody>(expr.mac.tokens).unwrap();
let mut buf = Writer {
src: raw.lines().collect(),
..Writer::default()
};
let mut buf = Writer::new(raw);
write_body(&mut buf, &body);
@ -170,10 +161,7 @@ pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option<String> {
pub fn fmt_block(block: &str, indent_level: usize) -> Option<String> {
let body = syn::parse_str::<dioxus_rsx::CallBody>(block).unwrap();
let mut buf = Writer {
src: block.lines().collect(),
..Writer::default()
};
let mut buf = Writer::new(block);
buf.out.indent = indent_level;

View file

@ -9,8 +9,9 @@ use syn::{spanned::Spanned, Expr, ExprIf};
use crate::buffer::Buffer;
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Writer<'a> {
pub raw_src: &'a str,
pub src: Vec<&'a str>,
pub cached_formats: HashMap<Location, String>,
pub comments: VecDeque<usize>,
@ -31,7 +32,18 @@ impl Location {
}
}
impl Writer<'_> {
impl<'a> Writer<'a> {
pub fn new(raw_src: &'a str) -> Self {
let src = raw_src.lines().collect();
Self {
raw_src,
src,
cached_formats: HashMap::new(),
comments: VecDeque::new(),
out: Buffer::default(),
}
}
// Expects to be written directly into place
pub fn write_ident(&mut self, node: &BodyNode) -> Result {
match node {

View file

@ -39,5 +39,6 @@ twoway![
t2,
reallylong,
immediate_expr,
collapse_expr
collapse_expr,
trailing_expr
];

View file

@ -0,0 +1,7 @@
fn it_works() {
cx.render(rsx! {
div {
span { "Description: ", package.description.as_deref().unwrap_or("❌❌❌❌ missing") }
}
})
}