1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +00:00

feat: indent key-value in a few scenarios only

This commit is contained in:
Kevin Amado 2022-02-10 00:21:52 -05:00
parent 5e172e49ac
commit 4e791e6341
No known key found for this signature in database
GPG key ID: FFF341057F503148
2 changed files with 112 additions and 131 deletions

View file

@ -26,19 +26,17 @@ pub fn rule(
}
// /**/
let mut comment = false;
children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => {
comment = true;
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 let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_prev().unwrap().element.kind()
{
if comment {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
@ -54,52 +52,45 @@ pub fn rule(
let next = children.peek_next().unwrap();
let next_kind = next.element.kind();
if let rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = next_kind
if matches!(
next_kind,
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING
) || (matches!(next_kind, rnix::SyntaxKind::NODE_LAMBDA)
&& !matches!(
next.element
.clone()
.into_node()
.unwrap()
.children()
.next()
.unwrap()
.kind(),
rnix::SyntaxKind::NODE_PATTERN
))
|| (matches!(next_kind, rnix::SyntaxKind::NODE_APPLY)
&& matches!(
next.element
.clone()
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.iter()
.rev()
.next()
.unwrap()
.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING
))
{
steps.push_back(crate::builder::Step::Whitespace);
} else if let rnix::SyntaxKind::NODE_APPLY = next_kind {
if let rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = next
.element
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.iter()
.rev()
.next()
.unwrap()
.kind()
{
steps.push_back(crate::builder::Step::Whitespace);
} else {
dedent = true;
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
} else if let rnix::SyntaxKind::NODE_LAMBDA = next_kind {
if let rnix::SyntaxKind::NODE_PATTERN = next
.element
.into_node()
.unwrap()
.children()
.next()
.unwrap()
.kind()
{
dedent = true;
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
steps.push_back(crate::builder::Step::Whitespace);
}
} else {
dedent = true;
steps.push_back(crate::builder::Step::Indent);
@ -134,19 +125,17 @@ pub fn rule(
}
// /**/
let mut comment = false;
children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => {
comment = true;
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 let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_prev().unwrap().element.kind()
{
if comment {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}

View file

@ -1,93 +1,85 @@
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
/*
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;
/**/
a =
let
/*
b
*/
c = 1;
in
/*
e
*/
f;
a = let
/*
b
*/
c = 1;
in
/*
e
*/
f;
/**/
a =
let
/*
b
*/
c = 1;
/*
d
*/
in
f;
a = let
/*
b
*/
c = 1;
/*
d
*/
in
f;
/**/
a =
let
/*
b
*/
c = 1;
/*
d
*/
in
/*
e
*/
f;
a = let
/*
b
*/
c = 1;
/*
d
*/
in
/*
e
*/
f;
/**/
in
/**/