fix: formatter for tags with optional body

This commit is contained in:
Artemy Egorov 2024-08-09 19:27:24 +03:00
parent 74f42aa314
commit 4690aa8842

View file

@ -3,20 +3,35 @@ use super::{
utils::set_indent, 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 let Some(last1) = last1 {
if *last1 == Token::Br { // No body, no arg
return true; if [Token::Br, Token::Hr].contains(last1) {
}
if *last1 == Token::Hr {
return true; return true;
} }
if let Some(last2) = last2 { if let Some(last2) = last2 {
if *last2 == Token::Img { // No body, with arg
if [Token::Img, Token::Footlnk, Token::A].contains(last2) {
return true; 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<Spanned<Token<'src>>>) -> String {
} }
}; };
if nl_needed(last2, last1) { let current_token = &spanned_tokens[i].0;
if nl_needed(last2, last1, current_token) {
formatted.push_str("\n"); formatted.push_str("\n");
}; };
let spanned_token = &spanned_tokens[i].0; let to_push = match current_token {
let to_push = match spanned_token {
Token::LSquare => { Token::LSquare => {
current_indent += 1; current_indent += 1;
" [\n".to_owned() " [\n".to_owned()
@ -75,7 +90,7 @@ pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
set_indent("}", current_indent) set_indent("}", current_indent)
), ),
Token::RMLText(t) => format!(" {{#{t}}}\n"), 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)), Token::TextTag(t) => format!("{}\n", set_indent(t, current_indent)),