Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: if else #217

Merged
merged 1 commit into from Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
138 changes: 79 additions & 59 deletions src/alejandra_engine/src/parsers/if_else.rs
@@ -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);
}
crate::children::Trivia::Whitespace(_) => {}
});
// if_expr
let if_expr = children.get_next().unwrap();

// expr
if_else.if_expr = Some(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(_) => {}
});

// /**/
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_
let then_ = children.get_next().unwrap();

// 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(_) => {}
});

// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
if_else.comments_before_then_expr.push_back(text);
}
crate::children::Trivia::Whitespace(_) => {}
});
// then_expr
let then_expr = children.get_next().unwrap();

// expr
if_else.then_expr = Some(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(_) => {}
});

// /**/
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_
let else_ = children.get_next().unwrap();

// 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(_) => {}
});

// /**/
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());
// else_expr
let else_expr = children.get_next().unwrap();

if_else
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,
}
}
}
129 changes: 59 additions & 70 deletions src/alejandra_engine/src/rules/if_else.rs
Expand Up @@ -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);
}

Expand Down