1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 21:17:45 +00:00

Merge pull request #56 from kamadorueda/kamadorueda

feat: indent key-value in a few scenarios only
This commit is contained in:
Kevin Amado 2022-02-10 00:33:48 -05:00 committed by GitHub
commit a220eb56f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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,18 +52,29 @@ 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_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = next_kind | rnix::SyntaxKind::NODE_LET_IN
{
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_LIST
| rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_STRING
| rnix::SyntaxKind::NODE_STRING = next ) || (matches!(next_kind, rnix::SyntaxKind::NODE_LAMBDA)
.element && !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() .into_node()
.unwrap() .unwrap()
.children() .children()
@ -74,7 +83,12 @@ pub fn rule(
.rev() .rev()
.next() .next()
.unwrap() .unwrap()
.kind() .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 { } else {
@ -83,29 +97,6 @@ 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);
} }
} 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);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
} }
crate::config::Layout::Wide => { crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
@ -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,7 +1,6 @@
let let
/**/ /**/
a = a = let
let
b = 2; b = 2;
c = 3; c = 3;
in in
@ -10,8 +9,7 @@ let
a = let c = 1; in f; a = let c = 1; in f;
/**/ /**/
a = a = let
let
c = 1; c = 1;
in in
/* /*
@ -19,8 +17,7 @@ let
*/ */
f; f;
/**/ /**/
a = a = let
let
c = 1; c = 1;
/* /*
d d
@ -29,8 +26,7 @@ let
f; f;
/**/ /**/
a = a = let
let
c = 1; c = 1;
/* /*
d d
@ -41,8 +37,7 @@ let
*/ */
f; f;
/**/ /**/
a = a = let
let
/* /*
b b
*/ */
@ -50,8 +45,7 @@ let
in in
f; f;
/**/ /**/
a = a = let
let
/* /*
b b
*/ */
@ -62,8 +56,7 @@ let
*/ */
f; f;
/**/ /**/
a = a = let
let
/* /*
b b
*/ */
@ -74,8 +67,7 @@ let
in in
f; f;
/**/ /**/
a = a = let
let
/* /*
b b
*/ */