mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-29 19:47:45 +00:00
Don't add blank lines after comments in inherit
Fixes #376 Note that handling of extra newlines in `inherit` statements is still a little janky; usually blank lines will be removed from formatted output, but in some cases (e.g. between an end-of-line comment and a full-line comment) they will be preserved. See the tests added for a few examples of this.
This commit is contained in:
parent
5324ea3181
commit
a7005b0c50
6 changed files with 101 additions and 4 deletions
|
@ -53,7 +53,15 @@ pub(crate) fn rule(
|
|||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
// Only add padding if there are no `trivialities` (that is, there's no extra
|
||||
// `Newlines(_)` to be added) or if the first one is a comment (that is, it'll need
|
||||
// to be indented to match the content).
|
||||
if matches!(
|
||||
child.trivialities.front(),
|
||||
None | Some(crate::children2::Trivia::Comment(_))
|
||||
) {
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
} else if (not_last_child && !child.has_trivialities)
|
||||
|| matches!(
|
||||
child.trivialities.front(),
|
||||
|
@ -64,10 +72,20 @@ pub(crate) fn rule(
|
|||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
|
||||
for trivia in child.trivialities {
|
||||
let mut trivia_iter = child.trivialities.into_iter().peekable();
|
||||
while let Some(trivia) = trivia_iter.next() {
|
||||
match trivia {
|
||||
crate::children2::Trivia::Comment(text) => {
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
// If the next `trivia` is a newline, don't add newlines and padding at the
|
||||
// end of this iteration, as it will lead to a new blank line in the
|
||||
// output.
|
||||
if matches!(
|
||||
trivia_iter.peek(),
|
||||
Some(crate::children2::Trivia::Newlines(_))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
crate::children2::Trivia::Newlines(_) => {}
|
||||
}
|
||||
|
|
|
@ -89,11 +89,11 @@
|
|||
{
|
||||
inherit # test
|
||||
a # test
|
||||
|
||||
|
||||
b # test
|
||||
c # test
|
||||
d # test
|
||||
|
||||
|
||||
e
|
||||
f
|
||||
g
|
||||
|
|
34
src/alejandra/tests/cases/inherit_blank_trailing/in
Normal file
34
src/alejandra/tests/cases/inherit_blank_trailing/in
Normal file
|
@ -0,0 +1,34 @@
|
|||
[
|
||||
{
|
||||
inherit # test
|
||||
a # test
|
||||
|
||||
b # test
|
||||
c # test
|
||||
d # test
|
||||
|
||||
e
|
||||
f
|
||||
|
||||
g
|
||||
h
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
a # mixed trivialities
|
||||
|
||||
# comment 1
|
||||
# comment 2
|
||||
|
||||
# comment 3 after blanks
|
||||
b # multiple newlines
|
||||
|
||||
|
||||
c # multiple comments
|
||||
# comment 1
|
||||
# comment 2
|
||||
# comment 3
|
||||
;
|
||||
}
|
||||
]
|
31
src/alejandra/tests/cases/inherit_blank_trailing/out
Normal file
31
src/alejandra/tests/cases/inherit_blank_trailing/out
Normal file
|
@ -0,0 +1,31 @@
|
|||
[
|
||||
{
|
||||
inherit # test
|
||||
a # test
|
||||
|
||||
b # test
|
||||
c # test
|
||||
d # test
|
||||
|
||||
e
|
||||
f
|
||||
g
|
||||
h
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
a # mixed trivialities
|
||||
|
||||
# comment 1
|
||||
# comment 2
|
||||
# comment 3 after blanks
|
||||
b # multiple newlines
|
||||
|
||||
c # multiple comments
|
||||
# comment 1
|
||||
# comment 2
|
||||
# comment 3
|
||||
;
|
||||
}
|
||||
]
|
7
src/alejandra/tests/cases/inherit_comment/in
Normal file
7
src/alejandra/tests/cases/inherit_comment/in
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
inherit # eeby deeby
|
||||
a
|
||||
# b
|
||||
c
|
||||
;
|
||||
}
|
7
src/alejandra/tests/cases/inherit_comment/out
Normal file
7
src/alejandra/tests/cases/inherit_comment/out
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
inherit # eeby deeby
|
||||
a
|
||||
# b
|
||||
c
|
||||
;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue