1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +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 {
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,18 +52,29 @@ 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
if matches!(
next_kind,
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = next_kind
{
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_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = next
.element
| 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()
@ -74,7 +83,12 @@ pub fn rule(
.rev()
.next()
.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);
} else {
@ -83,29 +97,6 @@ pub fn rule(
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);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
}
crate::config::Layout::Wide => {
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 {
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,7 +1,6 @@
let
/**/
a =
let
a = let
b = 2;
c = 3;
in
@ -10,8 +9,7 @@ let
a = let c = 1; in f;
/**/
a =
let
a = let
c = 1;
in
/*
@ -19,8 +17,7 @@ let
*/
f;
/**/
a =
let
a = let
c = 1;
/*
d
@ -29,8 +26,7 @@ let
f;
/**/
a =
let
a = let
c = 1;
/*
d
@ -41,8 +37,7 @@ let
*/
f;
/**/
a =
let
a = let
/*
b
*/
@ -50,8 +45,7 @@ let
in
f;
/**/
a =
let
a = let
/*
b
*/
@ -62,8 +56,7 @@ let
*/
f;
/**/
a =
let
a = let
/*
b
*/
@ -74,8 +67,7 @@ let
in
f;
/**/
a =
let
a = let
/*
b
*/