improve handling of newlines around comments
This commit is contained in:
parent
0edb60e2b3
commit
6b109a5c3d
@ -231,17 +231,28 @@ impl Css {
|
||||
|
||||
fn parse_stylesheet(mut self, stmts: Vec<Stmt>) -> SassResult<Css> {
|
||||
let mut is_first = true;
|
||||
let mut last_was_comment = false;
|
||||
for stmt in stmts {
|
||||
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 Some(Toplevel::MultilineComment(..)) = v.first() {
|
||||
if matches!(v.first(), Some(Toplevel::MultilineComment(..))) {
|
||||
if !last_was_comment && !is_first {
|
||||
self.blocks.push(Toplevel::Newline);
|
||||
}
|
||||
|
||||
last_was_comment = true;
|
||||
} else if is_first {
|
||||
is_first = false;
|
||||
last_was_comment = false;
|
||||
} else {
|
||||
self.blocks.push(Toplevel::Newline);
|
||||
if !last_was_comment {
|
||||
self.blocks.push(Toplevel::Newline);
|
||||
}
|
||||
|
||||
last_was_comment = false;
|
||||
}
|
||||
is_first = false;
|
||||
self.blocks.extend(v);
|
||||
}
|
||||
}
|
||||
@ -502,7 +513,14 @@ impl Formatter for ExpandedFormatter {
|
||||
writeln!(buf, "{}}}", padding)?;
|
||||
}
|
||||
Toplevel::MultilineComment(s) => {
|
||||
if has_written && should_emit_newline {
|
||||
writeln!(buf)?;
|
||||
}
|
||||
|
||||
has_written = true;
|
||||
|
||||
should_emit_newline = false;
|
||||
|
||||
writeln!(buf, "{}/*{}*/", padding, s)?;
|
||||
}
|
||||
Toplevel::Import(s) => {
|
||||
|
@ -34,7 +34,7 @@ test!(
|
||||
test!(
|
||||
preserves_toplevel_comment_after,
|
||||
"a {\n color: red;\n}\n/* foo */\n",
|
||||
"a {\n color: red;\n}\n/* foo */\n"
|
||||
"a {\n color: red;\n}\n\n/* foo */\n"
|
||||
);
|
||||
test!(
|
||||
removes_single_line_comment,
|
||||
@ -79,3 +79,22 @@ test!(
|
||||
a /** */ b {x: y}",
|
||||
"a b {\n x: y;\n}\n\na b {\n x: y;\n}\n\na b {\n x: y;\n}\n\na b {\n x: y;\n}\n"
|
||||
);
|
||||
test!(
|
||||
comment_has_newline_above_and_not_below_when_between_two_rulesets,
|
||||
r"a {
|
||||
color: red;
|
||||
}
|
||||
|
||||
/**/
|
||||
|
||||
a {
|
||||
color: green;
|
||||
}",
|
||||
"a {\n color: red;\n}\n\n/**/\na {\n color: green;\n}\n"
|
||||
);
|
||||
test!(
|
||||
no_extra_newline_between_two_comments,
|
||||
r"/**/
|
||||
/**/",
|
||||
"/**/\n/**/\n"
|
||||
);
|
||||
|
@ -84,7 +84,7 @@ test!(
|
||||
color: $b;
|
||||
}
|
||||
} /**/ ",
|
||||
"/**/\n/**/\na {\n color: a;\n}\n/**/\n\na {\n color: b;\n}\n/**/\n"
|
||||
"/**/\n/**/\na {\n color: a;\n}\n\n/**/\na {\n color: b;\n}\n\n/**/\n"
|
||||
);
|
||||
error!(
|
||||
list_of_single_map,
|
||||
|
Loading…
x
Reference in New Issue
Block a user