Print 2 newlines between unrelated styles

This commit is contained in:
ConnorSkees 2020-01-29 21:25:07 -05:00
parent 304c7b7489
commit 3699f987c8
2 changed files with 27 additions and 4 deletions

View File

@ -9,6 +9,7 @@ enum Toplevel {
RuleSet(Selector, Vec<BlockEntry>),
MultilineComment(String),
AtRule(AtRule),
Newline,
}
#[derive(Debug, Clone)]
@ -91,20 +92,33 @@ impl Css {
}
fn parse_stylesheet(mut self, s: StyleSheet) -> Css {
let mut is_first = true;
for stmt in s.0 {
let v = self.parse_stmt(stmt);
// this is how we print newlines between unrelated styles
// it could probably be refactored
if !v.is_empty() {
if let Toplevel::MultilineComment(..) = v[0] {}
else if is_first {
is_first = false;
} else {
self.blocks.push(Toplevel::Newline);
}
}
self.blocks.extend(v);
}
self
}
pub fn pretty_print<W: Write>(self, buf: &mut W) -> SassResult<()> {
let mut has_written = false;
for block in self.blocks {
match block {
Toplevel::RuleSet(selector, styles) => {
if styles.is_empty() {
continue;
}
has_written = true;
writeln!(buf, "{} {{", selector)?;
for style in styles {
write!(buf, "{}", style)?;
@ -112,11 +126,16 @@ impl Css {
writeln!(buf, "}}")?;
}
Toplevel::MultilineComment(s) => {
has_written = true;
writeln!(buf, "/*{}*/", s)?;
}
Toplevel::AtRule(r) => {
has_written = true;
writeln!(buf, "{}", r)?;
}
Toplevel::Newline => if has_written {
writeln!(buf)?
}
}
}
Ok(())

View File

@ -58,7 +58,7 @@ mod test_variables {
test!(
variable_shadowing_val_does_not_change_complex,
"a {\n color: red;\n}\n$y: before;\n$x: 1 2 $y;\n$y: after;\nfoo {\n a: $x;\n}",
"a {\n color: red;\n}\nfoo {\n a: 1 2 before;\n}\n"
"a {\n color: red;\n}\n\nfoo {\n a: 1 2 before;\n}\n"
);
test!(
variable_whitespace,
@ -425,17 +425,17 @@ mod test_styles {
);
test!(
two_rulesets,
"a {\n color: red;\n}\nc {\n color: white;\n}\n"
"a {\n color: red;\n}\n\nc {\n color: white;\n}\n"
);
test!(
two_rulesets_first_no_semicolon,
"a {\n color: red\n}\nc {\n color: white;\n}\n",
"a {\n color: red;\n}\nc {\n color: white;\n}\n"
"a {\n color: red;\n}\n\nc {\n color: white;\n}\n"
);
test!(
two_inner_outer_rulesets,
"a {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\na {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\n",
"a b {\n color: red;\n}\na c {\n color: white;\n}\na b {\n color: red;\n}\na c {\n color: white;\n}\n"
"a b {\n color: red;\n}\na c {\n color: white;\n}\n\na b {\n color: red;\n}\na c {\n color: white;\n}\n"
);
test!(
removes_empty_outer_styles,
@ -548,6 +548,10 @@ mod test_misc {
keyword_important_not_at_end,
"a {\n height: !important 1;\n}\n"
);
test!(
double_newline_between_unrelated_styles,
"a {\n color: red;\n}\n\nb {\n color: blue;\n}\n"
);
}
mod test_interpolation {