pretty_print/helpers/
soft_block.rs1use super::*;
2
3#[derive(Clone, Debug)]
14pub struct SoftBlock {
15 pub indent: usize,
17 pub lhs: &'static str,
19 pub rhs: &'static str,
21 pub joint: PrettyTree,
23 pub tail: PrettyTree,
25}
26
27impl SoftBlock {
28 pub fn new(lhs: &'static str, rhs: &'static str) -> Self {
30 Self { lhs, rhs, indent: 4, joint: PrettyTree::line_or_space(), tail: PrettyTree::Nil }
31 }
32 pub fn tuple() -> Self {
34 Self::new("(", ")")
35 }
36 pub fn parentheses() -> Self {
38 Self::new("(", ")")
39 }
40 pub fn brackets() -> Self {
42 Self::new("[", "]")
43 }
44 pub fn curly_braces() -> Self {
46 Self::new("{", "}")
47 }
48 pub fn with_joint(self, joint: PrettyTree) -> Self {
50 Self { joint, ..self }
51 }
52}
53
54impl SoftBlock {
55 pub fn join_slice<T: PrettyPrint>(&self, slice: &[T], theme: &PrettyProvider) -> PrettyTree {
57 let mut outer = PrettySequence::new(5);
58 outer += self.lhs;
59 outer += PrettyTree::line_or_space();
60 let mut inner = PrettySequence::new(slice.len() * 2);
61 for (idx, term) in slice.iter().enumerate() {
62 if idx != 0 {
63 inner += self.joint.clone();
64 }
65 inner += term.pretty(theme);
66 }
67 outer += inner.indent(self.indent);
68 outer += PrettyTree::line_or_space();
69 outer += self.rhs;
70 outer.into()
71 }
72}