1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 21:17:45 +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; use std::collections::LinkedList;
#[derive(Debug, Default)] #[derive(Debug)]
pub(crate) struct IfElse { pub(crate) struct IfElse {
pub if_: rnix::SyntaxElement,
pub comments_before_if_expr: LinkedList<String>, 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 comments_after_if_expr: LinkedList<String>,
pub then_: rnix::SyntaxElement,
pub comments_before_then_expr: LinkedList<String>, 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 comments_after_then_expr: LinkedList<String>,
pub else_: rnix::SyntaxElement,
pub comments_before_else_expr: LinkedList<String>, pub comments_before_else_expr: LinkedList<String>,
pub else_expr: Option<rnix::SyntaxElement>, pub else_expr: rnix::SyntaxElement,
} }
pub(crate) fn parse( impl IfElse {
build_ctx: &crate::builder::BuildCtx, pub(crate) fn parse(
node: &rnix::SyntaxNode, build_ctx: &crate::builder::BuildCtx,
) -> IfElse { node: &rnix::SyntaxNode,
let mut if_else = IfElse::default(); ) -> 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 // comments_before_if_expr
children.get_next().unwrap(); 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(_) => {}
});
// /**/ // if_expr
children.drain_trivia(|element| match element { let if_expr = children.get_next().unwrap();
crate::children::Trivia::Comment(text) => {
if_else.comments_before_if_expr.push_back(text); // 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> { ) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new(); 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 // if_
steps.push_back(crate::builder::Step::Token( steps.push_back(crate::builder::Step::Format(parsed.if_));
rnix::SyntaxKind::TOKEN_IF,
"if".to_string(),
));
if if_else.comments_before_if_expr.is_empty() { if parsed.comments_before_if_expr.is_empty() {
// expr // if_expr
let element = if_else.if_expr.unwrap(); if crate::builder::fits_in_single_line(
if crate::builder::fits_in_single_line(build_ctx, element.clone()) { build_ctx,
parsed.if_expr.clone(),
) {
steps.push_back(crate::builder::Step::Whitespace); 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 { } else {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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::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::Dedent);
} }
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);
} else { } else {
// /**/ // comments_before_if_expr
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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);
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::Comment(text));
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);
} }
// expr // if_expr
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(parsed.if_expr));
if_else.if_expr.unwrap(),
));
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
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);
} }
// /**/ // comments_after_if_expr
if !if_else.comments_after_if_expr.is_empty() { for text in parsed.comments_after_if_expr {
for text in if_else.comments_after_if_expr { steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::Comment(text)); 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);
}
} }
// then // then_
steps.push_back(crate::builder::Step::Token( steps.push_back(crate::builder::Step::Format(parsed.then_));
rnix::SyntaxKind::TOKEN_THEN,
"then".to_string(),
));
if if_else.comments_before_then_expr.is_empty() { if parsed.comments_before_then_expr.is_empty() {
// expr // then_expr
let element = if_else.then_expr.unwrap();
if matches!( if matches!(
element.kind(), parsed.then_expr.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LET_IN | rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING | 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::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(element)); steps
.push_back(crate::builder::Step::FormatWider(parsed.then_expr));
} else { } else {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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::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::Dedent);
} }
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);
} else { } else {
// /**/ // comments_before_then_expr
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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);
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::Comment(text));
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);
} }
// expr // then_expr
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(parsed.then_expr));
if_else.then_expr.unwrap(),
));
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
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);
} }
// /**/ // comments_after_then_expr
if !if_else.comments_after_then_expr.is_empty() { for text in parsed.comments_after_then_expr {
for text in if_else.comments_after_then_expr { steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::Comment(text)); 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);
}
} }
// else // else_
steps.push_back(crate::builder::Step::Token( steps.push_back(crate::builder::Step::Format(parsed.else_));
rnix::SyntaxKind::TOKEN_ELSE,
"else".to_string(),
));
if if_else.comments_before_else_expr.is_empty() { if parsed.comments_before_else_expr.is_empty() {
// expr // else_expr
let element = if_else.else_expr.unwrap();
if matches!( if matches!(
element.kind(), parsed.else_expr.kind(),
rnix::SyntaxKind::NODE_ATTR_SET rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IF_ELSE | rnix::SyntaxKind::NODE_IF_ELSE
| rnix::SyntaxKind::NODE_LET_IN | rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING | 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::Whitespace);
steps.push_back(crate::builder::Step::FormatWider(element)); steps
.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
} else { } else {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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::FormatWider(element)); steps
.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }
} else { } else {
// /**/ // comments_before_else_expr
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
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);
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::Comment(text));
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);
} }
// expr // else_expr
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(parsed.else_expr));
if_else.else_expr.unwrap(),
));
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }