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

feat: keep let-in new lines

This commit is contained in:
Kevin Amado 2022-02-06 20:51:22 -05:00
parent 9b66535246
commit 8564225506
No known key found for this signature in database
GPG key ID: FFF341057F503148
3 changed files with 43 additions and 24 deletions

View file

@ -4,7 +4,9 @@ pub fn rule(
) -> std::collections::LinkedList<crate::builder::Step> { ) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new(); let mut steps = std::collections::LinkedList::new();
let mut children = crate::children::Children::new(build_ctx, node); let mut children = crate::children::Children::new_with_configuration(
build_ctx, node, true,
);
let items_count = node let items_count = node
.children() .children()
@ -16,9 +18,10 @@ pub fn rule(
}) })
.count(); .count();
let layout = if children.has_comments() { let layout = if items_count > 1
&crate::config::Layout::Tall || children.has_comments()
} else if items_count > 1 { || children.has_newlines()
{
&crate::config::Layout::Tall &crate::config::Layout::Tall
} else { } else {
build_ctx.config.layout() build_ctx.config.layout()
@ -34,24 +37,30 @@ pub fn rule(
crate::config::Layout::Wide => {} crate::config::Layout::Wide => {}
} }
let mut item_index: usize = 0;
loop { loop {
// /**/ // /**/
children.drain_comments(|text| { children.drain_comments_and_newlines(|element| match element {
steps.push_back(crate::builder::Step::NewLine); crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
}
crate::children::DrainCommentOrNewline::Newline(_) => {
if item_index > 0 && item_index < items_count {
steps.push_back(crate::builder::Step::NewLine);
}
}
}); });
if let Some(child) = children.peek_next() { if let Some(child) = children.peek_next() {
let kind = child.element.kind(); if let rnix::SyntaxKind::TOKEN_IN = child.element.kind() {
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_IN = kind
{
break; break;
} }
// expr // expr
item_index += 1;
match layout { match layout {
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
@ -73,13 +82,6 @@ pub fn rule(
} }
} }
// /**/
children.drain_comments(|text| {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
});
// in // in
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
let indent = build_ctx.pos_new.column > 1; let indent = build_ctx.pos_new.column > 1;
@ -104,10 +106,13 @@ pub fn rule(
} }
// /**/ // /**/
children.drain_comments(|text| { children.drain_comments_and_newlines(|element| match element {
steps.push_back(crate::builder::Step::NewLine); crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
}
crate::children::DrainCommentOrNewline::Newline(_) => {}
}); });
// expr // expr

View file

@ -1,13 +1,19 @@
let let
/**/ /**/
a = let b=2; c=3; in d; a = let b=2; c=3; in d;
/**/ /**/
a = let c=1; in f; a = let c=1; in f;
/**/ /**/
a = let c=1; in /*e*/ f; a = let c=1; in /*e*/ f;
/**/ /**/
a = let c=1; /*d*/ in f; a = let c=1; /*d*/ in f;
/**/ /**/
a = let c=1; /*d*/ in /*e*/ f; a = let c=1; /*d*/ in /*e*/ f;
/**/ /**/
a = let /*b*/ c=1; in f; a = let /*b*/ c=1; in f;
@ -18,6 +24,12 @@ let
/**/ /**/
a = let /*b*/ c=1; /*d*/ in /*e*/ f; a = let /*b*/ c=1; /*d*/ in /*e*/ f;
/**/ /**/
in in
/**/ /**/
a a

View file

@ -8,6 +8,7 @@ let
d; d;
/**/ /**/
a = let c = 1; in f; a = let c = 1; in f;
/**/ /**/
a = a =
let let
@ -27,6 +28,7 @@ let
in in
f; f;
/**/ /**/
a = a =
let let
c = 1; c = 1;