2020-01-20 12:13:52 -05:00
|
|
|
use std::io::Write;
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-01-20 12:13:52 -05:00
|
|
|
use crate::{RuleSet, SassResult, 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) => {
|
|
|
|
writeln!(self.buf, "{}{}", padding, s)?;
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-01-26 15:27:38 -05:00
|
|
|
Stmt::AtRule(r) => {
|
|
|
|
writeln!(self.buf, "{}{}", padding, r)?;
|
|
|
|
}
|
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
|
|
|
|
2020-01-05 12:35:37 -05:00
|
|
|
/// Pretty print `crate::Stmt`
|
|
|
|
/// Keeps super selectors
|
2020-01-20 12:13:52 -05:00
|
|
|
fn pretty_print_stmt_preserve_super_selectors(&mut self, stmt: &Stmt) -> SassResult<()> {
|
2020-01-05 12:22:10 -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, "/*{}*/", s)?,
|
2020-01-05 12:22:10 -05:00
|
|
|
Stmt::RuleSet(RuleSet {
|
|
|
|
selector,
|
|
|
|
rules,
|
|
|
|
super_selector,
|
|
|
|
}) => {
|
2020-01-18 21:05:26 -05:00
|
|
|
writeln!(self.buf, "{}{} {{", padding, super_selector.zip(selector))?;
|
2020-01-05 12:22:10 -05:00
|
|
|
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) => {
|
|
|
|
writeln!(self.buf, "{}{}", padding, s)?;
|
2020-01-05 12:22:10 -05:00
|
|
|
}
|
2020-01-26 15:27:38 -05:00
|
|
|
Stmt::AtRule(r) => {
|
|
|
|
writeln!(self.buf, "{}{}", padding, r)?;
|
|
|
|
}
|
2020-01-05 12:22:10 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-05 12:35:37 -05:00
|
|
|
/// Pretty print a special form of SCSS that shows what the full selectors are for children
|
|
|
|
/// Meant for debugging
|
|
|
|
/// Empty rules are included
|
2020-01-05 12:52:50 -05:00
|
|
|
pub(crate) fn pretty_print_preserve_super_selectors(
|
|
|
|
&mut self,
|
|
|
|
s: &StyleSheet,
|
2020-01-20 12:13:52 -05:00
|
|
|
) -> SassResult<()> {
|
2020-01-18 14:57:56 -05:00
|
|
|
for rule in &s.0 {
|
2020-01-06 16:50:16 -05:00
|
|
|
self.pretty_print_stmt_preserve_super_selectors(rule)?;
|
2020-01-05 12:22:10 -05:00
|
|
|
}
|
|
|
|
writeln!(self.buf)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:55:04 -05:00
|
|
|
#[cfg(test)]
|
2020-01-05 20:23:35 -05:00
|
|
|
mod test_scss {
|
2020-01-04 22:55:04 -05:00
|
|
|
use super::StyleSheet;
|
|
|
|
macro_rules! test {
|
|
|
|
($func:ident, $input:literal) => {
|
|
|
|
#[test]
|
|
|
|
fn $func() {
|
|
|
|
let mut buf = Vec::new();
|
2020-01-06 19:23:52 -05:00
|
|
|
StyleSheet::new($input)
|
|
|
|
.expect(concat!("failed to parse on ", $input))
|
2020-01-04 22:55:04 -05:00
|
|
|
.pretty_print(&mut buf)
|
|
|
|
.expect(concat!("failed to pretty print on ", $input));
|
|
|
|
assert_eq!(
|
|
|
|
String::from($input),
|
|
|
|
String::from_utf8(buf).expect("produced invalid utf8")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($func:ident, $input:literal, $output:literal) => {
|
|
|
|
#[test]
|
|
|
|
fn $func() {
|
|
|
|
let mut buf = Vec::new();
|
2020-01-06 19:23:52 -05:00
|
|
|
StyleSheet::new($input)
|
|
|
|
.expect(concat!("failed to parse on ", $input))
|
2020-01-04 22:55:04 -05:00
|
|
|
.pretty_print(&mut buf)
|
|
|
|
.expect(concat!("failed to pretty print on ", $input));
|
|
|
|
assert_eq!(
|
|
|
|
String::from($output),
|
|
|
|
String::from_utf8(buf).expect("produced invalid utf8")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test!(empty, "");
|
|
|
|
test!(basic_nesting, "a {\n b {\n }\n}\n");
|
2020-01-05 20:23:35 -05:00
|
|
|
test!(mul_nesting, "a, b {\n a, b {\n }\n}\n");
|
2020-01-04 22:55:04 -05:00
|
|
|
test!(basic_style, "a {\n color: red;\n}\n");
|
|
|
|
test!(two_styles, "a {\n color: red;\n color: blue;\n}\n");
|
|
|
|
test!(
|
|
|
|
nested_style_in_parent,
|
|
|
|
"a {\n color: red;\n b {\n }\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
nested_style_in_child,
|
|
|
|
"a {\n b {\n color: red;\n }\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
nested_style_in_both,
|
|
|
|
"a {\n color: red;\n b {\n color: red;\n }\n}\n"
|
|
|
|
);
|
2020-01-05 12:22:10 -05:00
|
|
|
test!(
|
|
|
|
deeply_nested_selector,
|
|
|
|
"\
|
|
|
|
a {
|
|
|
|
b {
|
|
|
|
c {
|
|
|
|
d {
|
|
|
|
e {
|
|
|
|
f {
|
|
|
|
g {
|
|
|
|
h {
|
|
|
|
i {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
);
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|