2020-02-14 14:55:21 -05:00
|
|
|
use std::fmt::Write;
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-02-22 12:00:07 -05:00
|
|
|
use crate::atrule::AtRule;
|
2020-02-16 10:08:45 -05:00
|
|
|
use crate::error::SassResult;
|
|
|
|
use crate::{RuleSet, Stmt, StyleSheet};
|
2020-01-04 22:55:04 -05:00
|
|
|
|
|
|
|
pub(crate) struct PrettyPrinter<W: Write> {
|
|
|
|
buf: W,
|
|
|
|
scope: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> PrettyPrinter<W> {
|
|
|
|
pub(crate) fn new(buf: W) -> Self {
|
|
|
|
PrettyPrinter { buf, scope: 0 }
|
|
|
|
}
|
|
|
|
|
2020-01-05 12:35:37 -05:00
|
|
|
/// Pretty print `crate::Stmt`
|
2020-01-05 19:09:46 -05:00
|
|
|
/// Throws away super selectors and variables
|
2020-01-20 12:13:52 -05:00
|
|
|
fn pretty_print_stmt(&mut self, stmt: &Stmt) -> SassResult<()> {
|
2020-01-04 22:55:04 -05:00
|
|
|
let padding = vec![' '; self.scope * 2].iter().collect::<String>();
|
|
|
|
match stmt {
|
2020-01-08 20:39:05 -05:00
|
|
|
Stmt::MultilineComment(s) => writeln!(self.buf, "{}/*{}*/", padding, s)?,
|
2020-01-04 22:55:04 -05:00
|
|
|
Stmt::RuleSet(RuleSet {
|
|
|
|
selector, rules, ..
|
|
|
|
}) => {
|
|
|
|
writeln!(self.buf, "{}{} {{", padding, selector)?;
|
|
|
|
self.scope += 1;
|
|
|
|
for rule in rules {
|
|
|
|
self.pretty_print_stmt(rule)?;
|
|
|
|
}
|
|
|
|
writeln!(self.buf, "{}}}", padding)?;
|
|
|
|
self.scope -= 1;
|
|
|
|
}
|
2020-01-05 20:51:14 -05:00
|
|
|
Stmt::Style(s) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
writeln!(self.buf, "{}{}", padding, s.to_string()?)?;
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-02-22 12:00:07 -05:00
|
|
|
Stmt::AtRule(r) => match r {
|
|
|
|
AtRule::Unknown(..) => todo!("Display @rules properly"),
|
|
|
|
_ => todo!(),
|
|
|
|
},
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-05 12:35:37 -05:00
|
|
|
/// Pretty print SCSS
|
|
|
|
///
|
|
|
|
/// The result should be an exact copy of the SCSS input
|
|
|
|
/// Empty rules are included
|
2020-01-20 12:13:52 -05:00
|
|
|
pub fn pretty_print(&mut self, s: &StyleSheet) -> SassResult<()> {
|
2020-01-18 14:57:56 -05:00
|
|
|
for rule in &s.0 {
|
2020-01-04 22:55:04 -05:00
|
|
|
self.pretty_print_stmt(rule)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-05 12:22:10 -05:00
|
|
|
}
|