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>), RuleSet(Selector, Vec<BlockEntry>),
MultilineComment(String), MultilineComment(String),
AtRule(AtRule), AtRule(AtRule),
Newline,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -91,20 +92,33 @@ impl Css {
} }
fn parse_stylesheet(mut self, s: StyleSheet) -> Css { fn parse_stylesheet(mut self, s: StyleSheet) -> Css {
let mut is_first = true;
for stmt in s.0 { for stmt in s.0 {
let v = self.parse_stmt(stmt); 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.blocks.extend(v);
} }
self self
} }
pub fn pretty_print<W: Write>(self, buf: &mut W) -> SassResult<()> { pub fn pretty_print<W: Write>(self, buf: &mut W) -> SassResult<()> {
let mut has_written = false;
for block in self.blocks { for block in self.blocks {
match block { match block {
Toplevel::RuleSet(selector, styles) => { Toplevel::RuleSet(selector, styles) => {
if styles.is_empty() { if styles.is_empty() {
continue; continue;
} }
has_written = true;
writeln!(buf, "{} {{", selector)?; writeln!(buf, "{} {{", selector)?;
for style in styles { for style in styles {
write!(buf, "{}", style)?; write!(buf, "{}", style)?;
@ -112,11 +126,16 @@ impl Css {
writeln!(buf, "}}")?; writeln!(buf, "}}")?;
} }
Toplevel::MultilineComment(s) => { Toplevel::MultilineComment(s) => {
has_written = true;
writeln!(buf, "/*{}*/", s)?; writeln!(buf, "/*{}*/", s)?;
} }
Toplevel::AtRule(r) => { Toplevel::AtRule(r) => {
has_written = true;
writeln!(buf, "{}", r)?; writeln!(buf, "{}", r)?;
} }
Toplevel::Newline => if has_written {
writeln!(buf)?
}
} }
} }
Ok(()) Ok(())

View File

@ -58,7 +58,7 @@ mod test_variables {
test!( test!(
variable_shadowing_val_does_not_change_complex, 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}\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!( test!(
variable_whitespace, variable_whitespace,
@ -425,17 +425,17 @@ mod test_styles {
); );
test!( test!(
two_rulesets, 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!( test!(
two_rulesets_first_no_semicolon, 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}\nc {\n color: white;\n}\n" "a {\n color: red;\n}\n\nc {\n color: white;\n}\n"
); );
test!( test!(
two_inner_outer_rulesets, 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 {\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!( test!(
removes_empty_outer_styles, removes_empty_outer_styles,
@ -548,6 +548,10 @@ mod test_misc {
keyword_important_not_at_end, keyword_important_not_at_end,
"a {\n height: !important 1;\n}\n" "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 { mod test_interpolation {