Skip to content

Commit

Permalink
feat: fit if/then/else exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
kamadorueda committed Jan 31, 2022
1 parent 15c6a63 commit 658f5e2
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 183 deletions.
7 changes: 4 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
);
Expand Down
48 changes: 37 additions & 11 deletions src/rules/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/if_else/in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
(if a then b else c)
(if ./a then b else c)
(if /**/ a /**/ then /**/ b /**/ else /**/ c)
(if
(if
Expand Down

0 comments on commit 658f5e2

Please sign in to comment.