From 4690aa8842f1c533843b0178a75c5c28d5eec83b Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Fri, 9 Aug 2024 19:27:24 +0300 Subject: [PATCH] fix: formatter for tags with optional body --- src/daleth/format.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/daleth/format.rs b/src/daleth/format.rs index 9fb552b..3c39714 100644 --- a/src/daleth/format.rs +++ b/src/daleth/format.rs @@ -3,20 +3,35 @@ use super::{ utils::set_indent, }; -fn nl_needed<'src>(last2: Option<&Token<'src>>, last1: Option<&Token<'src>>) -> bool { +fn nl_needed<'src>( + last2: Option<&Token<'src>>, + last1: Option<&Token<'src>>, + current: &Token<'src>, +) -> bool { if let Some(last1) = last1 { - if *last1 == Token::Br { - return true; - } - - if *last1 == Token::Hr { + // No body, no arg + if [Token::Br, Token::Hr].contains(last1) { return true; } if let Some(last2) = last2 { - if *last2 == Token::Img { + // No body, with arg + if [Token::Img, Token::Footlnk, Token::A].contains(last2) { return true; } + + // Optional body + if [Token::Link, Token::Navlink, Token::Btn, Token::Navbtn].contains(last2) { + return match current { + Token::LSquare => false, + Token::TextBody(_) => false, + Token::MLText(_) => false, + Token::MLMSText(_, _) => false, + Token::RMLText(_) => false, + + _ => true, + }; + } } } @@ -45,13 +60,13 @@ pub fn format<'src>(spanned_tokens: &Vec>>) -> String { } }; - if nl_needed(last2, last1) { + let current_token = &spanned_tokens[i].0; + + if nl_needed(last2, last1, current_token) { formatted.push_str("\n"); }; - let spanned_token = &spanned_tokens[i].0; - - let to_push = match spanned_token { + let to_push = match current_token { Token::LSquare => { current_indent += 1; " [\n".to_owned() @@ -75,7 +90,7 @@ pub fn format<'src>(spanned_tokens: &Vec>>) -> String { set_indent("}", current_indent) ), Token::RMLText(t) => format!(" {{#{t}}}\n"), - Token::Comment(c) => format!("{}\n", set_indent(&format!("# {c}"), current_indent)), + Token::Comment(c) => format!("{}\n", set_indent(&format!("#{c}"), current_indent)), Token::TextTag(t) => format!("{}\n", set_indent(t, current_indent)),