fix: format for other than heading number argument

This commit is contained in:
Artemy Egorov 2024-08-09 20:56:22 +03:00
parent 61c9bbe300
commit 067cbd7e00

View file

@ -3,39 +3,51 @@ use super::{
utils::set_indent, utils::set_indent,
}; };
fn nl_needed<'src>( fn additional_str<'src>(
last2: Option<&Token<'src>>, last2: Option<&Token<'src>>,
last1: Option<&Token<'src>>, last1: Option<&Token<'src>>,
current: &Token<'src>, current: &Token<'src>,
) -> bool { ) -> &'src str {
if let Some(last1) = last1 { if let Some(last1) = last1 {
// No body, no arg // No body, no arg
if [Token::Br, Token::Hr].contains(last1) { if [Token::Br, Token::Hr].contains(last1) {
return true; return "\n";
} }
match current {
Token::NumberArgument(_) => {
if let Token::H = last1 {
return "";
} else {
return " ";
}
}
_ => {}
};
if let Some(last2) = last2 { if let Some(last2) = last2 {
// No body, with arg // No body, with arg
if [Token::Img, Token::Footlnk, Token::A].contains(last2) { if [Token::Img, Token::Footlnk, Token::A].contains(last2) {
return true; return "\n";
} }
// Optional body // Optional body
if [Token::Link, Token::Navlink, Token::Btn, Token::Navbtn].contains(last2) { if [Token::Link, Token::Navlink, Token::Btn, Token::Navbtn].contains(last2) {
return match current { return match current {
Token::LSquare => false, Token::LSquare => "",
Token::TextBody(_) => false, Token::TextBody(_) => "",
Token::MLText(_) => false, Token::MLText(_) => "",
Token::MLMSText(_, _) => false, Token::MLMSText(_, _) => "",
Token::RMLText(_) => false, Token::RMLText(_) => "",
_ => true, _ => "\n",
}; };
} }
} }
} }
false ""
} }
pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String { pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
@ -62,9 +74,7 @@ pub fn format<'src>(spanned_tokens: &Vec<Spanned<Token<'src>>>) -> String {
let current_token = &spanned_tokens[i].0; let current_token = &spanned_tokens[i].0;
if nl_needed(last2, last1, current_token) { formatted.push_str(additional_str(last2, last1, current_token));
formatted.push_str("\n");
};
let to_push = match current_token { let to_push = match current_token {
Token::LSquare => { Token::LSquare => {