mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
feat: keep functions in one line
This commit is contained in:
parent
8cb32da9c8
commit
ccf4aaf9a6
5 changed files with 48 additions and 22 deletions
|
@ -10,7 +10,7 @@ pub fn rule(
|
||||||
&crate::config::Layout::Tall
|
&crate::config::Layout::Tall
|
||||||
} else if node
|
} else if node
|
||||||
.children()
|
.children()
|
||||||
.filter(|node| match node.kind() {
|
.filter(|element| match element.kind() {
|
||||||
rnix::SyntaxKind::NODE_KEY_VALUE
|
rnix::SyntaxKind::NODE_KEY_VALUE
|
||||||
| rnix::SyntaxKind::NODE_INHERIT
|
| rnix::SyntaxKind::NODE_INHERIT
|
||||||
| rnix::SyntaxKind::NODE_INHERIT_FROM => true,
|
| rnix::SyntaxKind::NODE_INHERIT_FROM => true,
|
||||||
|
|
|
@ -6,7 +6,27 @@ pub fn rule(
|
||||||
|
|
||||||
let mut children = crate::children::Children::new(build_ctx, node);
|
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
|
&crate::config::Layout::Tall
|
||||||
} else {
|
} else {
|
||||||
build_ctx.config.layout()
|
build_ctx.config.layout()
|
||||||
|
@ -20,8 +40,12 @@ pub fn rule(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
child.element,
|
child.element,
|
||||||
));
|
));
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
if !has_comments && items_count <= 1 {
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
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 => {
|
crate::config::Layout::Wide => {
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
@ -130,8 +154,12 @@ pub fn rule(
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
crate::config::Layout::Tall => {
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
if !has_comments_between_curly_b && items_count <= 1 {
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
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 => {
|
crate::config::Layout::Wide => {
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
@ -151,8 +179,12 @@ pub fn rule(
|
||||||
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
|
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
crate::config::Layout::Tall => {
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
if !has_comments && items_count <= 1 {
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
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(
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
child.element,
|
child.element,
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,23 +1,19 @@
|
||||||
[
|
[
|
||||||
({ } @ a: _)
|
({ } @ a: _)
|
||||||
(
|
(
|
||||||
{
|
{ } @
|
||||||
}
|
|
||||||
@
|
|
||||||
/**/
|
/**/
|
||||||
a:
|
a:
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
{
|
{ }
|
||||||
}
|
|
||||||
/**/
|
/**/
|
||||||
@ a:
|
@ a:
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
{
|
{ }
|
||||||
}
|
|
||||||
/**/
|
/**/
|
||||||
@
|
@
|
||||||
/**/
|
/**/
|
||||||
|
@ -28,16 +24,13 @@
|
||||||
(
|
(
|
||||||
a @
|
a @
|
||||||
/**/
|
/**/
|
||||||
{
|
{ }:
|
||||||
}:
|
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
a
|
a
|
||||||
/**/
|
/**/
|
||||||
@
|
@ { }:
|
||||||
{
|
|
||||||
}:
|
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
|
@ -45,8 +38,7 @@
|
||||||
/**/
|
/**/
|
||||||
@
|
@
|
||||||
/**/
|
/**/
|
||||||
{
|
{ }:
|
||||||
}:
|
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[
|
[
|
||||||
({}: _)
|
({}: _)
|
||||||
|
({ a }: _)
|
||||||
({ /**/ }: _)
|
({ /**/ }: _)
|
||||||
({ ... }: _)
|
({ ... }: _)
|
||||||
({ ... /**/}: _)
|
({ ... /**/}: _)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
[
|
[
|
||||||
({ }: _)
|
({ }: _)
|
||||||
|
({ a }: _)
|
||||||
(
|
(
|
||||||
{ /**/
|
{ /**/
|
||||||
}:
|
}:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue