1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +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 { match element {
rnix::SyntaxElement::Node(node) => { 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 crate::config::Layout::Wide
} else { } else {
crate::config::Layout::Tall crate::config::Layout::Tall
@ -293,11 +294,11 @@ fn format_wider(
pub fn fits_in_single_line( pub fn fits_in_single_line(
build_ctx: &crate::builder::BuildCtx, build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode, node: rnix::SyntaxElement,
) -> bool { ) -> bool {
let maybe_green_node = build( let maybe_green_node = build(
&build_ctx.config.with_layout(crate::config::Layout::Wide), &build_ctx.config.with_layout(crate::config::Layout::Wide),
node.clone().into(), node,
true, true,
build_ctx.path.clone(), build_ctx.path.clone(),
); );

View file

@ -10,21 +10,47 @@ pub fn rule(
// if/then/else // if/then/else
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
steps.push_back(crate::builder::Step::Indent);
// /**/ if let rnix::SyntaxKind::TOKEN_COMMENT =
children.drain_comments(|text| { 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::NewLine);
steps.push_back(crate::builder::Step::Pad); 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 // expr
let child = children.get_next().unwrap(); if crate::builder::fits_in_single_line(
steps.push_back(crate::builder::Step::NewLine); build_ctx,
steps.push_back(crate::builder::Step::Pad); child.element.clone(),
steps.push_back(crate::builder::Step::FormatWider(child.element)); ) {
steps.push_back(crate::builder::Step::Dedent); 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" { if branch != "else" {
steps.push_back(crate::builder::Step::NewLine); 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 /**/ a /**/ then /**/ b /**/ else /**/ c)
(if (if
(if (if

View file

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