1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +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 { children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => { crate::children::DrainCommentOrNewline::Comment(text) => {
comment = true;
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));
} }
crate::children::DrainCommentOrNewline::Newline(_) => {} crate::children::DrainCommentOrNewline::Newline(_) => {}
}); });
if comment {
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::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} else { } else {
@ -54,52 +52,45 @@ pub fn rule(
let next = children.peek_next().unwrap(); let next = children.peek_next().unwrap();
let next_kind = next.element.kind(); let next_kind = next.element.kind();
if let rnix::SyntaxKind::NODE_ATTR_SET if matches!(
| rnix::SyntaxKind::NODE_LIST next_kind,
| rnix::SyntaxKind::NODE_PAREN rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_STRING = next_kind | 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); 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 { } else {
dedent = true; dedent = true;
steps.push_back(crate::builder::Step::Indent); 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 { children.drain_comments_and_newlines(|element| match element {
crate::children::DrainCommentOrNewline::Comment(text) => { crate::children::DrainCommentOrNewline::Comment(text) => {
comment = true;
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));
} }
crate::children::DrainCommentOrNewline::Newline(_) => {} crate::children::DrainCommentOrNewline::Newline(_) => {}
}); });
if comment {
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::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} }

View file

@ -1,93 +1,85 @@
let let
/**/ /**/
a = a = let
let b = 2;
b = 2; c = 3;
c = 3; in
in d;
d;
/**/ /**/
a = let c = 1; in f; a = let c = 1; in f;
/**/ /**/
a = a = let
let c = 1;
c = 1; in
in /*
/* e
e */
*/ f;
f;
/**/ /**/
a = a = let
let c = 1;
c = 1; /*
/* d
d */
*/ in
in f;
f;
/**/ /**/
a = a = let
let c = 1;
c = 1; /*
/* d
d */
*/ in
in /*
/* e
e */
*/ f;
f;
/**/ /**/
a = a = let
let /*
/* b
b */
*/ c = 1;
c = 1; in
in f;
f;
/**/ /**/
a = a = let
let /*
/* b
b */
*/ c = 1;
c = 1; in
in /*
/* e
e */
*/ f;
f;
/**/ /**/
a = a = let
let /*
/* b
b */
*/ c = 1;
c = 1; /*
/* d
d */
*/ in
in f;
f;
/**/ /**/
a = a = let
let /*
/* b
b */
*/ c = 1;
c = 1; /*
/* d
d */
*/ in
in /*
/* e
e */
*/ f;
f;
/**/ /**/
in in
/**/ /**/