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

refactor: if else

This commit is contained in:
Kevin Amado 2022-02-27 16:49:54 -05:00
parent 712f3988f9
commit 8498ce5a4b
2 changed files with 146 additions and 137 deletions

View file

@ -1,82 +1,102 @@
use std::collections::LinkedList;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct IfElse {
pub if_: rnix::SyntaxElement,
pub comments_before_if_expr: LinkedList<String>,
pub if_expr: Option<rnix::SyntaxElement>,
pub if_expr: rnix::SyntaxElement,
pub comments_after_if_expr: LinkedList<String>,
pub then_: rnix::SyntaxElement,
pub comments_before_then_expr: LinkedList<String>,
pub then_expr: Option<rnix::SyntaxElement>,
pub then_expr: rnix::SyntaxElement,
pub comments_after_then_expr: LinkedList<String>,
pub else_: rnix::SyntaxElement,
pub comments_before_else_expr: LinkedList<String>,
pub else_expr: Option<rnix::SyntaxElement>,
pub else_expr: rnix::SyntaxElement,
}
pub(crate) fn parse(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> IfElse {
let mut if_else = IfElse::default();
impl IfElse {
pub(crate) fn parse(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> IfElse {
let mut children = crate::children::Children::new(build_ctx, node);
let mut children = crate::children::Children::new(build_ctx, node);
// if_
let if_ = children.get_next().unwrap();
// if
children.get_next().unwrap();
// comments_before_if_expr
let mut comments_before_if_expr = LinkedList::new();
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_before_if_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_before_if_expr.push_back(text);
// if_expr
let if_expr = children.get_next().unwrap();
// comments_after_if_expr
let mut comments_after_if_expr = LinkedList::new();
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_after_if_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// then_
let then_ = children.get_next().unwrap();
// comments_before_then_expr
let mut comments_before_then_expr = LinkedList::new();
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_before_then_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// then_expr
let then_expr = children.get_next().unwrap();
// comments_after_then_expr
let mut comments_after_then_expr = LinkedList::new();
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_after_then_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// else_
let else_ = children.get_next().unwrap();
// comments_before_else_expr
let mut comments_before_else_expr = LinkedList::new();
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_before_else_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// else_expr
let else_expr = children.get_next().unwrap();
IfElse {
if_,
comments_before_if_expr,
if_expr,
comments_after_if_expr,
then_,
comments_before_then_expr,
then_expr,
comments_after_then_expr,
else_,
comments_before_else_expr,
else_expr,
}
crate::children::Trivia::Whitespace(_) => {}
});
// expr
if_else.if_expr = Some(children.get_next().unwrap());
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_after_if_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// then
children.get_next().unwrap();
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_before_then_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// expr
if_else.then_expr = Some(children.get_next().unwrap());
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_after_then_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// else
children.get_next().unwrap();
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_before_else_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// expr
if_else.else_expr = Some(children.get_next().unwrap());
if_else
}
}

View file

@ -4,154 +4,143 @@ pub(crate) fn rule(
) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new();
let if_else = crate::parsers::if_else::parse(build_ctx, node);
let parsed = crate::parsers::if_else::IfElse::parse(build_ctx, node);
// if
steps.push_back(crate::builder::Step::Token(
rnix::SyntaxKind::TOKEN_IF,
"if".to_string(),
));
// if_
steps.push_back(crate::builder::Step::Format(parsed.if_));
if if_else.comments_before_if_expr.is_empty() {
// expr
let element = if_else.if_expr.unwrap();
if crate::builder::fits_in_single_line(build_ctx, element.clone()) {
if parsed.comments_before_if_expr.is_empty() {
// if_expr
if crate::builder::fits_in_single_line(
build_ctx,
parsed.if_expr.clone(),
) {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(element));
steps.push_back(crate::builder::Step::FormatWider(parsed.if_expr));
} 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(element));
steps.push_back(crate::builder::Step::FormatWider(parsed.if_expr));
steps.push_back(crate::builder::Step::Dedent);
}
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
// /**/
// comments_before_if_expr
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
for text in if_else.comments_before_if_expr {
for text in parsed.comments_before_if_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// expr
steps.push_back(crate::builder::Step::FormatWider(
if_else.if_expr.unwrap(),
));
// if_expr
steps.push_back(crate::builder::Step::FormatWider(parsed.if_expr));
steps.push_back(crate::builder::Step::Dedent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// /**/
if !if_else.comments_after_if_expr.is_empty() {
for text in if_else.comments_after_if_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// comments_after_if_expr
for text in parsed.comments_after_if_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// then
steps.push_back(crate::builder::Step::Token(
rnix::SyntaxKind::TOKEN_THEN,
"then".to_string(),
));
// then_
steps.push_back(crate::builder::Step::Format(parsed.then_));
if if_else.comments_before_then_expr.is_empty() {
// expr
let element = if_else.then_expr.unwrap();
if parsed.comments_before_then_expr.is_empty() {
// then_expr
if matches!(
element.kind(),
parsed.then_expr.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING
) || crate::builder::fits_in_single_line(build_ctx, element.clone())
{
) || crate::builder::fits_in_single_line(
build_ctx,
parsed.then_expr.clone(),
) {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(element));
steps
.push_back(crate::builder::Step::FormatWider(parsed.then_expr));
} 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(element));
steps
.push_back(crate::builder::Step::FormatWider(parsed.then_expr));
steps.push_back(crate::builder::Step::Dedent);
}
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {
// /**/
// comments_before_then_expr
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
for text in if_else.comments_before_then_expr {
for text in parsed.comments_before_then_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// expr
steps.push_back(crate::builder::Step::FormatWider(
if_else.then_expr.unwrap(),
));
// then_expr
steps.push_back(crate::builder::Step::FormatWider(parsed.then_expr));
steps.push_back(crate::builder::Step::Dedent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// /**/
if !if_else.comments_after_then_expr.is_empty() {
for text in if_else.comments_after_then_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// comments_after_then_expr
for text in parsed.comments_after_then_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// else
steps.push_back(crate::builder::Step::Token(
rnix::SyntaxKind::TOKEN_ELSE,
"else".to_string(),
));
// else_
steps.push_back(crate::builder::Step::Format(parsed.else_));
if if_else.comments_before_else_expr.is_empty() {
// expr
let element = if_else.else_expr.unwrap();
if parsed.comments_before_else_expr.is_empty() {
// else_expr
if matches!(
element.kind(),
parsed.else_expr.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IF_ELSE
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING
) || crate::builder::fits_in_single_line(build_ctx, element.clone())
{
) || crate::builder::fits_in_single_line(
build_ctx,
parsed.else_expr.clone(),
) {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(element));
steps
.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
} 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(element));
steps
.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
steps.push_back(crate::builder::Step::Dedent);
}
} else {
// /**/
// comments_before_else_expr
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
for text in if_else.comments_before_else_expr {
for text in parsed.comments_before_else_expr {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
// expr
steps.push_back(crate::builder::Step::FormatWider(
if_else.else_expr.unwrap(),
));
// else_expr
steps.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
steps.push_back(crate::builder::Step::Dedent);
}