fix formatting and merging if chains in attributes (#2697)
* fix formatting and merging if chains in attributes * fix autoformat if attribute chains * Fix IfAttributeValue ToTokens implementation --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
1 parent
a6e41e9d68
commit
7f210633eb
8 files changed
+301
-145
No files matched your search
@@ -226,14 +226,8 @@ impl Writer<'_> {
|
||||
|
||||
fn write_attribute_value(&mut self, value: &AttributeValue) -> Result {
|
||||
match value {
|
||||
AttributeValue::AttrOptionalExpr { condition, value } => {
|
||||
write!(
|
||||
self.out,
|
||||
"if {condition} {{ ",
|
||||
condition = unparse_expr(condition),
|
||||
)?;
|
||||
self.write_attribute_value(value)?;
|
||||
write!(self.out, " }}")?;
|
||||
AttributeValue::IfExpr(if_chain) => {
|
||||
self.write_attribute_if_chain(if_chain)?;
|
||||
}
|
||||
AttributeValue::AttrLiteral(value) => {
|
||||
write!(self.out, "{value}")?;
|
||||
@@ -258,6 +252,26 @@ impl Writer<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_attribute_if_chain(&mut self, if_chain: &IfAttributeValue) -> Result {
|
||||
write!(self.out, "if {} {{ ", unparse_expr(&if_chain.condition))?;
|
||||
self.write_attribute_value(&if_chain.then_value)?;
|
||||
write!(self.out, " }}")?;
|
||||
match if_chain.else_value.as_deref() {
|
||||
Some(AttributeValue::IfExpr(else_if_chain)) => {
|
||||
write!(self.out, "else ")?;
|
||||
self.write_attribute_if_chain(else_if_chain)?;
|
||||
}
|
||||
Some(other) => {
|
||||
write!(self.out, "else {{")?;
|
||||
self.write_attribute_value(other)?;
|
||||
write!(self.out, " }}")?;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_mulitiline_tokens(&mut self, out: String) -> Result {
|
||||
let mut lines = out.split('\n').peekable();
|
||||
let first = lines.next().unwrap();
|
||||
|
||||
@@ -249,10 +249,18 @@ impl<'a> Writer<'a> {
|
||||
|
||||
pub(crate) fn attr_value_len(&mut self, value: &ElementAttrValue) -> usize {
|
||||
match value {
|
||||
ElementAttrValue::AttrOptionalExpr { condition, value } => {
|
||||
let condition_len = self.retrieve_formatted_expr(condition).len();
|
||||
let value_len = self.attr_value_len(value);
|
||||
condition_len + value_len + 6
|
||||
ElementAttrValue::IfExpr(if_chain) => {
|
||||
let condition_len = self.retrieve_formatted_expr(&if_chain.condition).len();
|
||||
let value_len = self.attr_value_len(&if_chain.then_value);
|
||||
let if_len = 2;
|
||||
let brace_len = 2;
|
||||
let space_len = 2;
|
||||
let else_len = if_chain
|
||||
.else_value
|
||||
.as_ref()
|
||||
.map(|else_value| self.attr_value_len(else_value) + 1)
|
||||
.unwrap_or_default();
|
||||
condition_len + value_len + if_len + brace_len + space_len + else_len
|
||||
}
|
||||
ElementAttrValue::AttrLiteral(lit) => lit.to_string().len(),
|
||||
ElementAttrValue::Shorthand(expr) => expr.span().line_length(),
|
||||
|
||||
Reference in new issue
Block a user