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

feat: fit if/then/else exprs

This commit is contained in:
Kevin Amado 2022-01-30 23:39:09 -05:00
parent 15c6a6345d
commit 658f5e25db
No known key found for this signature in database
GPG key ID: FFF341057F503148
4 changed files with 126 additions and 183 deletions

View file

@ -275,7 +275,8 @@ fn format_wider(
) {
match element {
rnix::SyntaxElement::Node(node) => {
let layout = if fits_in_single_line(build_ctx, node) {
let layout = if fits_in_single_line(build_ctx, node.clone().into())
{
crate::config::Layout::Wide
} else {
crate::config::Layout::Tall
@ -293,11 +294,11 @@ fn format_wider(
pub fn fits_in_single_line(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
node: rnix::SyntaxElement,
) -> bool {
let maybe_green_node = build(
&build_ctx.config.with_layout(crate::config::Layout::Wide),
node.clone().into(),
node,
true,
build_ctx.path.clone(),
);

View file

@ -10,21 +10,47 @@ pub fn rule(
// if/then/else
let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child.element));
steps.push_back(crate::builder::Step::Indent);
// /**/
children.drain_comments(|text| {
if let rnix::SyntaxKind::TOKEN_COMMENT =
children.peek_next().unwrap().element.kind()
{
steps.push_back(crate::builder::Step::Indent);
// /**/
children.drain_comments(|text| {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
});
// expr
let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
});
steps.push_back(crate::builder::Step::FormatWider(child.element));
steps.push_back(crate::builder::Step::Dedent);
} else {
let child = children.get_next().unwrap();
// expr
let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::FormatWider(child.element));
steps.push_back(crate::builder::Step::Dedent);
// expr
if crate::builder::fits_in_single_line(
build_ctx,
child.element.clone(),
) {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
} else {
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
steps.push_back(crate::builder::Step::Dedent);
}
}
if branch != "else" {
steps.push_back(crate::builder::Step::NewLine);

View file

@ -1,5 +1,5 @@
[
(if a then b else c)
(if ./a then b else c)
(if /**/ a /**/ then /**/ b /**/ else /**/ c)
(if
(if

View file

@ -1,11 +1,8 @@
[
(
if
a
then
b
else
c
if ./a
then b
else c
)
(
if
@ -27,90 +24,63 @@
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
then
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
else
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
)
@ -120,90 +90,63 @@
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
then
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
else
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
)
@ -213,90 +156,63 @@
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
then
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
else
(
if
(
if
a
then
b
else
c
if a
then b
else c
)
then
(
if
a
then
b
else
c
if a
then b
else c
)
else
(
if
a
then
b
else
c
if a
then b
else c
)
)
)