fn insert_whitespaces(syn: SyntaxNode) -> String {
    use SyntaxKind::*;

    let mut res = String::new();
    let mut token_iter = syn
        .preorder_with_tokens()
        .filter_map(|event| {
            if let WalkEvent::Enter(NodeOrToken::Token(token)) = event {
                Some(token)
            } else {
                None
            }
        })
        .peekable();

    let mut indent = 0;
    let mut last: Option<SyntaxKind> = None;

    while let Some(token) = token_iter.next() {
        let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
            token_iter.peek().map(|it| f(it.kind())).unwrap_or(default)
        };
        let is_last =
            |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };

        res += &match token.kind() {
            k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ",
            L_CURLY if is_next(|it| it != R_CURLY, true) => {
                indent += 1;
                let leading_space = if is_last(is_text, false) { " " } else { "" };
                format!("{}{{\n{}", leading_space, "  ".repeat(indent))
            }
            R_CURLY if is_last(|it| it != L_CURLY, true) => {
                indent = indent.saturating_sub(1);
                format!("\n{}}}", "  ".repeat(indent))
            }
            R_CURLY => format!("}}\n{}", "  ".repeat(indent)),
            T![;] => format!(";\n{}", "  ".repeat(indent)),
            T![->] => " -> ".to_string(),
            T![=] => " = ".to_string(),
            T![=>] => " => ".to_string(),
            _ => token.text().to_string(),
        };

        last = Some(token.kind());
    }

    return res;

    fn is_text(k: SyntaxKind) -> bool {
        k.is_keyword() || k.is_literal() || k == IDENT
    }
}
// FIXME: It would also be cool to share logic here and in the mbe tests,
// which are pretty unreadable at the moment.
It would also be cool to share logic here and in the mbe tests,
which are pretty unreadable at the moment.
It would also be cool to share logic here and in the mbe tests which are pretty unreadable at the moment .
fn insert_whitespaces(syn: SyntaxNode) -> String {
    use SyntaxKind::*;
    let mut res = String::new();
    let mut token_iter = syn
        .preorder_with_tokens()
        .filter_map(|event| {
            if let WalkEvent::Enter(NodeOrToken::Token(token)) = event {
                Some(token)
            } else {
                None
            }
        })
        .peekable();
    let mut indent = 0;
    let mut last: Option<SyntaxKind> = None;
    while let Some(token) = token_iter.next() {
        let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
            token_iter.peek().map(|it| f(it.kind())).unwrap_or(default)
        };
        let is_last =
            |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
        res += &match token.kind() {
            k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ",
            L_CURLY if is_next(|it| it != R_CURLY, true) => {
                indent += 1;
                let leading_space = if is_last(is_text, false) { " " } else { "" };
                format!("{}{{\n{}", leading_space, "  ".repeat(indent))
            }
            R_CURLY if is_last(|it| it != L_CURLY, true) => {
                indent = indent.saturating_sub(1);
                format!("\n{}}}", "  ".repeat(indent))
            }
            R_CURLY => format!("}}\n{}", "  ".repeat(indent)),
            T![;] => format!(";\n{}", "  ".repeat(indent)),
            T![->] => " -> ".to_string(),
            T![=] => " = ".to_string(),
            T![=>] => " => ".to_string(),
            _ => token.text().to_string(),
        };
        last = Some(token.kind());
    }
    return res;
    fn is_text(k: SyntaxKind) -> bool {
        k.is_keyword() || k.is_literal() || k == IDENT
    }
}
fn insert_whitespaces ( syn : SyntaxNode ) -> String { use SyntaxKind :: * ; let mut res = String :: new ( ) ; let mut token_iter = syn . preorder_with_tokens ( ) . filter_map ( | event | { if let WalkEvent :: Enter ( NodeOrToken :: Token ( token ) ) = event { Some ( token ) } else { None } } ) . peekable ( ) ; let mut indent = 0 ; let mut last : Option < SyntaxKind > = None ; while let Some ( token ) = token_iter . next ( ) { let mut is_next = | f : fn ( SyntaxKind ) -> bool , default | -> bool { token_iter . peek ( ) . map ( | it | f ( it . kind ( ) ) ) . unwrap_or ( default ) } ; let is_last = | f : fn ( SyntaxKind ) -> bool , default | -> bool { last . map ( f ) . unwrap_or ( default ) } ; res += & match token . kind ( ) { k if is_text ( k ) && is_next ( | it | ! it . is_punct ( ) , true ) => token . text ( ) . to_string ( ) + " " , L_CURLY if is_next ( | it | it != R_CURLY , true ) => { indent += 1 ; let leading_space = if is_last ( is_text , false ) { " " } else { " " } ; format ! ( " \n " leading_space " " repeat ( indent ) ) } R_CURLY if is_last ( | it | it != L_CURLY , true ) => { indent = indent . saturating_sub ( 1 ) ; format ! ( " \n " " " repeat ( indent ) ) } R_CURLY => format ! ( " \n " " " repeat ( indent ) ) , T ! [ ] => format ! ( " \n " " " repeat ( indent ) ) , T ! [ ] => " " . to_string ( ) , T ! [ ] => " " . to_string ( ) , T ! [ ] => " " . to_string ( ) , _ => token . text ( ) . to_string ( ) , } ; last = Some ( token . kind ( ) ) ; } return res ; fn is_text ( k : SyntaxKind ) -> bool { k . is_keyword ( ) || k . is_literal ( ) || k == IDENT } }
FIXME: It would also be cool to share logic here and in the mbe tests,
which are pretty unreadable at the moment.
FIXME : It would also be cool to share logic here and in the mbe tests which are pretty unreadable at the moment .
syn
SyntaxNode
syn
SyntaxNode