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

feat: keep lists new lines

This commit is contained in:
Kevin Amado 2022-02-06 22:24:53 -05:00
parent 8564225506
commit 595a59c365
No known key found for this signature in database
GPG key ID: FFF341057F503148
11 changed files with 264 additions and 32 deletions

View file

@ -9,13 +9,20 @@ pub fn rule(
); );
let items_count = node let items_count = node
.children() .children_with_tokens()
.skip_while(|element| {
element.kind() != rnix::SyntaxKind::TOKEN_CURLY_B_OPEN
})
.take_while(|element| {
element.kind() != rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE
})
.filter(|element| { .filter(|element| {
matches!( matches!(
element.kind(), element.kind(),
rnix::SyntaxKind::NODE_KEY_VALUE rnix::SyntaxKind::NODE_KEY_VALUE
| rnix::SyntaxKind::NODE_INHERIT | rnix::SyntaxKind::NODE_INHERIT
| rnix::SyntaxKind::NODE_INHERIT_FROM | rnix::SyntaxKind::NODE_INHERIT_FROM
| rnix::SyntaxKind::TOKEN_COMMENT
) )
}) })
.count(); .count();
@ -66,16 +73,6 @@ pub fn rule(
crate::config::Layout::Wide => {} crate::config::Layout::Wide => {}
} }
// /**/
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(_) => {}
});
let mut item_index: usize = 0; let mut item_index: usize = 0;
loop { loop {
@ -85,6 +82,7 @@ pub fn rule(
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));
item_index += 1;
} }
crate::children::DrainCommentOrNewline::Newline(_) => { crate::children::DrainCommentOrNewline::Newline(_) => {
if item_index > 0 && item_index < items_count { if item_index > 0 && item_index < items_count {

View file

@ -77,8 +77,6 @@ pub fn rule(
} }
children.move_next(); children.move_next();
} else {
break;
} }
} }

View file

@ -4,9 +4,19 @@ 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 layout = if children.has_comments() { let items_count = node
.children_with_tokens()
.filter(|element| {
!matches!(element.kind(), rnix::SyntaxKind::TOKEN_WHITESPACE)
})
.count()
- 2;
let layout = if children.has_comments() || children.has_newlines() {
&crate::config::Layout::Tall &crate::config::Layout::Tall
} else { } else {
build_ctx.config.layout() build_ctx.config.layout()
@ -22,24 +32,34 @@ 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| {
steps.push_back(crate::builder::Step::NewLine); match element {
steps.push_back(crate::builder::Step::Pad); crate::children::DrainCommentOrNewline::Comment(text) => {
steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
item_index += 1;
}
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_SQUARE_B_CLOSE = child.element.kind()
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_SQUARE_B_CLOSE = kind
{ {
break; break;
} }
// item // item
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);
@ -56,18 +76,9 @@ pub fn rule(
} }
children.move_next(); children.move_next();
} else {
break;
} }
} }
// /**/
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));
});
// ] // ]
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
match layout { match layout {

View file

@ -2,6 +2,8 @@
{} {}
{/*a*/} {/*a*/}
{a=1;} {a=1;}
{a=1;
}
{ b=1; } { b=1; }
{ b=1; /*c*/ } { b=1; /*c*/ }
@ -18,4 +20,36 @@
rec /*a*/ { /*b*/ c=1; /*d*/ } rec /*a*/ { /*b*/ c=1; /*d*/ }
{a=rec {a={a=rec {a={a=rec {a={a=rec {a={a=rec {a={};};};};};};};};};};} {a=rec {a={a=rec {a={a=rec {a={a=rec {a={a=rec {a={};};};};};};};};};};}
rec {
c=1;
e=1;
}
rec
/*a*/
{
/*b*/
c=1;
/*d*/
e=1;
/*f*/
}
] ]

View file

@ -6,6 +6,10 @@
*/ */
} }
{ a = 1; } { a = 1; }
{
a = 1;
}
{ b = 1; } { b = 1; }
{ {
b = 1; b = 1;
@ -28,6 +32,7 @@
c c
*/ */
} }
rec { c = 1; } rec { c = 1; }
rec { rec {
c = 1; c = 1;
@ -90,6 +95,7 @@
d d
*/ */
} }
{ {
a = rec { a = rec {
a = { a = {
@ -101,4 +107,32 @@
}; };
}; };
} }
rec {
c = 1;
e = 1;
}
rec
/*
a
*/
{
/*
b
*/
c = 1;
/*
d
*/
e = 1;
/*
f
*/
}
] ]

View file

@ -1,38 +1,49 @@
[ [
/**/ /**/
/**/ /**/
/**/ /**/
/**/ /**/
/**/ /**/
/**/ /**/
/* /*
@ @
*/ */
/* /*
* *
@ @
* *
*/ */
/* /*
@ @
@ @
@ @
*/ */
/* /*
@ @
@ @
@ @
*/ */
/* /*
@ @
@ @
@ @
*/ */
/* /*
@ @
@ @
@ @
*/ */
/* /*
test test
* test * test

46
tests/cases/lists/in Normal file
View file

@ -0,0 +1,46 @@
[
[ 1 ]
[ 1
]
[ b d ]
[ b d /*e*/ ]
[ b /*c*/ d ]
[ b /*c*/ d /*e*/ ]
[ /*a*/ b d ]
[ /*a*/ b d /*e*/ ]
[ /*a*/ b /*c*/ d ]
[ /*a*/ b /*c*/ d /*e*/ ]
[
b
d
]
[
/*a*/
b
/*c*/
d
/*e*/
]
]

96
tests/cases/lists/out Normal file
View file

@ -0,0 +1,96 @@
[
[ 1 ]
[
1
]
[ b d ]
[
b
d
/*
e
*/
]
[
b
/*
c
*/
d
]
[
b
/*
c
*/
d
/*
e
*/
]
[
/*
a
*/
b
d
]
[
/*
a
*/
b
d
/*
e
*/
]
[
/*
a
*/
b
/*
c
*/
d
]
[
/*
a
*/
b
/*
c
*/
d
/*
e
*/
]
[
b
d
]
[
/*
a
*/
b
/*
c
*/
d
/*
e
*/
]
]

View file

@ -20,6 +20,7 @@
a: a:
_ _
) )
(a @ { }: _) (a @ { }: _)
( (
a @ a @

View file

@ -26,6 +26,7 @@
}: }:
_ _
) )
( (
{ b { b
, e , e
@ -1050,6 +1051,7 @@
}: }:
_ _
) )
({ a ? null }: _) ({ a ? null }: _)
( (
{ /* { /*

View file

@ -29,6 +29,7 @@
'' a '' a
'' ''
### ###
'' a '' a
'' ''
### ###