1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +00:00

Merge pull request #111 from kamadorueda/kamadorueda

feat: remove unnecessary newlines
This commit is contained in:
Kevin Amado 2022-02-16 19:26:58 -05:00 committed by GitHub
commit 142f855a4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 52 deletions

View file

@ -19,7 +19,6 @@ pub fn rule(
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element)); steps.push_back(crate::builder::Step::FormatWider(child.element));
steps.push_back(crate::builder::Step::Indent);
} }
crate::config::Layout::Wide => { crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
@ -28,19 +27,18 @@ pub fn rule(
if children.has_next() { if children.has_next() {
// /**/ // /**/
let mut comment = false;
children.drain_comments_and_newlines(|element| match element { children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => { crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::Comment(text));
comment = true;
} }
crate::children::DrainCommentOrNewline::Newline(_) => {} crate::children::DrainCommentOrNewline::Newline(_) => {}
}); });
if let rnix::SyntaxKind::TOKEN_COMMENT if comment {
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_prev().unwrap().element.kind()
{
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} else { } else {
@ -52,38 +50,49 @@ pub fn rule(
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
// /**/ // /**/
let mut comment = false;
children.drain_comments_and_newlines(|element| match element { children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => { crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::Comment(text));
comment = true;
} }
crate::children::DrainCommentOrNewline::Newline(_) => {} crate::children::DrainCommentOrNewline::Newline(_) => {}
}); });
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_prev().unwrap().element.kind()
{
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
steps.push_back(crate::builder::Step::Whitespace);
}
// expr // expr
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
let single_line = crate::builder::fits_in_single_line(
build_ctx,
child.element.clone(),
);
if single_line {
if comment {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
steps.push_back(crate::builder::Step::Whitespace);
}
} else {
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(
child.element, child.element,
)); ));
steps.push_back(crate::builder::Step::Dedent);
} }
crate::config::Layout::Wide => { crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
} }
} }
if !single_line {
steps.push_back(crate::builder::Step::Dedent);
}
} }
steps steps

View file

@ -1199,14 +1199,14 @@
a a
*/ */
b b
/* /*
a a
*/ */
? ?
/* /*
a a
*/ */
null null
/* /*
c c
*/ */
@ -1215,14 +1215,14 @@
d d
*/ */
e e
/* /*
a a
*/ */
? ?
/* /*
a a
*/ */
null null
/* /*
f f
*/ */
@ -1245,16 +1245,16 @@
*/ */
# #
b b
/* /*
a a
*/ */
# #
? ?
/* /*
a a
*/ */
# #
null null
/* /*
c c
*/ */
@ -1265,16 +1265,16 @@
*/ */
# #
e e
/* /*
a a
*/ */
# #
? ?
/* /*
a a
*/ */
# #
null null
/* /*
f f
*/ */