diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index 43e556bcb..a949e69f4 100644 --- a/packages/autofmt/src/element.rs +++ b/packages/autofmt/src/element.rs @@ -328,10 +328,8 @@ impl Writer<'_> { None } } - [BodyNode::RawExpr(ref expr)] => { - // TODO: let rawexprs to be inlined - get_expr_length(expr) - } + // TODO: let rawexprs to be inlined + [BodyNode::RawExpr(ref expr)] => get_expr_length(expr), [BodyNode::Element(ref el)] => { let attr_len = self.is_short_attrs(&el.attributes); diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index 5b98fb1b6..82cebffed 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -79,12 +79,7 @@ pub fn fmt_file(contents: &str) -> Vec { .count() / 4; - // Oneliner optimization - if writer.is_short_children(&body.roots).is_some() { - writer.write_ident(&body.roots[0]).unwrap(); - } else { - writer.write_body_indented(&body.roots).unwrap(); - } + write_body(&mut writer, &body); // writing idents leaves the final line ended at the end of the last ident if writer.out.buf.contains('\n') { @@ -106,8 +101,8 @@ pub fn fmt_file(contents: &str) -> Vec { let end = byte_offset(contents, span.end()) - 1; // Rustfmt will remove the space between the macro and the opening paren if the macro is a single expression - let body_is_solo_expr = - body.roots.len() == 1 && matches!(body.roots[0], BodyNode::RawExpr(_)); + let body_is_solo_expr = body.roots.len() == 1 + && matches!(body.roots[0], BodyNode::RawExpr(_) | BodyNode::Text(_)); if formatted.len() <= 80 && !formatted.contains('\n') && !body_is_solo_expr { formatted = format!(" {} ", formatted); @@ -135,14 +130,28 @@ pub fn write_block_out(body: CallBody) -> Option { ..Writer::default() }; - // Oneliner optimization + write_body(&mut buf, &body); + + buf.consume() +} + +fn write_body(buf: &mut Writer, body: &CallBody) { + use std::fmt::Write; + if buf.is_short_children(&body.roots).is_some() { - buf.write_ident(&body.roots[0]).unwrap(); + // write all the indents with spaces and commas between + for idx in 0..body.roots.len() - 1 { + let ident = &body.roots[idx]; + buf.write_ident(ident).unwrap(); + write!(&mut buf.out.buf, ", ").unwrap(); + } + + // write the last ident without a comma + let ident = &body.roots[body.roots.len() - 1]; + buf.write_ident(ident).unwrap(); } else { buf.write_body_indented(&body.roots).unwrap(); } - - buf.consume() } pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option { @@ -153,12 +162,7 @@ pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option { ..Writer::default() }; - // Oneliner optimization - if buf.is_short_children(&body.roots).is_some() { - buf.write_ident(&body.roots[0]).unwrap(); - } else { - buf.write_body_indented(&body.roots).unwrap(); - } + write_body(&mut buf, &body); buf.consume() } @@ -173,12 +177,7 @@ pub fn fmt_block(block: &str, indent_level: usize) -> Option { buf.out.indent = indent_level; - // Oneliner optimization - if buf.is_short_children(&body.roots).is_some() { - buf.write_ident(&body.roots[0]).unwrap(); - } else { - buf.write_body_indented(&body.roots).unwrap(); - } + write_body(&mut buf, &body); // writing idents leaves the final line ended at the end of the last ident if buf.out.buf.contains('\n') { @@ -195,8 +194,6 @@ pub fn apply_format(input: &str, block: FormattedBlock) -> String { let (left, _) = input.split_at(start); let (_, right) = input.split_at(end); - // dbg!(&block.formatted); - format!("{}{}{}", left, block.formatted, right) } diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs index b3198f9a7..84c48fde2 100644 --- a/packages/autofmt/tests/samples.rs +++ b/packages/autofmt/tests/samples.rs @@ -38,5 +38,6 @@ twoway![ ifchain_forloop, t2, reallylong, - immediate_expr + immediate_expr, + collapse_expr ]; diff --git a/packages/autofmt/tests/samples/collapse_expr.rsx b/packages/autofmt/tests/samples/collapse_expr.rsx new file mode 100644 index 000000000..189717a0a --- /dev/null +++ b/packages/autofmt/tests/samples/collapse_expr.rsx @@ -0,0 +1,4 @@ +fn itworks() { + rsx!( "{name}", "{name}", "{name}" ) +} +