|
| 1 | +pub(crate) fn rule( |
| 2 | + build_ctx: &crate::builder::BuildCtx, |
| 3 | + node: &rnix::SyntaxNode, |
| 4 | +) -> std::collections::LinkedList<crate::builder::Step> { |
| 5 | + let mut steps = std::collections::LinkedList::new(); |
| 6 | + |
| 7 | + let parsed = |
| 8 | + crate::parsers::assert_or_with::AssertOrWith::new(build_ctx, node); |
| 9 | + |
| 10 | + let vertical = build_ctx.vertical |
| 11 | + || !parsed.comments_after_assert_or_with.is_empty() |
| 12 | + || parsed.has_newlines_after_assert_or_with |
| 13 | + || !parsed.comments_after_semicolon.is_empty() |
| 14 | + || parsed.has_newlines_after_semicolon; |
| 15 | + |
| 16 | + // assert_or_with |
| 17 | + steps.push_back(crate::builder::Step::Format(parsed.assert_or_with)); |
| 18 | + |
| 19 | + // comments_after_assert_or_with |
| 20 | + if parsed.comments_after_assert_or_with.is_empty() { |
| 21 | + steps.push_back(crate::builder::Step::Whitespace); |
| 22 | + } else { |
| 23 | + steps.push_back(crate::builder::Step::NewLine); |
| 24 | + steps.push_back(crate::builder::Step::Pad); |
| 25 | + for text in parsed.comments_after_assert_or_with { |
| 26 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 27 | + steps.push_back(crate::builder::Step::NewLine); |
| 28 | + steps.push_back(crate::builder::Step::Pad); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // first_expression |
| 33 | + if vertical { |
| 34 | + steps.push_back(crate::builder::Step::FormatWider( |
| 35 | + parsed.first_expression, |
| 36 | + )); |
| 37 | + } else { |
| 38 | + steps.push_back(crate::builder::Step::Format(parsed.first_expression)); |
| 39 | + } |
| 40 | + |
| 41 | + // semicolon |
| 42 | + steps.push_back(crate::builder::Step::Format(parsed.semicolon)); |
| 43 | + |
| 44 | + // comments_after_semicolon |
| 45 | + let has_comments_after_semicolon = |
| 46 | + !parsed.comments_after_semicolon.is_empty(); |
| 47 | + for text in parsed.comments_after_semicolon { |
| 48 | + steps.push_back(crate::builder::Step::NewLine); |
| 49 | + steps.push_back(crate::builder::Step::Pad); |
| 50 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 51 | + } |
| 52 | + |
| 53 | + // second_expression |
| 54 | + if vertical { |
| 55 | + if matches!( |
| 56 | + parsed.second_expression.kind(), |
| 57 | + rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH |
| 58 | + ) { |
| 59 | + steps.push_back(crate::builder::Step::NewLine); |
| 60 | + steps.push_back(crate::builder::Step::Pad); |
| 61 | + steps.push_back(crate::builder::Step::FormatWider( |
| 62 | + parsed.second_expression, |
| 63 | + )); |
| 64 | + } else if has_comments_after_semicolon |
| 65 | + || !matches!( |
| 66 | + parsed.second_expression.kind(), |
| 67 | + rnix::SyntaxKind::NODE_ATTR_SET |
| 68 | + | rnix::SyntaxKind::NODE_IDENT |
| 69 | + | rnix::SyntaxKind::NODE_PAREN |
| 70 | + | rnix::SyntaxKind::NODE_LET_IN |
| 71 | + | rnix::SyntaxKind::NODE_LIST |
| 72 | + | rnix::SyntaxKind::NODE_LITERAL |
| 73 | + | rnix::SyntaxKind::NODE_STRING |
| 74 | + ) |
| 75 | + { |
| 76 | + steps.push_back(crate::builder::Step::Indent); |
| 77 | + steps.push_back(crate::builder::Step::NewLine); |
| 78 | + steps.push_back(crate::builder::Step::Pad); |
| 79 | + steps.push_back(crate::builder::Step::FormatWider( |
| 80 | + parsed.second_expression, |
| 81 | + )); |
| 82 | + steps.push_back(crate::builder::Step::Dedent); |
| 83 | + } else { |
| 84 | + steps.push_back(crate::builder::Step::Whitespace); |
| 85 | + steps.push_back(crate::builder::Step::FormatWider( |
| 86 | + parsed.second_expression, |
| 87 | + )); |
| 88 | + } |
| 89 | + } else { |
| 90 | + steps.push_back(crate::builder::Step::Whitespace); |
| 91 | + steps.push_back(crate::builder::Step::Format(parsed.second_expression)); |
| 92 | + } |
| 93 | + |
| 94 | + steps |
| 95 | +} |
0 commit comments