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

feat: improve with

- There were some interactions where the user would feel
  that indentation levels were missing/excessive
This commit is contained in:
Kevin Amado 2022-02-12 13:56:06 -05:00
parent f4ed680daa
commit 70042d5bbd
No known key found for this signature in database
GPG key ID: FFF341057F503148
4 changed files with 92 additions and 37 deletions

View file

@ -52,26 +52,15 @@ pub fn rule(
let next = children.peek_next().unwrap();
let next_kind = next.element.kind();
if matches!(
next_kind,
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_WITH
| 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
))
if false
|| 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_APPLY)
&& matches!(
next.element
@ -90,6 +79,39 @@ pub fn rule(
| 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_WITH)
&& 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_IDENT
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_LITERAL
| rnix::SyntaxKind::NODE_STRING
))
{
steps.push_back(crate::builder::Step::Whitespace);
} else {

View file

@ -19,19 +19,18 @@ pub fn rule(
steps.push_back(crate::builder::Step::Format(child.element));
// /**/
let mut comment = false;
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));
comment = true;
}
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 {
@ -40,7 +39,6 @@ pub fn rule(
// expr
let child = children.get_next().unwrap();
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element));
@ -68,17 +66,35 @@ pub fn rule(
// expr
let child = children.get_next().unwrap();
if comment || matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH) {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
steps.push_back(crate::builder::Step::Whitespace);
}
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element));
if comment
|| matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH)
|| !matches!(
child.element.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_LITERAL
| rnix::SyntaxKind::NODE_STRING
)
{
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
} else {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
}
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element));
}
}

View file

@ -21,4 +21,12 @@
(with a; with b; with c; {a=1;})
(with a; with b; with c; {a=1;b=2;})
(with a; /* comment */ with b; with c; {a=1;b=2;})
{ a = with b;with b;with b;
1;
}
{binPath = with pkgs;
makeBinPath (
[
rsync
util-linux]);}
]

View file

@ -46,11 +46,7 @@
a = with b; 1;
# comment
}
(
with a;
with b;
with c; { a = 1; }
)
(with a; with b; with c; { a = 1; })
(
with a;
with b;
@ -70,4 +66,17 @@
b = 2;
}
)
{
a = with b; with b; with b; 1;
}
{
binPath =
with pkgs;
makeBinPath (
[
rsync
util-linux
]
);
}
]