1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::io::RenderKotlin;
use crate::spec::{VisibilityModifier, Class, CodeBlock, Function, Property};
use crate::spec::kdoc::{KdocSlot, mixin_kdoc_mutators};
use crate::tokens;

/// Companion object for class
/// Can contain properties, functions, subclasses and init blocks like class itself.
#[derive(Debug, Clone)]
pub struct CompanionObject {
    visibility_modifier: VisibilityModifier,
    member_nodes: Vec<crate::spec::class::ClassMemberNode>,
    kdoc: KdocSlot
}

impl CompanionObject {
    pub fn new() -> Self {
        CompanionObject {
            member_nodes: Vec::new(),
            visibility_modifier: VisibilityModifier::default(),
            kdoc: KdocSlot::default()
        }
    }

    /// Adds a property to the companion object
    pub fn property(mut self, property: Property) -> Self {
        self.member_nodes.push(crate::spec::class::ClassMemberNode::Property(property));
        self
    }

    /// Adds a function to the companion object
    pub fn function(mut self, function: Function) -> Self {
        self.member_nodes.push(crate::spec::class::ClassMemberNode::Function(function));
        self
    }

    /// Adds a subclass to the companion object
    pub fn subclass(mut self, subclass: Class) -> Self {
        self.member_nodes.push(crate::spec::class::ClassMemberNode::Subclass(subclass));
        self
    }

    /// Adds an init block to the companion object
    pub fn init<CodeBlockLike: Into<CodeBlock>>(mut self, block: CodeBlockLike) -> Self {
        self.member_nodes.push(crate::spec::class::ClassMemberNode::InitBlock(block.into()));
        self
    }

    /// Sets the visibility modifier for the companion object
    pub fn visibility_modifier(mut self, visibility_modifier: VisibilityModifier) -> Self {
        self.visibility_modifier = visibility_modifier;
        self
    }

    mixin_kdoc_mutators!();
}

impl RenderKotlin for CompanionObject {
    fn render_into(&self, block: &mut CodeBlock) {
        block.push_renderable(&self.kdoc);
        block.push_renderable(&self.visibility_modifier);
        block.push_space();
        block.push_static_atom(tokens::keyword::COMPANION);
        block.push_space();
        block.push_static_atom(tokens::keyword::OBJECT);
        block.push_space();
        block.push_curly_brackets(|code| {
            for node in &self.member_nodes {
                code.push_renderable(node);
                code.push_new_line();
            }
        });
    }
}

#[cfg(test)]
mod tests {
    use crate::io::RenderKotlin;
    use crate::spec::{Class, CodeBlock, CompanionObject, Function, Parameter, Name, Property, Type};

    #[test]
    fn companion_object() {
        let companion = CompanionObject::new()
            .property(
                Property::new(Name::from("name"), Type::string())
                    .initializer(CodeBlock::atom("\"John Doe\""))
            )
            .function(
                Function::new(Name::from("printName"))
                    .parameter(Parameter::new(Name::from("name"), Type::string()))
                    .body(CodeBlock::statement("println(name)"))
            );

        let code = companion.render_string();
        assert_eq!(
            code,
            "public companion object {\n    public final val name: kotlin.String = \"John Doe\"\n    public fun printName(name: kotlin.String): kotlin.Unit {\n        println(name)\n    }\n}"
        );
    }

    #[test]
    fn companion_object_with_subclass() {
        let subclass = Class::new("Subclass");
        let companion = CompanionObject::new()
            .subclass(subclass);

        let code = companion.render_string();
        assert_eq!(code, "public companion object {\n    public final class Subclass {\n\n    }\n}");
    }

    #[test]
    fn companion_object_with_kdoc() {
        let companion = CompanionObject::new()
            .kdoc("Hello\nWorld");

        let code = companion.render_string();
        assert_eq!(code, "/**\n * Hello\n * World\n */\npublic companion object {\n}");
    }

    #[test]
    fn companion_object_with_init_block() {
        let companion = CompanionObject::new()
            .init(CodeBlock::statement("println(\"init block\")"));

        let code = companion.render_string();
        assert_eq!(code, "public companion object {\n    init{\n        println(\"init block\")\n    }\n}");
    }
}