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

feat: keep functions in one line

This commit is contained in:
Kevin Amado 2022-01-30 18:47:01 -05:00
parent 8cb32da9c8
commit ccf4aaf9a6
No known key found for this signature in database
GPG key ID: FFF341057F503148
5 changed files with 48 additions and 22 deletions

View file

@ -10,7 +10,7 @@ pub fn rule(
&crate::config::Layout::Tall
} else if node
.children()
.filter(|node| match node.kind() {
.filter(|element| match element.kind() {
rnix::SyntaxKind::NODE_KEY_VALUE
| rnix::SyntaxKind::NODE_INHERIT
| rnix::SyntaxKind::NODE_INHERIT_FROM => true,

View file

@ -6,7 +6,27 @@ pub fn rule(
let mut children = crate::children::Children::new(build_ctx, node);
let layout = if children.has_comments() {
let has_comments = children.has_comments();
let has_comments_between_curly_b = node
.children_with_tokens()
.skip_while(|element| {
element.kind() != rnix::SyntaxKind::TOKEN_CURLY_B_OPEN
})
.take_while(|element| {
element.kind() != rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE
})
.any(|element| element.kind() == rnix::SyntaxKind::TOKEN_COMMENT);
let items_count = node
.children_with_tokens()
.filter(|element| match element.kind() {
rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY => true,
_ => false,
})
.count();
let layout = if has_comments {
&crate::config::Layout::Tall
} else {
build_ctx.config.layout()
@ -20,8 +40,12 @@ pub fn rule(
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element));
@ -130,8 +154,12 @@ pub fn rule(
let child = children.get_next().unwrap();
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
if !has_comments_between_curly_b && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
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);
@ -151,8 +179,12 @@ pub fn rule(
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));

View file

@ -1,23 +1,19 @@
[
({ } @ a: _)
(
{
}
@
{ } @
/**/
a:
_
)
(
{
}
{ }
/**/
@ a:
_
)
(
{
}
{ }
/**/
@
/**/
@ -28,16 +24,13 @@
(
a @
/**/
{
}:
{ }:
_
)
(
a
/**/
@
{
}:
@ { }:
_
)
(
@ -45,8 +38,7 @@
/**/
@
/**/
{
}:
{ }:
_
)
]

View file

@ -1,5 +1,6 @@
[
({}: _)
({ a }: _)
({ /**/ }: _)
({ ... }: _)
({ ... /**/}: _)

View file

@ -1,5 +1,6 @@
[
({ }: _)
({ a }: _)
(
{ /**/
}: