|
| 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 mut children = crate::children2::new(build_ctx, node); |
| 8 | + |
| 9 | + let first = children.next().unwrap(); |
| 10 | + let second = children.next().unwrap(); |
| 11 | + let third = children.next().unwrap(); |
| 12 | + let fourth = children.next().unwrap(); |
| 13 | + |
| 14 | + let vertical = build_ctx.vertical |
| 15 | + || first.has_inline_comment |
| 16 | + || first.has_trivialities |
| 17 | + || second.has_inline_comment |
| 18 | + || second.has_trivialities |
| 19 | + || third.has_inline_comment |
| 20 | + || third.has_trivialities |
| 21 | + || fourth.has_inline_comment |
| 22 | + || fourth.has_trivialities; |
| 23 | + |
| 24 | + // first |
| 25 | + steps.push_back(crate::builder::Step::Format(first.element)); |
| 26 | + |
| 27 | + if let Some(text) = first.inline_comment { |
| 28 | + steps.push_back(crate::builder::Step::Whitespace); |
| 29 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 30 | + steps.push_back(crate::builder::Step::NewLine); |
| 31 | + steps.push_back(crate::builder::Step::Pad); |
| 32 | + } else if first.has_comments { |
| 33 | + steps.push_back(crate::builder::Step::NewLine); |
| 34 | + steps.push_back(crate::builder::Step::Pad); |
| 35 | + } else { |
| 36 | + steps.push_back(crate::builder::Step::Whitespace); |
| 37 | + } |
| 38 | + |
| 39 | + for trivia in first.trivialities { |
| 40 | + match trivia { |
| 41 | + crate::children2::Trivia::Comment(text) => { |
| 42 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 43 | + steps.push_back(crate::builder::Step::NewLine); |
| 44 | + steps.push_back(crate::builder::Step::Pad); |
| 45 | + } |
| 46 | + crate::children2::Trivia::Newlines(_) => {} |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // second |
| 51 | + if vertical { |
| 52 | + steps.push_back(crate::builder::Step::FormatWider(second.element)); |
| 53 | + } else { |
| 54 | + steps.push_back(crate::builder::Step::Format(second.element)); |
| 55 | + } |
| 56 | + |
| 57 | + // third |
| 58 | + steps.push_back(crate::builder::Step::Format(third.element)); |
| 59 | + |
| 60 | + if let Some(text) = third.inline_comment { |
| 61 | + steps.push_back(crate::builder::Step::Whitespace); |
| 62 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 63 | + steps.push_back(crate::builder::Step::NewLine); |
| 64 | + steps.push_back(crate::builder::Step::Pad); |
| 65 | + } |
| 66 | + |
| 67 | + for trivia in third.trivialities { |
| 68 | + match trivia { |
| 69 | + crate::children2::Trivia::Comment(text) => { |
| 70 | + steps.push_back(crate::builder::Step::NewLine); |
| 71 | + steps.push_back(crate::builder::Step::Pad); |
| 72 | + steps.push_back(crate::builder::Step::Comment(text)); |
| 73 | + } |
| 74 | + crate::children2::Trivia::Newlines(_) => {} |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // fourth |
| 79 | + if vertical { |
| 80 | + if matches!( |
| 81 | + fourth.element.kind(), |
| 82 | + rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH |
| 83 | + ) { |
| 84 | + steps.push_back(crate::builder::Step::NewLine); |
| 85 | + steps.push_back(crate::builder::Step::Pad); |
| 86 | + steps.push_back(crate::builder::Step::FormatWider(fourth.element)); |
| 87 | + } else if third.has_inline_comment |
| 88 | + || third.has_comments |
| 89 | + || !matches!( |
| 90 | + fourth.element.kind(), |
| 91 | + rnix::SyntaxKind::NODE_ATTR_SET |
| 92 | + | rnix::SyntaxKind::NODE_IDENT |
| 93 | + | rnix::SyntaxKind::NODE_PAREN |
| 94 | + | rnix::SyntaxKind::NODE_LET_IN |
| 95 | + | rnix::SyntaxKind::NODE_LIST |
| 96 | + | rnix::SyntaxKind::NODE_LITERAL |
| 97 | + | rnix::SyntaxKind::NODE_STRING |
| 98 | + ) |
| 99 | + { |
| 100 | + steps.push_back(crate::builder::Step::Indent); |
| 101 | + steps.push_back(crate::builder::Step::NewLine); |
| 102 | + steps.push_back(crate::builder::Step::Pad); |
| 103 | + steps.push_back(crate::builder::Step::FormatWider(fourth.element)); |
| 104 | + steps.push_back(crate::builder::Step::Dedent); |
| 105 | + } else { |
| 106 | + steps.push_back(crate::builder::Step::Whitespace); |
| 107 | + steps.push_back(crate::builder::Step::FormatWider(fourth.element)); |
| 108 | + } |
| 109 | + } else { |
| 110 | + steps.push_back(crate::builder::Step::Whitespace); |
| 111 | + steps.push_back(crate::builder::Step::Format(fourth.element)); |
| 112 | + } |
| 113 | + |
| 114 | + steps |
| 115 | +} |
0 commit comments