1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +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> {
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
.children()
@ -16,9 +18,10 @@ pub fn rule(
})
.count();
let layout = if children.has_comments() {
&crate::config::Layout::Tall
} else if items_count > 1 {
let layout = if items_count > 1
|| children.has_comments()
|| children.has_newlines()
{
&crate::config::Layout::Tall
} else {
build_ctx.config.layout()
@ -34,24 +37,30 @@ pub fn rule(
crate::config::Layout::Wide => {}
}
let mut item_index: usize = 0;
loop {
// /**/
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));
children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::NewLine);
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() {
let kind = child.element.kind();
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_IN = kind
{
if let rnix::SyntaxKind::TOKEN_IN = child.element.kind() {
break;
}
// expr
item_index += 1;
match layout {
crate::config::Layout::Tall => {
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
let child = children.get_next().unwrap();
let indent = build_ctx.pos_new.column > 1;
@ -104,10 +106,13 @@ 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));
children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
}
crate::children::DrainCommentOrNewline::Newline(_) => {}
});
// expr

View file

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

View file

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