<commit_before>//! Code formatter.
#![doc(html_root_url = "https://docs.rs/gluon_formatter/0.7.0")] // # GLUON

extern crate itertools;
extern crate pretty;
#[macro_use]
extern crate gluon_base as base;
extern crate gluon_parser as parser;

mod pretty_print;

pub fn format_expr(input: &str) -> Result<String, parser::ParseErrors> {
    use base::source::Source;
    use base::symbol::Symbols;
    use base::types::TypeCache;

    let newline = match input.find(|c: char| c == '\n' || c == '\r') {
        Some(i) => {
            if input[i..].starts_with("\r\n") {
                "\r\n"
            } else if input[i..].starts_with("\r") {
                "\r"
            } else {
                "\n"
            }
        }
        None => "\n",
    };

    let type_cache = TypeCache::new();
    let expr = parser::parse_expr(&mut Symbols::new(), &type_cache, input)?;

    let source = Source::new(input);
    let arena = pretty::Arena::new();
    let printer = pretty_print::Printer::new(&arena, &source);
    Ok(printer.format(100, newline, &expr))
}<commit_msg>feat: Export expr pretty printing function to format<commit_after>//! Code formatter.
#![doc(html_root_url = "https://docs.rs/gluon_formatter/0.7.0")] // # GLUON

#[macro_use]
extern crate gluon_base as base;
extern crate gluon_parser as parser;
extern crate itertools;
extern crate pretty;

use base::ast::SpannedExpr;
use base::source::Source;
use base::symbol::{Symbol, Symbols};
use base::types::TypeCache;

mod pretty_print;

pub fn pretty_expr(expr: &SpannedExpr<Symbol>, input: &str) -> String {
    let newline = match input.find(|c: char| c == '\n' || c == '\r') {
        Some(i) => {
            if input[i..].starts_with("\r\n") {
                "\r\n"
            } else if input[i..].starts_with("\r") {
                "\r"
            } else {
                "\n"
            }
        }
        None => "\n",
    };

    let source = Source::new(input);
    let arena = pretty::Arena::new();
    let printer = pretty_print::Printer::new(&arena, &source);
    printer.format(100, newline, &expr)
}

pub fn format_expr(input: &str) -> Result<String, parser::ParseErrors> {
    let type_cache = TypeCache::new();
    let expr = parser::parse_expr(&mut Symbols::new(), &type_cache, input)?;

    Ok(pretty_expr(&expr, input))
}
{'fraction_non_alphanumeric': 0.17372515125324114, 'fraction_numerical': 0.005185825410544511, 'mean_word_length': 2.6687797147385104, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4777846', 'n_tokens_mistral': 796, 'n_tokens_neox': 763, 'n_words': 179}