fix: short expr lists should not eat the final exprs
This commit is contained in:
1 parent
b6e4db1f2f
commit
b81eca0433
4 files changed
+31
-31
No files matched your search
@@ -328,10 +328,8 @@ impl Writer<'_> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[BodyNode::RawExpr(ref expr)] => {
|
// TODO: let rawexprs to be inlined
|
||||||
// TODO: let rawexprs to be inlined
|
[BodyNode::RawExpr(ref expr)] => get_expr_length(expr),
|
||||||
get_expr_length(expr)
|
|
||||||
}
|
|
||||||
[BodyNode::Element(ref el)] => {
|
[BodyNode::Element(ref el)] => {
|
||||||
let attr_len = self.is_short_attrs(&el.attributes);
|
let attr_len = self.is_short_attrs(&el.attributes);
|
||||||
|
|
||||||
|
|||||||
+23
-26
@@ -79,12 +79,7 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
|||||||
.count()
|
.count()
|
||||||
/ 4;
|
/ 4;
|
||||||
|
|
||||||
// Oneliner optimization
|
write_body(&mut writer, &body);
|
||||||
if writer.is_short_children(&body.roots).is_some() {
|
|
||||||
writer.write_ident(&body.roots[0]).unwrap();
|
|
||||||
} else {
|
|
||||||
writer.write_body_indented(&body.roots).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// writing idents leaves the final line ended at the end of the last ident
|
// writing idents leaves the final line ended at the end of the last ident
|
||||||
if writer.out.buf.contains('\n') {
|
if writer.out.buf.contains('\n') {
|
||||||
@@ -106,8 +101,8 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
|||||||
let end = byte_offset(contents, span.end()) - 1;
|
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
|
// Rustfmt will remove the space between the macro and the opening paren if the macro is a single expression
|
||||||
let body_is_solo_expr =
|
let body_is_solo_expr = body.roots.len() == 1
|
||||||
body.roots.len() == 1 && matches!(body.roots[0], BodyNode::RawExpr(_));
|
&& matches!(body.roots[0], BodyNode::RawExpr(_) | BodyNode::Text(_));
|
||||||
|
|
||||||
if formatted.len() <= 80 && !formatted.contains('\n') && !body_is_solo_expr {
|
if formatted.len() <= 80 && !formatted.contains('\n') && !body_is_solo_expr {
|
||||||
formatted = format!(" {} ", formatted);
|
formatted = format!(" {} ", formatted);
|
||||||
@@ -135,14 +130,28 @@ pub fn write_block_out(body: CallBody) -> Option<String> {
|
|||||||
..Writer::default()
|
..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() {
|
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 {
|
} else {
|
||||||
buf.write_body_indented(&body.roots).unwrap();
|
buf.write_body_indented(&body.roots).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.consume()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option<String> {
|
pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option<String> {
|
||||||
@@ -153,12 +162,7 @@ pub fn fmt_block_from_expr(raw: &str, expr: ExprMacro) -> Option<String> {
|
|||||||
..Writer::default()
|
..Writer::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Oneliner optimization
|
write_body(&mut buf, &body);
|
||||||
if buf.is_short_children(&body.roots).is_some() {
|
|
||||||
buf.write_ident(&body.roots[0]).unwrap();
|
|
||||||
} else {
|
|
||||||
buf.write_body_indented(&body.roots).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.consume()
|
buf.consume()
|
||||||
}
|
}
|
||||||
@@ -173,12 +177,7 @@ pub fn fmt_block(block: &str, indent_level: usize) -> Option<String> {
|
|||||||
|
|
||||||
buf.out.indent = indent_level;
|
buf.out.indent = indent_level;
|
||||||
|
|
||||||
// Oneliner optimization
|
write_body(&mut buf, &body);
|
||||||
if buf.is_short_children(&body.roots).is_some() {
|
|
||||||
buf.write_ident(&body.roots[0]).unwrap();
|
|
||||||
} else {
|
|
||||||
buf.write_body_indented(&body.roots).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// writing idents leaves the final line ended at the end of the last ident
|
// writing idents leaves the final line ended at the end of the last ident
|
||||||
if buf.out.buf.contains('\n') {
|
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 (left, _) = input.split_at(start);
|
||||||
let (_, right) = input.split_at(end);
|
let (_, right) = input.split_at(end);
|
||||||
|
|
||||||
// dbg!(&block.formatted);
|
|
||||||
|
|
||||||
format!("{}{}{}", left, block.formatted, right)
|
format!("{}{}{}", left, block.formatted, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,5 +38,6 @@ twoway![
|
|||||||
ifchain_forloop,
|
ifchain_forloop,
|
||||||
t2,
|
t2,
|
||||||
reallylong,
|
reallylong,
|
||||||
immediate_expr
|
immediate_expr,
|
||||||
|
collapse_expr
|
||||||
];
|
];
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fn itworks() {
|
||||||
|
rsx!( "{name}", "{name}", "{name}" )
|
||||||
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user