1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#![doc(html_root_url = "https://docs.rs/gluon_formatter/0.10.1")]
extern crate codespan;
#[macro_use]
extern crate gluon_base as base;
extern crate itertools;
extern crate pretty;
use base::{ast::SpannedExpr, symbol::Symbol};
mod pretty_print;
pub fn pretty_expr(input: &str, expr: &SpannedExpr<Symbol>) -> String {
Formatter::default().pretty_expr(input, expr)
}
#[derive(Default, Debug, Clone)]
pub struct Formatter {
pub expanded: bool,
}
impl Formatter {
pub fn pretty_expr(&self, input: &str, expr: &SpannedExpr<Symbol>) -> 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 = codespan::FileMap::new("test".into(), input.into());
let arena = pretty::Arena::<()>::new();
let printer = pretty_print::Printer::new(&arena, &source, self.clone());
printer.format(100, newline, &expr)
}
}